Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statement Examples

Similar presentations


Presentation on theme: "Control Statement Examples"— Presentation transcript:

1 Control Statement Examples
GC 201

2 With a block of statements –
The if Statement With a block of statements – Write a program that identifies the students who didn’t pass the course. The program should also print an appropriate message. INPUT Student’s score (variable: score, type: double) OUTPUT Letter grade = ‘F’ (variable: grade, type: char) Print an appropriate message Start grade = ‘X’ PROCESS if (score <60) 1) grade = ‘F’ 2) Print “Failed” Read score FLOWCHART True score < 60? NOTE THAT grade = ‘F’ False If score >= 60, grade will have no value to be printed. Therefore, we give an initial value to grade. Print “Failed” Print grade End

3 With a block of statements - CODE
The if Statement With a block of statements - CODE // import necessary libraries import java.util.*; //contains the class Scanner public class ifStatementN { // instantiate the object console from the class Scanner static Scanner console = new Scanner (System.in); public static void main (String[] args) // Declaration section: to declare needed variables double score; char grade = ‘X’; //initialize grade // Input section: to enter values of used variables System.out.println (“Enter student’s score”); //prompt score = console.nextDouble(); // Processing section: processing statements if (score < 60.0) // score is double grade = ‘F’; System.out.println (“Failed”); } //end if(score < 60.0) // Output section: display program output System.out.printf (“Student’s Grade = %3c“, grade); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

4 With a block of statements - PROGRAM : CASE 1 (TRUE)
The if Statement With a block of statements - PROGRAM : CASE 1 (TRUE) double score; char grade = ‘X’; //initialize grade 10 11 ??? score X grade System.out.println (“Enter student’s score”); //prompt score = console.nextDouble(); 13 14 Enter student’s score 50.5_ 1 2 50.5 score X grade

5 With a block of statements - PROGRAM : CASE 1 (TRUE)
The if Statement With a block of statements - PROGRAM : CASE 1 (TRUE) if (score < 60.0) // score is double { grade = ‘F’; System.out.println (“Failed”); //output line 3 } //end if (score < 60.0) 16 17 18 19 20 Enter student’s score 50.5 Failed_ 1 2 3 50.5 score F grade System.out.printf (“Student’s Grade = %3c“, grade); //output line 4 22 Enter student’s score 50.5 Failed Student’s Grade = ~~F_ 1 2 3 4 50.5 score F grade

6 With a block of statements: PROGRAM : CASE 2 (FALSE)
The if Statement With a block of statements: PROGRAM : CASE 2 (FALSE) double score; char grade = ‘X’; //initialize grade 10 11 ??? score X grade System.out.println (“Enter student’s score”); //prompt score = console.nextDouble(); 13 14 Enter student’s score 85_ 1 2 85.0 score X grade

7 With a block of statements - PROGRAM : CASE 2 (FALSE)
The if Statement With a block of statements - PROGRAM : CASE 2 (FALSE) if (score < 60.0) // score is double { grade = ‘F’; System.out.println (“Failed”); } //end if (score < 60.0) 16 17 18 19 20 Enter student’s score 85_ 1 2 85.0 score X grade System.out.printf (“Student’s Grade = %3c“, grade); 22 Enter student’s score 85 Student’s Grade = ~~X_ 1 2 3 85.0 score X grade

8 With a single statement – PROGRAM : ANALYSIS
The if…else Statement With a single statement – PROGRAM : ANALYSIS Write a program that calculates the net salary after taxes deduction. If the salary is greater than 5000 SR, taxes are 20% of the salary; otherwise, taxes are 25% of the salary. INPUT Salary (variable: salary, type: double) OUTPUT Net Salary (variable: netSalary, type: double) PROCESS if (salary > 5000) netSalary=0.8*salary; if (salary <= 5000) netSalary=0.75*salary.

9 With a single statement – PROGRAM : Flowchart
The if…else Statement With a single statement – PROGRAM : Flowchart Start Read salary salary > 5000? False True netSalary = 0.8 * salary netSalary = 0.75 * salary Print netSalary End

10 With a single statement - PROGRAM : CODE
The if…else Statement With a single statement - PROGRAM : CODE // import necessary libraries import java.util.*; //contains the class Scanner public class ifElseStatement1 { // instantiate the object console from the class Scanner static Scanner console = new Scanner (System.in); public static void main (String[] args) // Declaration section: to declare needed variables double salary, netSalary; // Input section: to enter values of used variables System.out.println (“Enter employee’s salary”); //prompt salary = console.nextDouble(); // Processing section: processing statements if (salary > ) // salary is double netSalary = 0.8 * salary; else netSalary = 0.75 * salary; // Output section: display program output System.out.printf (“Net Salary = %.2f“, netSalary); //2 digits after decimal point } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

