Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.

Similar presentations


Presentation on theme: "Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1."— Presentation transcript:

1 Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1

2 Code JunkiesLoops 2 Types of loops  definite loop: A loop that executes a known number of times.  indefinite loop: A loop where it is not easily determined in advance how many times it will execute. Indefinatedefinate

3 For Loop  Is for loop syntax clear to everybody?  for(initialization ;test condition ; update ); Code JunkiesLoops 3

4 Code JunkiesLoops 4 Nested for loops  A for loop can contain any kind of statement in its body, including another for loop. –The inner loop must have a different name for its loop counter variable so that it will not conflict with the outer loop.  nested loop: Loops placed inside one another, creating a loop of loops. for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { cout<<"six”; } Output: six

5 Code JunkiesLoops 5 Nested for loop exercise  What is the output of the following nested for loop? for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { cout<<"*”; } cout<<“\n”; }  Output: * ** *** **** ***** ******

6 Code JunkiesLoops 6 Nested for loop exercise  What is the output of the following nested for loop? for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { cout<<i; } cout<<“\n”; }  Output: 1 22 333 4444

7 Code JunkiesLoops 7 Nested for loop exercise  Create a nested for loops produce the following output.....1...22..333.4444 55555

8 Code JunkiesLoops 8 Nested for loop exercise  A for loop can have more than one loop nested in it.  What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= (5 - i); j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(i); } System.out.println(); }  Answer: 1 22 333 4444 55555

9 Code JunkiesLoops 9 Common nested loop bugs  It is a common bug to accidentally type the wrong loop counter variable, which can lead to incorrect behavior. –What is the output of the following nested loops? for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 5; j++) { cout<<j; } cout<<“\n”; } –What is the output of the following nested loops? for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; i++) { cout<<j; } cout<<“\n”; }

10 While Loop Code JunkiesLoops 10

11 Code JunkiesLoops 11 Equivalence of for,while loops  Any for loop of the following form: for ( ; ; ) { ; } can be replaced by a while loop of the following form: ; while ( ) { ; }

12 Code JunkiesLoops 12 The while loop statement  The while loop is a new loop statement that is well suited to writing indefinite loops.  The while loop, general syntax: while ( ) { ; } –Example: int number = 1; while (number <= 200) { cout<<number<<“\n”; number *= 2; } –OUTPUT: 1 2 4 8 16 32 64 128

13 Code JunkiesLoops 13 While loop flow chart  The execution of a while loop can be depicted as the following: | V +---------------+ +---- --+ | +---------------+ | | ^ V V | +-----------------------------------+ | | | execute the controlled statements | | | +-----------------------------------+ | ^ | V | V | | | | +---<-----<-------<-----+ V +-------------------+ | execute statement | | after while loop | +-------------------+

14 Code JunkiesLoops 14 Example while loop  A loop that finds and prints the first factor of a number (other than 1): Cout<<"Type a number:“ ; int number; Cin>>number; int factor = 2; while (number % factor != 0) { factor++; } Cout<<“First factor: “<<factor;  OUTPUT: Type a number: 49 First factor: 7


Download ppt "Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1."

Similar presentations


Ads by Google