150 likes | 245 Views
AP Computer Science Ch. 8 LOOPS. For Loops. A For Loop is a coding structure that allows repetition across a set of values. An iterator (integer) keeps track of the values. The code for a typical for loop looks something like this: for(int i=0; i<5; i++) { code ……… }
E N D
For Loops • A For Loop is a coding structure that allows repetition across a set of values. An iterator (integer) keeps track of the values. • The code for a typical for loop looks something like this: for(int i=0; i<5; i++) { code ……… } A for loop executes as follows: • The initial statement executes. (In this case, i=0 is the initial statement – i must be declared with int if it isn’t already declared!) • The loop condition is evaluated. (In this case, i<5.) If the condition is true then, • Execute the code inside the loop. • Execute the update expression (In this case, i++.) • Repeat Step 2 until the loop condition evaluates to false. • The for loop in this case would run the code inside of it 5 times! (i=0, 1, 2, 3, 4) Notice the semi-colons – not commas!
Example 1 int x=0; for (int i=1; i<=16; i+=3) x=x+i; Final Value of i: Final Value of x: Number of Loops: 19 51 6
Guess the Output! int i=0, x=0; for(i=1; i<6; i++) x = x+i; System.out.println(x); 15 int i=0, x=0; for(i=1; i<6; i++) { x = x+i; System.out.println(x); } 1 3 6 10 15
Write the Loop! Print 10 stars (*) on different lines! for(int i=0; i<10; i++) System.out.println(“*”); Assume the user has already inputted a positive integer x. Find the xth power of 3! int power=1; for (int i=0; i<x; i++) power = power * 3;
While Loops • A While Loop continues to execute as long as a certain expression is true. The code for a typical while loop looks something like this: int i=0; while(i<7) { code ……… } • This while loop would run the code inside of it as long as i<7. • It is possible that a while loop might never run at all! (What if I declared i to be 10 instead of 0?)
Guess the Output! int x=21; while(x<30) x++; System.out.println(x); 30
Guess the Output! int x = 30; while(x>=10) x+=5; System.out.println(x); Infinite Loop! Program Crashes!
Guess the Output! Suppose that the user enters the following input: 23 6 0 7 9 10 -4 Output: 51 int x=0, sum=0; Scanner c=new Scanner(System.in); while(x>=0) { x=c.nextInt(); sum+=x; } System.out.println(sum); How could I fix this code so that the negative number is not included in the sum? Switch the two lines in the body of the loop.
Do-While (Do) Loops • A Do-While Loop is similar to a while loop because it continues to execute as long as a certain expression is true. The code for a typical do-while loop looks something like this: int i=0; do { code ……… } while(i<7); • This loop would run the code inside of it as long as i<7. • A do-while loop always executes at least once!
Guess the Output! int x=100; do { x= x+1; }while(x<30); System.out.println(x); 101
More Loop Stuff • A break statement in a loop will immediately exit the body of a loop. • A return statement in a loop will also stop a loop and return the value of the function. • Example: What is the output if the user enters 8 4 6 -5? int x=5, num, sum=0; Scanner console=new Scanner(System.in); while(x>0) { System.out.println("Enter a number."); num=console.nextInt(); if(num<0) break; else sum+=num; x--; } System.out.println("The sum is " + sum +"."); The sum is 18.
Guess the Output! for(int j=0; j<8; j++) { System.out.print(j*25 + " – "); if(j!=7) System.out.println((j+1)*25-1); else System.out.println((j+1)*25); } 0 – 24 25 – 49 50 – 74 75 – 99 100 – 124 125 – 149 150 – 174 175 – 200
Guess the Output! for(int i=1; i<5; i++) { for(int j=3; j>0; j--) System.out.print(i*j + " "); System.out.println(); } 3 2 1 6 4 2 9 6 3 12 8 4