11 With a single statement - PROGRAM : CASE 1 (TRUE)
The if…else Statement With a single statement - PROGRAM : CASE 1 (TRUE) System.out.println (“Enter employee’s salary”); //prompt score = console.nextDouble(); 12 13 Enter employee’s salary 6000_ 1 2 if (salary > ) // salary is double netSalary = 0.8 * salary; //condition is true else netSalary = 0.75 * salary; 15 16 17 18 Enter employee’s salary 6000_ 1 2 System.out.printf (“Net Salary = %.2f“, netSalary); //output line 3 20 Enter employee’s salary 6000 Net Salary = _ 1 2 3

12 With a single statement - PROGRAM : CASE 2 (FALSE)
The if…else Statement With a single statement - PROGRAM : CASE 2 (FALSE) System.out.println (“Enter employee’s salary”); //prompt score = console.nextDouble(); 12 13 Enter employee’s salary 2500.0_ 1 2 if (salary > ) // salary is double netSalary = 0.8 * salary; else netSalary = 0.75 * salary; //condition is false 15 16 17 18 Enter employee’s salary 2500.0_ 1 2 System.out.printf (“Net Salary = %.2f“, netSalary); //output line 3 20 Enter employee’s salary 2500.0 Net Salary = _ 1 2 3

13 The switch Statement EXAMPLE 2
Write a program that displays the following menu, then acts accordingly: Enter your choice: Add two positive numbers Get the double of a positive number Get the square of a number The program should give an error message for invalid inputs.

