Download - Loops - cse.sc.edu
![Page 1: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/1.jpg)
![Page 2: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/2.jpg)
LoopsPart 02
![Page 3: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/3.jpg)
Syntax
for(<<Init>> ; <<Boolean expression>> ; <<Update>>){
//Body of the for-statement}//Outside Body of the for-statement
• For-statement• Counting Loop• Special kind of While-Statement• Arguments require 3 parts, separated by
semicolons– Init: This initialization of a counting variable. Only runs
once.– Boolean Expression: Just like before– Update: Updates the counting variable after all other
statements in the body have run• Putting curly braces “{}” to denote the body of
the for-statement is strongly encouraged• Do not put a semicolon “;” after the parenthesis• Spoken
– “Do that for this many times”
Examplesfor(int i=0; i<10; i++){
System.out.println(i);}
![Page 4: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/4.jpg)
General For-Statement Flow ChartSyntax
for(<<Init>> ; <<Boolean expression>> ; <<Update>>){
//Body of the for-statement}//Outside Body of the for-statement
Body of for
Outside Body of
for
true falseBoolean Expression
Update
Init
![Page 5: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/5.jpg)
While-Loop
<<Init>>; while(<<Boolean expression>>) {
//Body of the while-statement<<Update>>;
}//Outside Body of the while-statement
For-Loop
for(<<Init>> ; <<Boolean expression>> ; <<Update>>){
//Body of the for-statement}//Outside Body of the for-statement
![Page 6: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/6.jpg)
General For-Statement Flow ChartSyntax
for(<<Init>> ; <<Boolean expression>> ; <<Update>>){
//Body of the for-statement}//Outside Body of the for-statement
Body of for
Outside Body of while
true falseBoolean Expression
Update
Init
![Page 7: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/7.jpg)
General For-Statement Flow ChartSyntax
<<Init>>; while(<<Boolean expression>>) {
//Body of the while-statement<<Update>>;
}//Outside Body of the while-statement Body of
forOutside Body of while
true falseBoolean Expression
Update
Init
![Page 8: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/8.jpg)
Example
![Page 9: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/9.jpg)
Nested Example
![Page 10: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/10.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = DOES NOT EXISTj = DOES NOT EXIST
Console
![Page 11: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/11.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = DOES NOT EXISTj = DOES NOT EXIST
Console
![Page 12: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/12.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = DOES NOT EXIST
Console
![Page 13: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/13.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = DOES NOT EXIST
Console
![Page 14: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/14.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = DOES NOT EXIST
Console
![Page 15: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/15.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 0
Console
![Page 16: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/16.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 0
Console
![Page 17: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/17.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 0
Console*
![Page 18: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/18.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 1
Console*
![Page 19: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/19.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 1
Console*
![Page 20: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/20.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 1
Console**
![Page 21: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/21.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 2
Console**
![Page 22: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/22.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 2
Console**
![Page 23: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/23.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 2
Console***
![Page 24: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/24.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 3
Console***
![Page 25: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/25.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = 3
Console***
![Page 26: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/26.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 0j = DOES NOT EXIST
Console***
![Page 27: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/27.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 1j = DOES NOT EXIST
Console***
![Page 28: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/28.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 1j = 0
Console***
![Page 29: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/29.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 1j = 0
Console***
![Page 30: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/30.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = 1j = 0
Console****
![Page 31: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/31.jpg)
A Few Steps Later
![Page 32: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/32.jpg)
for(int i=0;i<length;i++){
for(int j=0;j<width;j++){
System.out.print(“*”);}System.out.println();
}
Variable Valueslength = 2width = 3;i = DOES NOT EXISTj = DOES NOT EXIST
Console******
![Page 33: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/33.jpg)
• While– Body runs 0 to many times– Great for “ask-before-iterating”
• Do-While– Body runs 1 to many times– Great for “ask-before-iterating”
• For-Loop– Body runs a countable number of times– Great for “count-controlled” situations
![Page 34: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/34.jpg)
Break Exampleint a = 0;while(true){
if(a >= 5){
break;}System.out.print(a+“ ”);a++;
}
//Output: 0 1 2 3 4
• The statement “break” immediately stops a loop.
• Once the break statement is reached the it will run the next statements outside the body of the loop.– “Jumps out of the loop”
• For nested loops it stops the loop whose body the break statement is found.
![Page 35: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/35.jpg)
Continue Exampleint a = 0;while(a<10){
a++;if(a%2==0){
continue;}System.out.print(a+ “ ”);
}//Output: 1 3 5 7 9
• The statement “continue” stops the current loop iterations and starts a new one.
• Once the continue statement is reached it immediately starts the loop again.– “Jumps back to the start of the loop and
continues”
• For nested loops it continues the loop whose body the continue statement is found.
![Page 36: Loops - cse.sc.edu](https://reader031.vdocument.in/reader031/viewer/2022020916/61ac932f90385f2df4452c04/html5/thumbnails/36.jpg)
Sentinel Value Example//Find the average of positive valuesint value = 0;int sum = 0;int count = 0;while(true){
value = keyboard.nextInt();if(value < 0)//The sentinel values are negative{
break;}sum += value;count++;
}int average = sum / count;System.out.println(average);//If the input was 2 4 6 8 10 -1 then it would output 6
• Special value(s) used signal the end of an algorithm.
• We can use these to stop loops.• Sentinel values should be selected so that
they are distinct from other valid values.