Week 3 If Control Structure.pdf
Document Details
Uploaded by SimplifiedLaplace
Tags
Full Transcript
Selection Structures if Control Structure 1 Control Structures • Control Structures determine the order in which statements or instructions are carried out in a Java program. • The three main control structures in any programming language are – Sequence – Selection – Repetition/Iteration • 2 C...
Selection Structures if Control Structure 1 Control Structures • Control Structures determine the order in which statements or instructions are carried out in a Java program. • The three main control structures in any programming language are – Sequence – Selection – Repetition/Iteration • 2 Control Structures • Sequence • Selection – – – – if if/else else/if or switch if/else ladder(chain) • Repetition/Iteration – for – while – do/while 3 Control Statements Sequence - instructions are executed in the same order in which they appear within the program • Unrealistically simple – no logical control structures Selection - one group of statements is selected from several available groups Repetition - instructions are executed repeatedly, until some logical condition has been satisfied. 4 Selection • Programs often require alternative actions depending on different inputs to the program by the user. • For example, the price of a cinema ticket may be determined by age. PROGRAM OUTPUT 1 Please enter age:23 Price per ticket: €10.00 PROGRAM OUTPUT 2 Please enter age:16 Price per ticket:€5.00 ‘if’ selection structure • Selection structure is used to choose between alternative courses of action. • keyword if followed by an expression in parentheses. • If the expression is found to be true, then the statement (or statements) following the if are executed. • If the expression is false, the statements following the if are not executed. 6 The if Statement • Syntax of if if (expression) statement; • For example if (accountBalance<0) { System.out.println("you are overdrawn"); } This statement tests whether the variable accountBalance is less than 0. • If true, then the statement is executed and the message is displayed. • If accountBalance contains a number greater than or equal to 0, then the statement is not executed. 7 Sample Program 1 import java.util.*; public class IfTest { //main method public static void main(String[] args) { //create instance of scanner class Scanner keyboardIn = new Scanner(System.in); //declare variables double accountBalance; //prompt user to input System.out.print("Enter Account Balance: "); //read and store keyboard input accountBalance = keyboardIn.nextDouble(); if(accountBalance < 0) { //display message System.out.println("You are overdrawn!!"); } }//end main method }//end class 8 Flow chart for if statement true Conditional exp false statements Flow chart for if statement accBalance < 0 false true DISPLAY “overdrawn” Relational operators < <= > >= less than less than or equal to greater than greater than or equal to Equality operators = = equal to != not equal to 11 Comparing Values: Relational Operators Relational operators compare values Java Math Notation Description > > Greater than >= ≥ Greater than or equal to < < Less than <= ≤ Less than or equal to == = Equal to != ≠ Not equal to • The == denotes equality testing a = 5; // Assign 5 to a if (a == 5) . . . // Test whether a equals 5 12 Common error • Confusing equality (==) Operator and Assignment Operator (=) • If these operators are confused it will not always result in a compiler error the program may compile successfully but it will not run as expected. Assignment operator (=) vs equality operator (==) For example: if (salary == 10000) System.out.println("Give payrise"); • In the example, we are checking if salary is equal to 10000 using the equality operator (==). • If true the message “Give payrise” will be displayed on the screen. Assignment operator (=) vs equality operator (==) • If we simply wanted to give the value of 10000 to the variable salary then we use assignment operator (=) double salary = 10000; • the equality operator (==) used when comparing values, and • the assignment operator (=) used when assigning values to variables. Write logical expression • number of hours worked is 37 or over hoursWorked >= 37 • temperature is less than 0 temperature < 0 • exam mark is 40 or above examMark >= 40 16 Examples - if if(x < 0) { System.out.print(x); } Causes the value of the variable x to be displayed if its value is negative if(balance > 0) { fee = 0; } A value of zero is assigned to fee if the value of balance 17 exceeds zero • Determine if an employee has worked over time (over 38 hours) if(hrsWked > 38) { System.out.print(“Overtime hours: " +(hrsWked - 38)); } 18 • Determine if a rectangle is a square if(length == width) { System.out.print("it is square"); } 19 • Calculate and apply 10% discount if totalPrice is more than €100 if(totalPrice >= 100) { disc = totalPrice * .1; totalPrice = totalPrice – disc; } 20 3 Space Indentation • 3 space indentation is important in the if statement. Everything after the if statement should be indented 3 spaces (use tab) as shown. if(grade > 39) { ~~~System.out.print("PASS"); } Program Example 1 22 Program Example 2 23