Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.

Similar presentations


Presentation on theme: "Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure."— Presentation transcript:

1

2 Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.

3 Conditional Statement Definition A conditional statement is a program expression that evaluates to true or false. Most conditional statements require a relational operator. All conditions must be placed inside (parentheses).

4 Relational Operators NameOperatorExpressionEvaluates Equals == 5 == 5 K == 10 true depends Not Equals != 50 != 25 100 != 100 true false Less than < 100 < 200 P < Q true depends Greater than > 100 > 200 P > Q false depends Less than or equals <= 25 <= 26 25 <= 25 true Greater than or equals >= 1000 >= 1000 K >= (P + Q) true depends

5

6 One-Way Selection Syntax One-Way selection general syntax: if (condition true) execute program statement if (counter > 100) System.out.println("Counter exceeds 100"); Use braces { } and block structure to control multiple program statements. if (cavings >= 10000) { System.out.println("It’s skiing time"); System.out.println("Let’s pack"); System.out.println("Remember your skis"); }

7 Indentation Rule: Java syntax uses freeform program style. Program statements may be placed on multiple lines with or without indentation. By convention, control structures and their conditional statements are placed on one line. The program statement that is executed, if the condition is true, is placed on the next line, and indented below the conditional statement. if(sales >= 500000) bonus = 1000; if(sales >=500000) bonus = 1000;

8 Two-Way Selection Syntax Two-Way selection general syntax: if (condition true) execute first program statement else // when condition is false execute second program statement if (gpa >= 90.0) System.out.println ( "You’re an honor graduate"); else System.out.println ("You’re not an honor graduate");

9 Multiple-Way Selection Program Statement Condition True False Program StatementCondition True False Program StatementCondition True False

10 Multiple-Way Selection Syntax General Syntax: switch(selectionVariable) { case selectionConstant: program statement; break; default : program statement; } The default statement is used to handle the situation when a proper match is not found. Frequently an error message is used to indicate that no match was found.

11 Multiple-Way Selection Block-Structure Syntax NO-NO Example The extra braces accomplish nothing and even prevent the break commands from working. switch(courseGrade) { case ’A’:{ points = 4; System.out.println(“Excellent!”); break; } case ’B’:{ points = 3; System.out.println(“Good”); break; } case ’C’:{ points = 2; System.out.println(“Fair”); break; } case ’D’:{ points = 1; System.out.println(“Poor”); break; } case ’F’:{ points = 0; System.out.println(“Bad”); break; } default : points = 0; }

12 Multiple-Way Selection Block-Structure Syntax Yes-Yes Example The switch statement is the only control structure that does not use braces for block structure. The break command at the end of each case is all it needs to keep each case separate. switch(courseGrade) { case ’A’:points = 4; System.out.println(“Excellent!”); break; case ’B’:points = 3; System.out.println(“Good”); break; case ’C’:points = 2; System.out.println(“Fair”); break; case ’D’:points = 1; System.out.println(“Poor”); break; case ’F’:points = 0; System.out.println(“Bad”); break; default : points = 0; }

13

14 // Java0501.java // This program (and the next few programs) demonstrate // user keyboard input during program execution. // This particular program demonstrates keyboard input of // a String in a text window using the Scanner class. public class Java0501 { public static void main (String args[]) { System.out.println("\nJAVA0501.JAVA\n"); System.out.print("Enter name ===>> "); // Line 1 Scanner input = new Scanner(System.in); // Line 2 String name = input.nextLine(); // Line 3 System.out.println("\nName Entered: " + name); System.out.println(); }//end of main }//end of program Line 1 is called the prompt. It asks or “prompts” the user for information. Line 2 is the line that actually scans the screen until the Enter key is hit. Line 3 is the line that stores the data entered on the screen in a variable.

15 // Java0502.java // This program demonstrates how to use nextLine( ) method of the Scanner class // for three separate String keyboard inputs. public class Java0502 { public static void main (String args[]) { System.out.print("Enter Line 1 ===>> "); Scanner input1 = new Scanner(System.in); String firstName = input1.nextLine(); System.out.print("Enter Line 2 ===>> "); Scanner input2 = new Scanner(System.in); String midName = input2.nextLine(); System.out.print("Enter Line 3 ===>> "); Scanner input3 = new Scanner(System.in); String lastName = input3.nextLine(); System.out.println(); System.out.println(firstName); System.out.println(midName); System.out.println(lastName); System.out.println(); }//end of main } //end of program

16 // Java0503.java // This program demonstrates objects concatenation with // keyboard entered data. public class Java0503 { public static void main (String args[]) { System.out.println("\nJAVA0503.JAVA\n"); System.out.print("Enter 1st Number ===>> "); Scanner input1 = new Scanner(System.in); String number1 = input1.nextLine(); System.out.print("Enter 2nd Number ===>> "); Scanner input2 = new Scanner(System.in); String number2 = input2.nextLine(); String sum = number1 + number2; System.out.println(); System.out.println(number1 + " + " + number2 + " = " + sum); System.out.println(); }//end of main }//end of program

