for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf ·...
Transcript of for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf ·...
![Page 1: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/1.jpg)
for LoopsCOMP163
![Page 2: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/2.jpg)
“They have computers, and they may
have other weapons of mass destruction.”
Janet Reno
![Page 3: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/3.jpg)
Programming Assignment
• This week’s programming assignment requires reading from a file with about 16,000 lines of data
• The data comes from the National Oceanic and Atmospheric Administration (NOAA)
• Due (as always) on Friday by midnight
![Page 4: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/4.jpg)
Scanner with Files
• When you create an object of the Scanner class, you can specify a File object instead of System.in
java.io.File bat = new java.io.File(“numbers.txt");
java.util.Scanner bird = new java.util.Scanner( bat );
// read a number from the file
double bug = bird.nextDouble();
![Page 5: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/5.jpg)
Useful Scanner Methods
• nextInt() – read an int
• nextDouble() – read a double
• nextLine() – read the whole line as a String
• next() – read the next word as a String
• hasNext() – true if there is more data
• hasNextInt() – true if there is another int
• hasNextDouble() – true if there is another double
• close() – close the file when done
![Page 6: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/6.jpg)
Using the Wrong Next
1 2
7 9
1 3
java.io.File badger = new java.io.File("stuff.txt");java.util.Scanner mouse = new java.util.Scanner( badger);int rat = 0, vole;while ( mouse.hasNext() ) {
vole = mouse.nextInt();rat += vole;
}mouse.close();System.out.println( rat );
Data.txt showing spaces
![Page 7: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/7.jpg)
Using the Proper Next
1 2
7 9
1 3
java.io.File badger = new java.io.File("stuff.txt");java.util.Scanner mouse = new java.util.Scanner( badger);int rat = 0, vole;while ( mouse.hasNextInt() ) {
vole = mouse.nextInt();rat += vole;
}mouse.close();System.out.println( rat );
Data.txt showing spaces
![Page 8: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/8.jpg)
Scanner File Exceptions
• When you create a Scanner object using a File object, it could throw an exception
• Your program must handle the possible exception
• The easiest way to do this is to throw the exception
public static void main( String[] horse ) throwsjava.io.IOException {
![Page 9: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/9.jpg)
Data You Don’t Want
• Sometimes the data file has more data than you want
• If there are five numbers on a line and you only want numbers 2 and 4, it may be easiest to read all five numbers and ignore the ones you don’t need
• The Scanner method nextLine() will read the rest of the line
![Page 10: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/10.jpg)
Skipping Data
• Imagine a data file looks like:
92 3/15/2009 14.5 Fred Smith
47 7/4/1953 3.75 Mary Jones
32 4/1/2013 42.1 Mike J. Snodgrass
• You need the first and third numbers on a lineint num1 = infile.nextInt();
infile.next();
double num2 = infile.nextDouble();
infile.nextLine();
![Page 11: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/11.jpg)
hasNext() with the Keyboard
• You can use the hasNext methods when reading from the keyboard
Scanner keyboard = new Scanner( System.in );
int sum = 0;
while (keyboard.hasNextInt()) {
sum += keyboard.nextInt();
}
• If you enter 5 7 XYZ the program will read only the two numbers
![Page 12: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/12.jpg)
Reading from the Web
• In Java you can read a file from a web server in almost the same way you read a file
• You need to create a java.net.URL object instead of a java.io.File object
• Instead of passing the File object to Scanner use openStream() method of a URL object
• You have to throw java.io.IOException
![Page 13: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/13.jpg)
/* Example reading a web file */
public class ReadWeb {
public static void main(String[] rat) throws
java.io.IOException {
java.util.Scanner keyboard = new
java.util.Scanner(System.in);
System.out.print("Enter the URL >");
String filename = keyboard.next();
java.net.URL webFile = new java.net.URL(filename);
java.util.Scanner fileIn = new
java.util.Scanner(webFile.openStream());
String line;
while ( fileIn.hasNext() ) {
line = fileIn.nextLine();
System.out.println( line );
}
fileIn.close();
![Page 14: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/14.jpg)
You should close a file
A. After each read
B. After you have read everything from it
C. At the very end of the program
D. Don’t bother, the system will close it
![Page 15: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/15.jpg)
Looping Structures
• Java provides three different ways to create a loop
while loops
do while loops
for loops
• All of them cause the program to repeat a set of statements
![Page 16: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/16.jpg)
While Loop Format
• A while loop has a loop condition and a body
• The body is repeated while the loop condition is true
while (loop condition ) {
// loop body
}
![Page 17: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/17.jpg)
while Loop
double sum = 0.0, term = 4.0;double counter = 1.0, sign = 1.0;while ( term > 0.0001) {
term = 4.0 / counter;sum += sign * term;sign = -1.0 * sign;counter = counter + 2.0;
}System.out.println( "Pi is " + sum );
![Page 18: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/18.jpg)
What is displayed?
A. -4
B. -1
C. 0
D. 1
E. 5
int dog = 5, cat = 1;while (dog > 0) {
dog = dog – cat;cat++;
}System.out.println( dog );
![Page 19: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/19.jpg)
do while Syntax
• When a do while loop gets to the end, it checks the logical expression. If true, it repeats the loop
do {
// loop body
} while (logical expression);
![Page 20: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/20.jpg)
do while Loop
double sum = 0.0, term;double counter = 1.0, sign = 1.0;do {
term = 4.0 / counter;sum += sign * term;sign = -1.0 * sign;counter = counter + 2.0;
} while ( term > 0.0001);System.out.println("Pi is " + sum );
![Page 21: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/21.jpg)
Always Once• A do while loop is always executed at least
once
int cow = 5, bull = 7;
while (cow < 4 ) {
cow = cow * cow; // never executed
}
do {
bull = bull* bull; // executed once
} while( bull < 4 );
![Page 22: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/22.jpg)
What is displayed?
A. -4
B. -1
C. 0
D. 1
E. 5
int dog = 5, cat = 1;do {
dog = cat - dog;cat++;
} while (dog > 0);System.out.println( dog );
![Page 23: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/23.jpg)
Input Validation
• Sometimes an if and loop must test the same thing
do {
System.out.print(“Enter the GPA >”);
gpa = keyboard.nextDouble();
if (gpa > 4.0) {
System.out.println(“GPA cannot be more than 4.0”);
}
} while (gpa > 4.0);
![Page 24: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/24.jpg)
Input Validation
• Using a boolean to avoid second comparison
boolean problem;do {
System.out.print(“Enter the GPA >”);gpa = keyboard.nextDouble();problem = false;if (gpa > 4.0) {
System.out.println(“GPA cannot be more than 4.0”);problem = true;
}} while (problem);
![Page 25: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/25.jpg)
Reading a File with do whilepublic class FileDo { // average numbers in a file
public static void main(String[] unused)
throws java.io.IOException {
java.io.File dog = new java.io.File("numbers.txt");
java.util.Scanner cat = new java.util.Scanner(dog);
double sum = 0.0; // sum of values in file
int count = 0; // number of values in file
do {
sum += cat.nextDouble();
count++;
} while ( cat.hasNextDouble() );
cat.close();
System.out.println( sum / count );
}
}
![Page 26: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/26.jpg)
Increment and Decrement Operator
• You can increment or decrement an integer by putting ++ or -- after the variable
cow++; bull--;
• This is the same as
cow = cow + 1; bull = bull – 1;
• Example:
int bull = 5;
bull++;
System.out.println( bull ); // shows 6
![Page 27: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/27.jpg)
Java for the Common Loop
• Java has the for loop that is very useful for loops that repeat a fixed number of times
• The for loop is equivalent to a while loop with the initialization and counter
![Page 28: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/28.jpg)
for Loop
• The for loop has three parts separated by semicolons
➢ initialization – Done once before the loop
➢ test – Loop executed when test is true
➢ update – Done at the end of the loop before test
for ( initialization; test ; update ) {
// do something
}
![Page 29: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/29.jpg)
Print the numbers 1 to 10 and squares
int dog, cat;
for (dog = 1; dog <= 10; dog++) {
cat = dog * dog;
System.out.println(dog+" "+cat);
}
![Page 30: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/30.jpg)
for and while loop equivalence
for ( initialization ; test; update ) {statements to repeat
}
is the same as
initialization;
while (test) {
statements to repeat
update;
}
![Page 31: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/31.jpg)
do while version
• To read 7 integers with a do while loop
Scanner keys = new Scanner(System.in);
int sum = 0, count;
count = 0;
do {
sum = sum + keys.nextInteger();
count++;
} while(count < 7);
![Page 32: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/32.jpg)
while version
• To read 7 integers with a while loop
Scanner keys = new Scanner(System.in);
int sum = 0, count;
count = 0;
while(count < 7){
sum = sum + keys.nextInteger();
count++;
}
![Page 33: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/33.jpg)
for Example
• To read 7 integers with a for loop
Scanner keys = new Scanner(System.in);
int sum = 0, count;
for (count = 0; count < 7; count++){
sum = sum + keys.nextInteger();
}
![Page 34: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/34.jpg)
What is displayed?
A. 2
B. 3
C. 5
D. 8
E. 12
int elk, moose = 3;for ( elk = 2; elk < 4; elk++ ) {
moose += elk;}System.out.println(moose);
![Page 35: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/35.jpg)
Write this algorithm in Java with your team
• num1 = 1 and num2 = 1
• Repeat 5 times
➢ print num2
➢ set temp to num1 + num2
➢ set num1 to num2
➢ set num2 to temp
![Page 36: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/36.jpg)
Possible Solution
int num1 = 1, num2 = 1, temp, looper;
for ( looper = 0; looper < 5; looper++) {
System.out.println( num2 );
temp = num1 + num2;
num1 = num2;
num2 = temp;
}
![Page 37: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/37.jpg)
Declaring the Loop Counter
• In a for loop, you can declare the variable that is used as the loop counter
• The variable can only be used in the loop
for (int cow = 0; cow < 28; cow++) {
// loop body
// the loop counter, cow, can be used here
}
// You cannot use cow after the loop
![Page 38: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/38.jpg)
What does this print?
A. 0
B. 1
C. 3
D. 4
E. none of the above
int dog = 1;for (int cat = 0; cat < 3; cat++) {
dog = dog + cat;}System.out.print( dog );
![Page 39: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/39.jpg)
What does this print?
A. 0
B. 1
C. 3
D. 4
E. none of the above
int dog = 1;for (int cat = 0; cat < 3; cat++) {
dog = dog + cat;}System.out.print( cat );
![Page 40: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/40.jpg)
Unusual for Loop Example
int sum = 0;for (int num = keyboard.nextInt(); num > 0 ;
num = keyboard.nextInt() ) { sum = sum + num;
}-------------------------- same as --------------------------int num = keyboard.nextInt();while (num > 0) {
sum = sum + num;num = keyboard.nextInt() ) {
}
![Page 41: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/41.jpg)
Classical for Loop
• A for loop is very often used for loop a specified number of times
for (int ayeaye = 0; ayeaye < loopCount; ayeaye++) {
// This part is repeated loopCount times
}
![Page 42: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/42.jpg)
Counting Down
• Sometimes you want to count down from a number
for (int ayeaye = loopCount; ayeaye >0; ayeaye--) {
// This part is repeated loopCount times
}
![Page 43: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/43.jpg)
Which will loop 8 times?
A. for (int i = 0; i < 8; i++)
B. for (int i = 0; i <= 8; i++)
C. for (int i = 1; i < 8; i++)
D. for (int i = 0; i <= 9; i++)
![Page 44: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/44.jpg)
Loops in Loops
• Sometimes a program will need one loop in the body of another loop
int sum = 0, cat = 5, dog = 3;
for (int i = 0; i < cat; i++) {
for (int k = 0; k < dog; k++) {
sum += i * k;
}
}
• Note that the loop counter variables (i and k) must be different
![Page 45: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/45.jpg)
Long Running Programs
• If a loop repeats n times is inside a loop that repeats m times, the total number of iterations of the loop is m * n
• If n and m are large, it may take a very long time for the program to complete
• Loops inside loops inside loops inside loops can take a long time to run
![Page 46: for loops - North Carolina A&T State Universitywilliams.comp.ncat.edu/COMP163/forLoops.pdf · whileloops do whileloops forloops •All of them cause the program to repeat a set of](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec476a795fe5563720f5dab/html5/thumbnails/46.jpg)
What does this program display?
int count = 0;
for (int i = 0; i < 2; i++) {
for (int k = 0; k < 3; k++) {
count = count + 1;
}
}
System.out.println( count );
A. 1B. 2C. 3D. 5E. 6