Control Statement Examples

Slides:



Advertisements
Similar presentations
Logic & program control part 3: Compound selection structures.
Advertisements

Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computer Programming Decisions If/Else Booleans.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 4 Selection Structures: Making Decisions.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 4: Control Structures II
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Control Statements IT108 George Mason University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CompSci 230 S Programming Techniques
Decision making If.. else statement.
Java Fundamentals 4.
CSC111 Quick Revision.
Selection (also known as Branching) Jumail Bin Taliba by
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Introduction to programming in java
Chapter 5: Control Structures II
The Selection Structure
Chapter 4: Control Structures
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
User input We’ve seen how to use the standard output buffer
Chapter 4: Making Decisions.
Repetition-Counter control Loop
Conditionals & Boolean Expressions
Repetition-Sentinel,Flag Loop/Do_While
Chapter 4: Making Decisions.
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
OUTPUT STATEMENTS GC 201.
INPUT STATEMENTS GC 201.
Building Java Programs
IDENTIFIERS CSC 111.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Compound Assignment Operators in C++
SELECTION STATEMENTS (2)
1) C program development 2) Selection structure
AP Java Review If else.
Decision making If statement.
Introduction to Computer Programming
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Module 3 Selection Structures 2/19/2019 CSE 1321 Module 3.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 3: Selection Structures: Making Decisions
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Building Java Programs
Building Java Programs
HNDIT11034 More Operators.
Selection Control Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structures.
Presentation transcript:

Control Statement Examples GC 201

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

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

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

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

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

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

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.

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

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 > 5000.0) // 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

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 > 5000.0) // 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 = 4800.00_ 1 2 3

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 > 5000.0) // 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 = 1875.00_ 1 2 3

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.

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

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

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:

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

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

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:

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.

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

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

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

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.

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