Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.

Similar presentations


Presentation on theme: "Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement."— Presentation transcript:

1 Lecture 11

2 Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement

3 3 Review (Equality and Relational operator) Similar to arithmetic operators Equality operator == equal to != not equal to Relational operator < less than > greater than <= less than or equal to >= greater than or equal to

4 Boolean expression is –a condition that evaluates either true or false –It contains equality or relational operators –It returns boolean results Example int num = 5; boolean flag; flag = num >= 3; Review (boolean expressions) Equality operator Assignment operator true Boolean expression Ask like “is the value of num greater than or equal to 3 ?”

5 Examples boolean a; a = (15 / 3 < 5); System.out.println(“a contains a value “ + a); double num = 2.5; a = (2.5 >= num); System.out.println(“a contains a value “ + a); false true

6 6 if Statement The if statement has the following syntax: if ( condition ) { statement; } if is a Java key word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

7 7 If-else Statement if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement1 is executed. If it is false, the statement2 is executed.

8 8 If-elseif Statement if ( condition1 ) { statement1; } else if ( condition2 ) { statement2; } else { statement3; } The condition1 and condition2 must be a boolean expression. It must evaluate to either true or false. If the condition1 is true, the statement1 is executed. If the condition1 is false and condition2 is true, the statement2 is executed. Otherwise statement3 is executed

9 int MAX = 10; int num = 15; if ( num < MAX ) { System.out.println(“num is smaller?”); } else { System.out.println(“num is bigger!!”); } Print as num is bigger!! Example

10 Exercise Write a class named MyConverter The converter will ask users to enter one of conversions they want to perform –1. Mile to Kilometer conversion –2. Fahrenheit to Celsius conversion Ask users to enter corresponding values Print out the result that users want

11 public class MyConverter { public static void main(String[] argv) { JFrame frame = new JFrame(“My Calculator"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); console.println( “********** MENU ***********”); console.println( “ 1. Distance (Mile->Kilo)” ); console.println( “ 2. Temperature (F->C) ” ); console.println( “******************************”); next page

12 int option; for( int i=0; i<10; i++) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users


Download ppt "Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement."

Similar presentations


Ads by Google