Learning Plan 4 Looping
Loop Structure Decision-making makes programs seem smart Looping makes programs powerful A loop – repeated execution of a block of statements Like if statements, a boolean expression is evaluated If it’s true, a block of statements called the loop body executes and boolean expression evaluated again As long as expression is true, the loop body continues to execute Three types of loops – while, for , and do/while
While Loops Can be used to execute a specified number of times Need to use a counter variable (called a loop control variable Counter variable needs to be incremented/decremented Or the number of times it executes might not be determined until the program is running Called an indefinite While loop
Infinite Loop
While Loop Example
While Loop Problem Example
Using Shortcut Arithmetic Operators loopCount = 1; While(loopCount <3) { System.out.println(“Hello); loopCount = loopCount + 1; } ******************************* loopCount ++ // means to add 1 – called incrementing
Shortcuts - continued loopCount += 1; // also means to add 1 loopCount ==; // means to subtract 1
Indefinite While Loop Often the value of a loop control variable is not altered by adding or subtracting from it, but instead altered by user input Called indefinite because you don’t know how many times it will eventually loop EX: you want to continue performing some task as long as the user indicates a desire to continue You won’t know if the loop will eventually be executed two times or 200 times or not at all
Indefinite While Loop Example Double bankBalance = 1276.23; Int userSelection = Integer.parseInt(JOptionPane.showInputDialog(null, “Show Bank Balance (1 for YES, 2 for NO)”)); While (userSelection == 1) { JOptionPane.showMessageDialog(null, “Available balance is “ + bankBalance); userSelection = Integer.parseInt(JOptionPane.showInputDialog (null, “Show Bank Balance (1 for YES, 2 for NO)”)); } JOptionPane.showMessageDialog(null, “Thanks for using the program”);
Another Example Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password !=12345678) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”);
For Loops Can accomplish the same thing that a while loop can, but typically used only when you have a counter-controlled loop Same as While, but shorter code. Both do the same, but in different order/code Initialize the loop control variable Test the loop control variable Update the loop control variable
For Loops For (int x =1; x<11; x++) { System.out.println(x); } ***************** int x =1; While(x<11) x ++
Do/While Loops All examples thus far showed that the loop body might execute many times However, it is possible that the loop will not execute at all Conditional statement is checked BEFORE the loop body The do/while has a conditional statement AT THE END of the loop body, guaranteeing the loop occurs at least once
Int password=Integer. parseInt(JOptionPane Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password !=12345678) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”); ************** Do While (password !=12345678); JOptionPane.showMessageDialog(null, “Welcome to the site!”);
Nested Loops Just as if statements can be nested, so can loops For (int x =0; x<4; x++) { for (int y =0; y<4; y++) System.out.println(x + “, “ + y); } Can you guess what output will be???
0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3 2, 0 2, 1 2, 2 2, 3 3, 0 3, 1 3, 2 3, 3 For (int x =0; x<4; x++) { for (int y =0; y<4; y++) System.out.println(x + “, “ + y); }