Introduction to programming in java Lecture 07 Conditional














- Slides: 14
Introduction to programming in java Lecture 07 Conditional if statements
Basic boolean Operators Operator Meaning Example == equal (4 -2)==(8 -6) is true != Not equal 3 !=2 is true but 4!=(6 -2) is false > Greater than (3>2) is true >= Greater or equal (3>=2) is true < Less than (3<2) is false <= Less or equal (3<=4) is true
Compound Boolean expressions Operator Meaning Example && And True && true is true (3>2) && (1<=10) is true True && False is False (3>2) && (1>10) is False && False is false (3<2) && (1>10) is False || OR True || true is true (3>2) || (1<=10) is true True|| False is True (3>2) || (1>10) is True False || False is false (3<2) || (1>10) is False
Compound Boolean Expressions (Cont) && • True && True = True • False && False = False • (True && False) = (False && True) = False || • True || True = True • False || False = False • (True || False) = (False || True) = True
If – The Conditional Statement • The if statement evaluates an expression and if that evaluation is true then the specified action is taken if ( x < 10 ) x = 10; • If the value of x is less than 10, make x equal to 10 • It could have been written: if ( x < 10 ) x = 10; • Or, alternatively: if ( x < 10 ) { x = 10; }
The if statement Executes a block of statements only if a test is true if (test) { statement; . . . statement; } • Example: int x = 2; if (x >= 0) { System. out. println(“ x is positive"); }
The if/else statement Executes one block if a test is true, another if false if (test) { statement(s); } else { statement(s); } • Example: int x = 2; if (x >= 0) { else System. out. println(“ x is positive"); } { } System. out. println(“x is negative. ");
Example 1 What does this program do? // public class Is. Even { public static void main(String[] arguments) { int x = Integer. parse. Int(args[0]) if (x % 2 == 0) { System. out. println(“YES”); } else { System. out. println(“No”); } } } //end of program
Example 2 What does this program do? // public class Is. Odd { public static void main(String[] arguments) { int x = Integer. parse. Int(args[0]); if (x % 2 == 1) { System. out. println(“YES”); } else { System. out. println(“No”); } } } //end of program
Example 3 What does this program do? // public class First. Is. Multiple. Of. Second { public static void main(String[] arguments) { int x = Integer. parse. Int(args[0]); int y = Integer. parse. Int(args[1]); if (x % y == 0) { System. out. println(“YES”); } else { System. out. println(“No”); } } } //end of program
Example 4 What does this program do? // public class One. Is. Multiple. Of. Other { public static void main(String[] arguments) { int x = Integer. parse. Int(args[0]); int y = Integer. parse. Int(args[1]); if ((x % y ==0 ) || (y%x ==0 )) { System. out. println(“YES”); } else { System. out. println(“No”); } } } //end of program
Example 5 Nested Ifs // program that takes the exam final mark prints out the corresponding grade. public class Examgrade { public static void main(String[] arguments) { double testscore = double. parse. Double(args[0]); char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System. out. println("Grade = " + grade); } } //end of program
Practice Question Write a program, Dayof. The. Week. java, that takes one command line integer n = 1, 2, . . . , 7. The program prints the following n n n n = = = = 1 2 3 4 5 6 7 it it prints prints Monday Tuesday Wednesday Thursday Friday Saturday Sunday Hints: you can either use Nested ifs or a switch statement.
Solution ( nested if else) Dayof. The. Week. java public class Dayof. The. Week { public static void main(String[] args) { int day = Integer. parse. Int(args[0]); String today; if(day ==1) { today= "Monday"; } else if(day==2) {today= "Tuesday"; } else if(day == 3) {today= "Wednesday"; } else if(day==4) {today= "Thursday"; } else if(day==5) {today= "Friday"; } else if(day==6) {today= "Saturday"; } else if(day==7) {today= "Sunday"; } else {today= "Wrong number Entered"; } System. out. println(" Today is " + today); } } //end of program