1. Using Java
MINISTRY OF EDUCATION & HIGHER EDUCATION
COLLEGE OF SCIENCE AND TECHNOLOGY
KHANYOUNIS- PALESTINE
Lecture 7
Decision Making : selection statements (cont.)
2. if...else Double-Selection Statement
Nested if...else Statements
Blocks
Switch
Be care
Practices
Emank X Mezank
2Presented & Prepared by: Mahmoud R. Alfarra
3. 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.
3Presented & 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. 4Presented & Prepared by: Mahmoud R. Alfarra
if...else Double-Selection Statement
X<=0
yes
F(x)= XF(x)= - X
NO
Write a program to code the above flowchart
HW 7.1
5. Write a program to receive an integer from
user and then print if it is an even or an odd
5Presented & Prepared by: Mahmoud R. Alfarra
Example: Print Even or Odd
Write the pseudo code and flowchart of the above
exampleHW 7.2
6. 6Presented & 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 oddHW 7.3
7. 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.
7Presented & Prepared by: Mahmoud R. Alfarra
Nested if...else Statements
9. 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”.
9Presented & Prepared by: Mahmoud R. Alfarra
Example: Privacy login
Write the pseudo code and flowchart of the above
exampleHW 7.4
10. 10Presented & 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 oddHW 7.5
Example: Privacy login
11. 11Presented & 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:
“Ali”.ComparTo(“zain”) = 1
“Ali”.ComparTo(“Ali”) = 0
“yahia”.ComparTo(“ahmad”) = -1
12. 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.
12Presented & Prepared by: Mahmoud R. Alfarra
Blocks
13. A block can be placed anywhere in a program
that a single statement can be placed.
13Presented & 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. 14Presented & 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 …
15. 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.
15Presented & Prepared by: Mahmoud R. Alfarra
Switch
A constant value in expression of type byte, short,
int or char, but not long.
17. Write a program to receive an integer from
user and then print the day of this number as
▪ 1 : Saturday
▪ 2 : Sunday
▪ ….
▪ ….
17Presented & Prepared by: Mahmoud R. Alfarra
Example: Print Days
Write the pseudo code and flowchart of the above
exampleHW 7.6
18. 18Presented & Prepared by: Mahmoud R. Alfarra
Example: Print Days
Re-Write this program using one of if statements
HW 7.7