Elementary programming with c einternational

Page 165

Session 9

Loop Concepts

Example 12: #include <stdio.h> main () { int count1, count2; for(count1 = 1, count2 = 0; count1 <=100; count1++) { printf(“Enter %d Count2 : “, count1); scanf(“%d”, &count2); if(count2 == 100) break; } } A sample output is shown below: Enter 1 count2 : 10 Enter 2 count2 : 20 Enter 3 count2 : 100 In the above code, the user can enter 100 values for j. However, if 100 is entered, the loop ends and control is passed to the next statement. Another point to be remembered while using a break is that it causes an exit from an inner loop. This means that if a for loop is nested within another for loop, and a break statement is encountered in the inner loop, the control is passed back to the outer for loop.

9.2.4 The ‘continue’ Statement The continue statement causes the next iteration of the enclosing loop to being. When this statement is met in the program, the remaining statements in the body of the loop are skipped and the control is passed to the re-initialization step. In case of the for loop, continue causes the increment portions of the loop and then the conditional test to be executed. In case of the while and do...while loops, program control passes to the conditional tests. For example: Example 13: #include <stdio.h> main () Elementary Programming with C

Version 1.1 © Aptech Limited

page 155 of 356


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.