Computer Science, asked by agarwalanushka068, 16 days ago

(c) Explain the following loops:

(i) for loop
(ii) while loop​

Answers

Answered by BtPrince8
0

Answer:

In a do…while loop, the condition is always executed after the body of a loop. It is also called an exit-controlled loop. 3. In a for loop, the initial value is performed only once, then the condition tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned

PLZ MARK ME AS BRAINLLEST

Answered by Patelhulsi
0

i)

for Loop

The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)

{

// statements inside the body of loop

}

Here is the flow of control in a 'for' loop −

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

ii) While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us. while loops are used in situations where we do not know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of test condition.

Syntax:

We have already stated that a loop is mainly consisted of three statements – initialization expression, test expression, update expression. The syntax of the three loops – For, while and do while mainly differs on the placement of these three statements.

initialization expression;

while (test_expression)

{

// statements

update_expression;

}

Similar questions