answersLogoWhite

0


Best Answer

In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Indentation simply makes it much easier to see the structure of a program. By indenting a loop body, the loop body stands out and we can more easily see the extent of that body. We can also more easily see the loop's control statements.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

For readability. In some languages (python), the indent is required syntactically.

it is easier to read:

for (condition) {

statement;

statement;

statement;

}

then:

for (condition){

statement;

statement;

statement;

}

or:

for (condition){statement;statement;statement;}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The more readable code is, the easier it is to maintain. Indenting code is one way we can improve the readability of code. However, if we have a large or complex section of indented code within a loop (particularly within nested loops), we can improve the readability even further by placing that code in a separate function entirely. The loop can then be reduced to a "simple" statement rather than a compound one:

for (int i=0; i<100; ++i) do_something (i);

This answer is:
User Avatar

User Avatar

Ikraam Abkow

Lvl 2
1y ago

Why should you indent the statements in the body of a loop

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why should you indent the statement in the body of a loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which command is used to skip the rest of a loop and carry on from the top of the loop again?

From inside any loop statement, the continue; statement will skip any remaining statements and re-evaluate the loop's conditional expression. If that expression remains true, a new iteration of the loop begins, otherwise control passes to the statement that follows the loop. Note that in a for or while loop, the conditional expression is defined before the loop body but in a do loop it is defined after the loop body.


Where is the continue statement NOT usually used?

The continue statement is not actually used when it is the last statement of the body of the loop. Plus: outside any loop it is rarely or never used.


What is a while statement in turbo c plus plus?

A while statement is one type of looping statement. by which we can start a loop in our programs. while loop is precondition checking statement, because it first check its condition then loop will go to its body part. EX. while(i&gt;0) { //body part } here when i will &gt;0 then it will check it body part and execute it and display result.


What is flag controlled while loop?

A while loop evaluates a conditional expression at the start of each iteration. If the conditional expression evaluates false, execution passes to the statement that immediately follows the body of the loop. If the conditional expression evaluates true, the body of the loop executes one iteration. When the end of the loop is reached, or a continue statement is encountered within the body of the loop, control passes back to the while statement where the conditional expression is re-evaluated. If a break statement is encountered within the body of the loop, the loop terminates and control passes to the next statement. If a return statement is encountered within the body of the loop, the loop terminates and control passes to the calling function.while (expression) {// repeats until the expression evaluates false}The braces are optional when the body of the loop is a simple statement. Compound statements must be enclosed in braces.A flag-controlled while loop has a simple conditional expression that evaluates a Boolean value:bool x;// ...while (x==true) { // flag-controlled loop// ...}}The above loop can also be written without the equality operator:while (x) { // flag-controlled loop // ...}Moreover, any integral type (such as int or char) will implicitly convert to a bool such that non-zero values evaluate true and zero evaluates false. As such, any integral type can be used in a flag-controlled loop.


Can break statement be used without any loop?

The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }

Related questions

Which command is used to skip the rest of a loop and carry on from the top of the loop again?

From inside any loop statement, the continue; statement will skip any remaining statements and re-evaluate the loop's conditional expression. If that expression remains true, a new iteration of the loop begins, otherwise control passes to the statement that follows the loop. Note that in a for or while loop, the conditional expression is defined before the loop body but in a do loop it is defined after the loop body.


What is difference between for loop statement and do while statement in c language?

The for loop has an initializer, an end condition, a loop expression, and a body. The initializer always runs. If the end condition is not satisified, the loop ends or never starts. The loop expression runs at the end of each iteration. Note that the body of a for loop can run no times. The do while statement has a body and an end condition. The body is executed, and then the end condition determines if the loop will iterate. Like the for loop, the loop expression runs at the end of each iteration. Note that the body of a do while loop will run at least one time.


Where is the continue statement NOT usually used?

The continue statement is not actually used when it is the last statement of the body of the loop. Plus: outside any loop it is rarely or never used.


What is a while statement in turbo c plus plus?

A while statement is one type of looping statement. by which we can start a loop in our programs. while loop is precondition checking statement, because it first check its condition then loop will go to its body part. EX. while(i&gt;0) { //body part } here when i will &gt;0 then it will check it body part and execute it and display result.


Are The two parts of a while statement are known as the loop head and the loop body?

The syntax of while is: while (expression)statementThe 'while (expression)' part would be then 'head'; the 'statement' would be the 'body'


What is flag controlled while loop?

A while loop evaluates a conditional expression at the start of each iteration. If the conditional expression evaluates false, execution passes to the statement that immediately follows the body of the loop. If the conditional expression evaluates true, the body of the loop executes one iteration. When the end of the loop is reached, or a continue statement is encountered within the body of the loop, control passes back to the while statement where the conditional expression is re-evaluated. If a break statement is encountered within the body of the loop, the loop terminates and control passes to the next statement. If a return statement is encountered within the body of the loop, the loop terminates and control passes to the calling function.while (expression) {// repeats until the expression evaluates false}The braces are optional when the body of the loop is a simple statement. Compound statements must be enclosed in braces.A flag-controlled while loop has a simple conditional expression that evaluates a Boolean value:bool x;// ...while (x==true) { // flag-controlled loop// ...}}The above loop can also be written without the equality operator:while (x) { // flag-controlled loop // ...}Moreover, any integral type (such as int or char) will implicitly convert to a bool such that non-zero values evaluate true and zero evaluates false. As such, any integral type can be used in a flag-controlled loop.


What is difference between while and dowhile?

the test condition will be checked first after wards the body of the loop will be excuted in while statement and the the do while statement represented the body of the loop will be executed first and then the test condition will checked next


Can break statement be used without any loop?

The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }


Why for loop is not terminated in c?

Of course the for loop is terminated in C. All statements are terminated. Look at the syntax of the for statement... for (init-statement; test-condition; loop-statement) body-statement; That looks quite terminated to me. (By the semi-colon) Perhaps you are thinking about statement blocks... for (init-statement; test-condition; loop-statement) { statement; statement; ... statement; } ... Well, that is just fine because the body-statement, like any other statement, can be replaced by one or more statements enclosed in braces. There is still a terminating semi-colon on each statement. if i am understanding ur ques. right then according to me it is correct that for loop is not terminated because we do not execute for loop.we only check the conditions. those statements are terminated which we want to execute. because using a semicolon or terminating the statement means that statement is execitable.


What is while loop?

A do-while loop is a statement or series of statements that are executed at least once. At the end of each iteration, a conditional expression enclosed in a while() statement is evaluated to determine if the loop should start a new iteration or not.


How do you convert a while loop to do-while loop?

A for loop is classified as an iteration statement. A simple example might be... For x = 1 To 10 'loop body Next x The same loop expressed as a while loop (classified as a control flow statement) could be... x = 0 Do While x &lt; 11 'loop body x = x + 1 Loop .


Why the continue statement not allowed in swicth statements?

'continue' is allowed anywhere in a loop's body. But, sure, it is not synonym of 'break', which is a different statement.