Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.

Similar presentations


Presentation on theme: "Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already."— Presentation transcript:

1 Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already seen two of these.

2 Lots of Choices import cs1.Keyboard; public class Testing { public static void main( String [] args) { System.out.println("Enter an integer between 1 and 5:"); int num = Keyboard.readInt(); if (num == 1) System.out.println("It's a one"); else if (num == 2) System.out.println("It's a two"); else if (num == 3) System.out.println("It's a three"); else if (num == 4) System.out.println("It's a four"); else if (num == 5) System.out.println("It's a five"); else System.out.println("Number not in range"); }

3 The Switch Statement switch(num) { case 1: System.out.println("It's a one"); break; case 2: System.out.println("It's a two"); break; case 3: System.out.println("It's a three"); break; case 4: System.out.println("It's a four"); break; case 5: System.out.println("It's a five"); break; default: System.out.println("Number not in range"); }

4 Repetition---the while loop public static void main( String [] args) { System.out.println("Enter an integer number between 1 and 5:"); int num = Keyboard.readInt(); while(num 5) { System.out.println("Number not in range---try again"); num = Keyboard.readInt(); } System.out.print("\nThanks, the number "); if (num == 1) System.out.print("one"); else if (num == 2) System.out.print("two"); else if (num == 3) System.out.print("three"); else if (num == 4) System.out.print("four"); else System.out.print("five"); System.out.println(" is a great choice"); }

5 The while Loop The while loop in Java is a pre-test loop. The test of the condition is done at the beginning of the loop. The loop continues as long as the condition is true. As soon as the condition becomes false, the loop stops and the next statement is executed. If the condition is false the first time it is tested, the loop is never executed at all.

6 Infinite Loops The loop control variable needs to be initialized at the beginning of the loop, and tested each time the loop is executed. Somewhere in the loop there must be something that can change the value of the loop control variable. If there is not, we have an infinite loop which will continue going round and round until you stop it from outside. If this happens in BlueJ, close the BlueJ window and start it up again. Then go to the editor and fix the problem.

7 7 Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it has one operand), but logical AND and logical OR are binary operators (they each have two operands)

8 Getting a Menu Choice public int getMenuChoice() { int choice = 0; while (choice 4) { System.out.println("\n\nChoose one from the menu below"); System.out.println("1. Yes"); System.out.println("2. No"); System.out.println("3. Maybe"); System.out.println("4. Exit"); int choice = Keyboard.readInt(); if (choice 4) System.out.println("Choose a number between 1 and 4"); } return choice; }

9 9 Increment and Decrement Operators The increment and decrement operators are arithmetic and operate on one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is essentially equivalent to count = count + 1;

10 10 Increment and Decrement Operators When used in a larger expression, the prefix and postfix forms have a different effect In both cases the variable is incremented (decremented) But the value used in the larger expression depends on the form: Expression count++ ++count count-- --count Operation add 1 subtract 1 Value of Expression old value new value old value new value

11 11 Assignment Operators There are many assignment operators, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

12 Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write the program using a do loop you could also write the program using a for loop

13 A simple while loop example public class Sum { public static void main( String [] args) { final int MIN = 1, MAX = 100; int count = MIN, sum = 0; while (count <= MAX) { sum += count; count++; } System.out.println("the sum of the number between " + MIN + " and " + MAX + " is " + sum); }

14 Equivalent for loop public class Sum { public static void main( String [] args) { final int MAX = 100, MIN = 1; int sum = 0; for (int count = MIN; count <= MAX; count++) { sum += count; } System.out.println("the sum of the number between " + MIN + " and " + MAX + " is " + sum); }

15 A do loop example import cs1.Keyboard; public class Sum { public static void main( String [] args) { int min, max, sum = 0; do { System.out.print(“enter limits for the sum"); System.out.println(" both must > 0:"); min=Keyboard.readInt(); max=Keyboard.readInt(); } while (min <= 0 || max < =0); for (int count = min; count <= max; count++) sum += count; }

16 Choosing a looping structure --- “rule of thumb” guidelines Use a do loop or a while loop when the number of iterations can not be known in advance Use a for loop for “counting” Use a do loop when the body of the loop must be executed before evaluating the “loop test”

17 A silly game version 1 import cs1.Keyboard; public class Guess { public static void main( String [] args) { final int MYNUMBER = 3; System.out.print("guess a number between 1 & 10: "); int guess = Keyboard.readInt(); if (guess == MYNUMBER) System.out.println("You got it!!!"); else System.out.println("Sorry, that's wrong"); }

18 A silly game version 2 import cs1.Keyboard; public class Guess { public static void main( String [] args) { final int MYNUMBER = 3; int guess; do { System.out.print("guess # between 1 & 10: "); guess = Keyboard.readInt(); if (guess == MYNUMBER) System.out.println("You got it!!!"); else System.out.println("Sorry, that's wong"); } while (guess != MYNUMBER); }

19 A silly game version 3 Add a loop to repeat the game


Download ppt "Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already."

Similar presentations


Ads by Google