17 Addition vs. Concatenation The problem with the previous program is that String data type was used, instead of int or double. When the plus sign ( + ) is used with a numerical value, like an int or a double, it performs addition. However, when the plus sign is used with two String variables, it joins the String s together. This is called String Concatenation.

18 // Java0504.java // This program uses the nextInt() method of the Scanner class to enter // integers from the keyboard. It is now possible to correctly add the two // numbers. public class Java0504 { public static void main (String args[]) { System.out.println("\nJAVA0504.JAVA\n"); System.out.print("Enter 1st Number ===>> "); Scanner input1 = new Scanner(System.in); int number1 = input1.nextInt(); System.out.print("Enter 2nd Number ===>> "); Scanner input2 = new Scanner(System.in); int number2 = input2.nextInt(); int sum = number1 + number2; System.out.println(); System.out.println(number1 + " + " + number2 + " = " + sum); System.out.println(); }//end of main }//end of program

19 // Java0505.java // This program demonstrates how to use nextDouble( ) method of the Scanner class // for three separate double keyboard inputs, which are used to display the mean. public class Java0505 { public static void main (String args[]) { System.out.println("\nJAVA0505.JAVA\n"); System.out.print("Enter Number 1 ===>> "); Scanner input1 = new Scanner(System.in); double n1 = input1.nextDouble(); System.out.print("Enter Number 2 ===>> "); Scanner input2 = new Scanner(System.in); double n2 = input2.nextDouble(); System.out.print("Enter Number 3 ===>> "); Scanner input3 = new Scanner(System.in); double n3 = input3.nextDouble(); System.out.println(); System.out.println(n1); System.out.println(n2); System.out.println(n3); double mean = (n1 + n2 + n3) / 3; System.out.println(); System.out.println("The mean is " + mean); System.out.println(); }//end of main }//end of program

20 // Java0507.java // This program demonstrates how to use GUI input methods. // It also shows a GUI display. public class Java0507 { public static void main (String args[]) { String firstName = JOptionPane.showInputDialog("Enter Student First Name."); String lastName = JOptionPane.showInputDialog("Enter Student Last Name."); String agePrompt = JOptionPane.showInputDialog(“Enter Student Age.”; int age = Integer.parseInt(agePrompt); String gpaPrompt = JOptionPane.showInputDialog(“Enter Student GPA.”; double gpa = Double.parseDouble(gpaPrompt); String sentence = firstName + " " + lastName +" is " + age + " years old, has a GPA of " + gpa; JOptionPane.showMessageDialog(null, sentence); }//end of main }//end of program

21

22 Keyboard Input Methods The following keyboard input methods are from the Scanner class: 1.nextInt() is used to enter an int from the text screen. 2.nextDouble() is used to enter a double from the text screen. 3.nextLine() is used to enter a String from the text screen. The following keyboard input methods are used in conjunction with the showInputDialog & showMessageDialog methods of the JOptionPane class, to enter data from a GUI (pop-up) window: 1.Double.parseDouble() is used to enter a double from a GUI window. 2.Integer.parseInt() is used to enter an int from a GUI window.

23 // Java0511.java // This program demonstrates two-way selection with. // Run the program twice: First with 1200, then with 1000. public class Java0511 { public static void main (String args[]) { System.out.println("\nJAVA0511.JAVA\n"); System.out.print("Enter SAT ===>> "); Scanner input = new Scanner(System.in); int sat = input.nextInt(); System.out.println(); if (sat >= 1100) System.out.println("You are admitted"); else System.out.println("You are not admitted"); System.out.println(); }//end of main } //end of program

24 // Java0512.java // This program demonstrates two-way selection with. // Multiple statements require the use of block structure. // Run the program twice: First with 1100, then with 1099. public class Java0512 { public static void main (String args[]) { System.out.println("\nJAVA0512.JAVA\n"); System.out.print("Enter SAT ===>> "); Scanner input = new Scanner(System.in); int sat = input.nextInt(); System.out.println(); if (sat >= 1100) { System.out.println("You are admitted"); System.out.println("Orientation will start in June"); } else { System.out.println("You are not admitted"); System.out.println("Please try again when your SAT improves."); } System.out.println(); }//end of main }//end of program

25 // Java0513.java // This program demonstrates multi-way selection with and. // This program compiles, but displays illogical output. public class Java0513 { public static void main (String args[]) { System.out.println("\nJAVA0513.JAVA\n"); System.out.print("Enter Letter Grade ===>> "); Scanner input = new Scanner(System.in); char grade = input.nextChar(); System.out.println(); switch (grade) { case 'A' : System.out.println("90.. 100 Average"); case 'B' : System.out.println("80.. 89 Average"); case 'C' : System.out.println("70.. 79 Average"); case 'D' : System.out.println("60.. 69 Average"); case 'F' : System.out.println("Below 60 Average"); } System.out.println(); }//end of main }//end of program

26 default is useful for input protection. Note that Java is case-sensitive.


Download ppt "Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure."

Similar presentations


Ads by Google