Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements (cont.)
if...else Double-Selection Statement Nested if...else Statements Blocks Switch Be care Practices Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra
The if...else double-selection statement allows the programmer to specify an action to perform when the condition is true and a different action when the condition is false. 3 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement If student's grade is greater than or equal to 60 Print "Passed" Else Print "Failed" if ( grade >= 60 ) System.out.println( "Passed" ); else System.out.println( "Failed" );
4 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement X <= 0 yes F(x) = X F(x) =- X NO Write a program to code the above flowchart HW 7.1
Write a program to receive an integer from user and then print if it is an even or an odd 5 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Even or Odd Write the pseudo code and flowchart of the above example HW 7.2
6 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Even or Odd Write a program to receive two an integer from user and then print if it is an even or an odd HW 7.3
If student's grade is greater than or equal to 90 Print "A" else If student's grade is greater than or equal to 80 Print "B" else If student's grade is greater than or equal to 70 Print "C" else If student's grade is greater than or equal to 60 Print "D" else Print "F" A program can test multiple cases by placing if...else statements inside other if...else statements to create nested if...else statements. 7 Presented & Prepared by: Mahmoud R. Alfarra Nested if...else Statements
8 Presented & Prepared by: Mahmoud R. Alfarra if...else Double-Selection Statement X <= 50 yes return “Success” return fail NO X <= 80 yesNO return “pass”
Write a program to receive the user name and then password from user, and if the user name matches “onway2allah” and the password matches “muslim4ever”, the program print “open”. Else the program print “pass is mismatched” or & “user is mismatched”. 9 Presented & Prepared by: Mahmoud R. Alfarra Example: Privacy login Write the pseudo code and flowchart of the above example HW 7.4
10 Presented & Prepared by: Mahmoud R. Alfarra Write a program to receive two an integer from user and then print if it is an even or an odd HW 7.5 Example: Privacy login
11 Presented & Prepared by: Mahmoud R. Alfarra Comparing strings : Be care Comparing strings is doing by using (ComparTo) method, not by ==. Using the following formula: String1.ComparTo(string2), this comparison results: 1 : means the string1 is larger than string2 -1: means the string2 is larger than string1 0: means the two strings are equal Using the following formula: String1.ComparTo(string2), this comparison results: 1 : means the string1 is larger than string2 -1: means the string2 is larger than string1 0: means the two strings are equal Using the following formula: “Ali”.ComparTo(“zain”) = 1 “Ali”.ComparTo(“Ali”) = 0 “yahia”.ComparTo(“ahmad”) = -1 Using the following formula: “Ali”.ComparTo(“zain”) = 1 “Ali”.ComparTo(“Ali”) = 0 “yahia”.ComparTo(“ahmad”) = -1
The if statement normally expects only one statement in its body. To include several statements in the body of an if (or the body of an else), enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a block. 12 Presented & Prepared by: Mahmoud R. Alfarra Blocks
A block can be placed anywhere in a program that a single statement can be placed. 13 Presented & Prepared by: Mahmoud R. Alfarra Blocks Forgetting both of the braces that delimit a block will lead to logic error in a program. Forgetting one of the braces that delimits a block will lead to syntax error in a program.
14 Presented & Prepared by: Mahmoud R. Alfarra Example: Blocks if ( grade >= 60 ) System.out.println( "Passed" ); else { System.out.println( "Failed" ); System.out.println( "You must take this course again." ); } All the instructions into the block are related to the if and else …
Java provides the switch multiple-selection statement to perform different actions based on the possible values of an integer variable or expression. Each action is associated with the value of a constant integral expression. 15 Presented & Prepared by: Mahmoud R. Alfarra Switch A constant value in expression of type byte, short, int or char, but not long.
16 Presented & Prepared by: Mahmoud R. Alfarra Formula: Switch switch (expr.) { case x: // instructions break; case y: // instructions break;. default : // instructions } switch (expr.) { case x: // instructions break; case y: // instructions break;. default : // instructions }
Write a program to receive an integer from user and then print the day of this number as ▪ 1 : Saturday ▪ 2 : Sunday ▪ …. 17 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Days Write the pseudo code and flowchart of the above example HW 7.6
18 Presented & Prepared by: Mahmoud R. Alfarra Example: Print Days Re-Write this program using one of if statements HW 7.7
قال الله تعالى: (يوم تكون الجبال كالعهن المنفوش) 19 Presented & Prepared by: Mahmoud R. Alfarra
Practices 20 Presented & Prepared by: Mahmoud R. Alfarra