14 The switch Statement EXAMPLE 2 public static void main (String[] args)
{ //Declaration section Scanner read = new Scanner (System.in); int choice, num1, num2, result = -1; String message = “Invalid input”; //input section //Display menu System.out.println (“Enter your choice:”); System.out.println (“1. Add two positive numbers”); System.out.println (“2. Get the double of a positive number”); System.out.println (“3. Get the square of a number”); choice = read.nextInt(); //processing section 1 2 3 4 5 6 7 8 9 10 11 12 13 14

15 The switch Statement EXAMPLE 2 (cont’d) //processing section
switch (choice) { case 1: System.out.println (“Enter two positive integers”); //prompt num1 = read.nextInt(); num2 = read.nextInt(); result = num1 + num2; // the value of result is no more equal to -1 break; case 2: System.out.println (“Enter a positive integer”); //prompt result = num1 * 2; // the value of result is no more equal to -1 case 3: System.out.println (“Enter an integer”); //prompt result = num1 * num1; // the value of result is no more equal to -1 default: message =“Invalid value of choice”; //no change to “result”  result=-1 } //end switch //output section if (result !=-1) System.out.println (result); //result is modified (!= -1) else System.out.println (message); //result is not modified (equals -1) } //end main 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

16 Without default Statement
EXAMPLE 2 In fact, line 30 in slide 10 adds nothing to the program. Note that the variable message is already initialized to “Invalid input” in line 6. In addition, the default is optional in the switch statement. In other words, it may be omitted from the switch statement if it is useless  Therefore, line 30 may be omitted. The modified “Processing section” of the program is shown in the next slide:

17 Without default Statement
EXAMPLE 2 //processing section switch (choice) { case 1: System.out.println (“Enter two positive integers”); //prompt num1 = read.nextInt(); num2 = read.nextInt(); result = num1 + num2; // the value of result is no more equal to -1 break; case 2: System.out.println (“Enter a positive integer”); //prompt result = num1 * 2; // the value of result is no more equal to -1 case 3: System.out.println (“Enter an integer”); //prompt result = num1 * num1; // the value of result is no more equal to -1 } //end switch //output section if (result !=-1) System.out.println (result); else System.out.println (message); } //end main 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

18 EXAMPLE 2 – Validating user input
Programming Hint (1) EXAMPLE 2 – Validating user input //processing section switch (choice) { case 1: System.out.println (“Enter two positive integers”); //prompt num1 = read.nextInt(); num2 = read.nextInt(); if ((num1 < 0) || (num2 < 0)) //should be made after reading num1 & num2 result = -1; else result = num1 + num2; // the value of result is no more equal to -1 break; case 2: System.out.println (“Enter a positive integer”); //prompt if (num1 < 0) //this should be made after reading num result = num1 * 2; // the value of result is no more equal to -1 case 3: System.out.println (“Enter an integer”); //prompt result = num1 * num1; // the value of result is always positive } //end switch //output section 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

19 EXAMPLE 2 – Validating user input (cont’d)
Programming Hint (1) EXAMPLE 2 – Validating user input (cont’d) //output section if (result !=-1) System.out.println (result); else System.out.println (message); } //end main 37 38 39 40 41 42 Validating user input is a vital issue in programming. The programmer should take into consideration all possible values entered by the user. In the example above, result is used to store the result of the arithmetic operation. It is also used to denote an invalid input. The sources of invalid inputs may be either the variable choice, num1 or num2. Initially, result is initialized to -1 (line 5). This is useful in case the user entered an invalid value for the variable choice. Now, let us reconsider the conditions in lines 20 and 27. In fact, these may be re-written in a smarter way as follows:

20 EXAMPLE 2 - Validating user input (cont’d)
Programming Hint (1) EXAMPLE 2 - Validating user input (cont’d) case 1: System.out.println (“Enter two positive integers”); //prompt num1 = read.nextInt(); num2 = read.nextInt(); if ((num1 >= 0) && (num2 >= 0)) result = num1 + num2; // the value of result is no more equal to -1 break; 17 18 19 20 21 22 case 2: System.out.println (“Enter a positive integer”); //prompt num1 = read.nextInt(); if (num1 >= 0) result = num1 * 2; // the value of result is no more equal to -1 break; 25 26 27 28 29 We were able to omit lines 21, 22 and lines 28, 29 in slide 13 since they add nothing to the logic. However, we had to modify the logical expressions as shown above.

21 EXAMPLE 2 – Reducing Redundancy
Programming Hint (2) EXAMPLE 2 – Reducing Redundancy System.out.println (“Enter a positive integer”); num1 = read.nextInt(); //processing section switch (choice) { case 1: System.out.println (“Enter a positive integer”); //prompt num2 = read.nextInt(); if ((num1 < 0) || (num2 < 0)) //should be made after reading num1 & num2 result = -1; else result = num1 + num2; // the value of result is no more equal to -1 break; case 2: System.out.println (“Enter a positive integer”); //prompt if (num1 < 0) //this should be made after reading num result = num1 * 2; // the value of result is no more equal to -1 case 3: System.out.println (“Enter an integer”); //prompt result = num1 * num1; // the value of result is always positive } //end switch 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

22 EXAMPLE 2 – Final version
Programming Hints EXAMPLE 2 – Final version public static void main (String[] args) { //Declaration section Scanner read = new Scanner (System.in); int choice, num1, num2, result = -1; String message = “Invalid input”; //this may also be declared as a constant //input section //Display menu System.out.println (“Enter your choice:”); System.out.println (“1. Add two positive numbers”); System.out.println (“2. Get the double of a positive number”); System.out.println (“3. Get the square of a number”); choice = read.nextInt(); System.out.println (“Enter a positive integer”); num1 = read.nextInt(); //processing section 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

23 EXAMPLE 2 – Final version (cont’d)
Programming Hint EXAMPLE 2 – Final version (cont’d) //processing section switch (choice) { case 1: System.out.println (“Enter a positive integer”); //prompt num2 = read.nextInt(); if ((num1 >= 0) && (num2 >= 0)) result = num1 + num2; // the value of result is no more equal to -1 break; case 2: if (num1 >= 0) //this should be made after reading num result = num1 * 2; // the value of result is no more equal to -1 case 3: result = num1 * num1; // the value of result is always positive } //end switch //output section if (result != -1) System.out.println (result); else System.out.println (message); } //end main 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

24 Without break Statement
EXAMPLE 4 Using a switch statement, write a program that accepts an integer value from the user ranging between -2 and 2. The program then identifies if the number is negative, zero or positive.

25 Without break Statement
EXAMPLE 4 - SOLUTION public static void main (String[] args) { //Declaration section Scanner read = new Scanner (System.in); int num; //input section System.out.println (“Enter an integer between -2 and +2); //prompt num = read.nextInt(); //processing section switch (num) case -2: case -1: System.out.println (“The number is negative”); break; case 0: System.out.println (“The number is zero”); case 1: case 2: System.out.println (“The number is positive”); default: System.out.println (“Invalid input”); } //end switch } //end main 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25


Download ppt "Control Statement Examples"

Similar presentations


Ads by Google