Content. • [1] Looping statement. • [2] WHILE LOOP. • [3] DO WHILE LOOP. • [4] FOR LOOP.
LOOPING • Looping is the 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
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.
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.
Syntax: while(test expression) { Body of the loop } statement-x;
Example: While(i=0; i<3; i++) printf(“hellon”); output: Hello Hello Hello
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.
Syntax: Do { body of the loop } While (test condition); Statement-x;
Example: Do { printf(“Hellon”); } While(i<3) Output: Hello Hello Hello
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.
Syntax: For(initialization; test condition; increment) { body of the loop }
•Different ways of using 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; )
Example: For(i=0; i<3; i+) Printf(“Hellon”); Output: Hello Hello Hello
Looping (Computer programming and utilization)

Looping (Computer programming and utilization)

  • 2.
    Content. • [1] Loopingstatement. • [2] WHILE LOOP. • [3] DO WHILE LOOP. • [4] FOR LOOP.
  • 3.
    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.
  • 6.
  • 7.
  • 8.
    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.
  • 9.
    Syntax: Do { body of theloop } While (test condition); Statement-x;
  • 10.
  • 11.
    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.
  • 12.
    Syntax: For(initialization; test condition;increment) { body of the loop }
  • 13.
    •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; )
  • 14.