SELF STUDY IF STATEMENTS SELECTION STRUCTURE if selection

  • Slides: 13
Download presentation
SELF STUDY

SELF STUDY

IF STATEMENTS

IF STATEMENTS

SELECTION STRUCTURE if selection statement if…else selection statement switch selection statement

SELECTION STRUCTURE if selection statement if…else selection statement switch selection statement

EXAMPLE Write a program that reads a student’s grade and prints “passed” if the

EXAMPLE Write a program that reads a student’s grade and prints “passed” if the grade is greater than or equal 60. Flowchart: Code: . . . if ( grade >= 60 ) System. out. print("Passed”); . . .

ONE-WAY SELECTION Example: char grade=‘’ if ( score >= 90 ) 5 grade =

ONE-WAY SELECTION Example: char grade=‘’ if ( score >= 90 ) 5 grade = ‘A’;

EXAMPLE IF-STATEMENT: FIND ABSOLUTE VALUE import java. util. Scanner; public class If 1 {

EXAMPLE IF-STATEMENT: FIND ABSOLUTE VALUE import java. util. Scanner; public class If 1 { public static void main(String[] args) { Scanner in = new Scanner(System. in); int a; System. out. print("Enter an integer value: "); a = in. next. Int(); if ( a < 0 ) a = -a; // Read in an integer from keyboard // If-statement // Negate a ONLY if a < 0 System. out. print("It's absolute value = "); System. out. println(a); } }

TWO-WAY SELECTION Syntax: if (expression) statement 1; else statement 2; else statement must be

TWO-WAY SELECTION Syntax: if (expression) statement 1; else statement 2; else statement must be paired with an if. 7

TWO-WAY SELECTION 8 Example: boolean positive, negative; if (number >= 0 ) positive =

TWO-WAY SELECTION 8 Example: boolean positive, negative; if (number >= 0 ) positive = true; else //number < 0 negative =true;

MULTIPLE SELECTION: NESTED IF Example 4_23 : if ( GPA >= 2. 0 )

MULTIPLE SELECTION: NESTED IF Example 4_23 : if ( GPA >= 2. 0 ) if (GPA >= 3. 9) System. out. println(“Dean Honor list”); else System. out. println(“GPA below graduation requirement”); If GPA = 3. 8 what will be printed? 9 GPA below graduation requirement

MULTIPLE SELECTION: NESTED IF Example 4_23 : (rewritten) if ( GPA >= 2. 0

MULTIPLE SELECTION: NESTED IF Example 4_23 : (rewritten) if ( GPA >= 2. 0 ) { if (GPA >= 3. 9) System. out. println(“Dean Honor list”); } else System. out. println(“GPA below graduation requirement”); Now, if GPA = 3. 8 what will be printed? 10

public class Test { public static void main(String args[]){ int x = 30; if(

public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System. out. print("Value of X is 10"); } else if( x == 20 ){ System. out. print("Value of X is 20"); } else if( x == 30 ){ System. out. print("Value of X is 30"); } else{ System. out. print("This is else statement"); } } }

EXERCISE What is the output for the following code lines if hours=35 and hours=60:

EXERCISE What is the output for the following code lines if hours=35 and hours=60: a) if (hours <= 40) System. out. println(“No overtime” ); System. out. println(“ You must work over 40 hours for overtimen”); System. out. println(“Continue” ); B) if (hours <= 40) { cout << “No overtime” << endl; cout << "You must work over 40 hours for overtime. n"; } cout << “Continue” << endl;