Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic : While, For, Do-While Loop Guided By : Branch : Batch :

Similar presentations


Presentation on theme: "Topic : While, For, Do-While Loop Guided By : Branch : Batch :"— Presentation transcript:

1 Topic : While, For, Do-While Loop Guided By : Branch : Batch :

2 Name ENROLLMENT NO. Abhishek Chokshi 140120109005 Raj Shah Kaveesh Raval 140120109054 140120109016

3

4  The syntax of while statement in C: loop repetition condition while (loop repetition condition) statement  Loop repetition condition  Loop repetition condition is the condition which controls the loop. true  The statement is repeated as long as the loop repetition condition is true. infinite loop  A loop is called an infinite loop if the loop repetition condition is always true.

5

6  A while statement has the following syntax: while ( condition ){ statement; } If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false

7 statement true false condition evaluated

8 int count = 1; while (count <= 5){ System.out.println (count); count++; } If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times

9

10  The syntax of the do-while statement in C: loop repetition condition do statement while (loop repetition condition);  The statement is first executed. loop repetition condition  If the loop repetition condition is satisfied, the statement is repeated else, the repetition of the loop is stopped.

11 Action true false Expression

12 true condition evaluated statement false

13 do { statement-1; statement-2; …… statement-k; } while(condition);

14 Initialize Statement-1 Statement-2 Statement-k Is condition TRUE? N Y

15 /* Find an even number input */ do{ printf(“Enter a value:\n”); scanf(“%d”,&num); }while (num%2!=0) printf(“\n%d”,num); This loop will repeat if the user inputs odd number.

16  The syntax of for statement in C: initialization expression for (initialization expression; loop repetition condition loop repetition condition; update expression update expression) statement initialization expression  The initialization expression set the initial value of the loop control variable. loop repetition condition  The loop repetition condition test the value of the loop control variable. update expression  The update expression update the loop control variable.

17  A for statement has the following syntax: for ( initialization ; condition ; increment ){ statement; } The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration

18 statement true condition evaluated false increment initialization

19  A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; increment; }

20  An example of a for loop: for (int count=1; count <= 5; count++){ System.out.println (count); } The initialization section can be used to declare a variable Like a while loop, the condition of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times

21  The increment section can perform any calculation A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance for (int num=100; num > 0; num -= 5){ System.out.println (num); }

22  Each expression in the header of a for loop is optional  If the initialization is left out, no initialization is performed  If the condition is left out, it is always considered to be true, and therefore creates an infinite loop ◦ We usually call this a “forever loop”  If the increment is left out, no increment operation is performed

23

24


Download ppt "Topic : While, For, Do-While Loop Guided By : Branch : Batch :"

Similar presentations


Ads by Google