Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning Plan 4 Looping.

Similar presentations


Presentation on theme: "Learning Plan 4 Looping."— Presentation transcript:

1 Learning Plan 4 Looping

2 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

3 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

4 Infinite Loop

5 While Loop Example

6 While Loop Problem Example

7 Using Shortcut Arithmetic Operators
loopCount = 1; While(loopCount <3) { System.out.println(“Hello); loopCount = loopCount + 1; } ******************************* loopCount ++ // means to add 1 – called incrementing

8 Shortcuts - continued loopCount += 1; // also means to add 1 loopCount ==; // means to subtract 1

9 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

10 Indefinite While Loop Example
Double bankBalance = ; 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”);

11 Another Example Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password != ) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”);

12 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

13 For Loops For (int x =1; x<11; x++) { System.out.println(x); } ***************** int x =1; While(x<11) x ++

14 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

15 Int password=Integer. parseInt(JOptionPane
Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password != ) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”); ************** Do While (password != ); JOptionPane.showMessageDialog(null, “Welcome to the site!”);

16 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???

17 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); }

18


Download ppt "Learning Plan 4 Looping."

Similar presentations


Ads by Google