Loop Questions In C
Which of the following statements about the while loop is not true? A.The while loop is a posttest loop. B.Testing condition is made before each iteration. C.The while loop statement must terminate with a semi-colon. There are 4 types of a loop statement in C. While loop; For Loop; DoWhile Loop; Nested Loop; Q #17) What is a nested loop? Ans) A loop running within another loop is referred as a nested loop. The first loop is called Outer loop and inside the loop is called Inner loop. Inner loop executes the number of times define an outer loop.
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is − 1701 ad gold edition download.
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.
Flow Diagram
Example
Live DemoWhen the above code is compiled and executed, it produces the following result −
Unlike for and while loops, which test the loop condition at the top of the loop, the do..while loop in C programming checks its condition at the bottom of the loop.
Tricky Loop Questions In C
A do..while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.
Syntax
The syntax of a do..while loop in C programming language is −
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.
Flow Diagram
Example
Do While Loop Questions In C
Live DemoWhile Loop Questions In C
When the above code is compiled and executed, it produces the following result −