The document discusses different types of looping statements in programming languages. It describes while loops, do-while loops, and for loops. While and for loops are pretest loops, where the condition is tested before each iteration. Do-while loops are posttest loops, where the condition is tested after each iteration, ensuring the body executes at least once. Syntax and examples are provided for each loop type to illustrate their usage and output.
Introduction to looping statements and their purpose. Looping involves repeatedly executing a block until certain conditions are met. Types are pretest and posttest.
Comparison of pretest (like for and while loops) and posttest (like do-while loops) structures and conditions.
Description and syntax of the while loop, an entry-controlled structure, with an example demonstrating its use and output.
Explanation of the do-while loop, which is exit-controlled, including its syntax and an example showcasing its functionality.
Overview of for loops as entry-controlled loops, detailing execution steps, syntax, and variations of initialization and increment.
Example usage of the for loop, demonstrating the output through a simple print statement executed multiple times.
LOOPING • Looping isthe process of repeatedly executing a block of statements until some conditions for the termination of the loop are satisfied. • Two types of loop structure are: • 1) pretest • 2) posttest
4.
Pretest v/s posttest Pretest •Condition is tested before each iteration to check if loops should occur. • eg: For & while loop. Posttest • Condition is tested after each iteration to check if loops should continue (at least a single iteration occurs). • Eg: do while loop.
5.
The while statement •It is an entry-controlled loop statement. The test condition is evaluated and if it is true, the body of the loop is executed. After execution of the body, the condition is again evaluated and if it is true, the body of the loop is executed again. This process continuous until the test condition becomes false and the control is transferred out of the loop.
The do_while statement •It is an exit controlled loop statement. The body of the loop of do statement is compulsorily executed for the first time. After executing once., at the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the body of the loop is executed again and this process continuous until the condition becomes false, the loop execution is terminated and the control goes to the statement that appears after the while statement.
The for statement: •It is also an entry-controlled loop. • All for loops are executed in following sequence. • 1] it executes initialization statements • 2] it check the test condition if true than go to step:3 other wise step 4. • 3] execute body of loop and go to step 2 after increment or decrement • 4] other statement of the program.
•Different ways ofusing for loop: •More than one variable can be initialized in the for loop. For(p=1, n=0; n<4; ++n) •The increment section may contain more than one part. For(p=1,n=0; n<4; ++n, ++p) •You can either do increment or decrement. For(n=4; n>0; -n) •You can also omit one or more sections in the for loop. For( ; n<4; )