Solved

INDENTATION

Posted on 1998-11-14
2
438 Views
Last Modified: 2010-04-01
MORE INFORMATION ABOUT INDENTATION IN c++. Enough to write on the topic.
0
Comment
Question by:BS111498
2 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 20 total points
ID: 1177893
Indentation is only meant to make code more readable, it has no effect on the way a program is compiled.  Indentation is used to make it easier to determine where a block of code begins and ends.  If one block of code contains another block of code, it is generally indented two or three spaces away from the block of code containing it.  Also, indentation is used on individual statements, whether they are in a block or not, when these statements belong to other statements such as for or if.

for (i=0; i<10; i++)
  for (j=0; j<10; j++)
    x += y[i,j];
if (x > 100)
  x -= 100;

is much easier to read than:

for (i=0; i<10; i++)
for (j=0; j<10; j++)
x += y[i,j];
if (x > 100)
x -= 100;

however they both do exactly the same thing.  In the first example you know that the second "for" statement is part of the first "for" statement, and the x += y[i,j] belongs to the second for statement.  Also, you know that the "if" statement is not contained within the first or second "for" statements because it is at the same level of indentation as the first "for" statement.  You can also tell that the statement x =- 100 belongs the the if statement by looking at indentation.  In the same example, you can figure it out by looking closely, but it is much easier and visually appealing to use indentation.


0
 

Author Comment

by:BS111498
ID: 1177894
Scrapdog I am very grateful for the answer, I have search many textbooks for this subject and only about two lines is included.

The question was " indentation causes confusion"

If you have any reference on this subject, please email it to me
                                           thanks BS


























0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now