OOPR211 Module 5
OOPR211 Module 5
8 10 12
1 2
⊳ Understanding if statements
⊳ Complex conditionals
⊳ The switch, case, and break statements
Java Statement
▸ If-Else
▸ If-Else-If
▸ Switch structure
Understanding
1 if statements
THE if STATEMENT
NO YES
Boolean
expression
THE if STATEMENT
If Structure: Example 1
If Structure: Example 2
1. Test if the value of the variable count is greater than 10. If the
answer is yes, print “Count is greater than 10”.
3. Test if the value of the variable age is greater than 18 and less than
or equal to 40. If the answer is yes, print “You’re not getting old!”
18
2 Complex
conditionals
THE if-else STATEMENT
The simple if-else statement has the following
syntax:
if (<boolean expression>)
<statement 1>
else
<statement 2>
THE IF/ELSE STATEMENT IS AN EXTENSION OF THE IF
STATEMENT. IF THE STATEMENTS IN THE IF STATEMENT FAILS,
THE STATEMENTS IN THE ELSE BLOCK ARE EXECUTED.
THE if-else STATEMENT
THE if-else STATEMENT
1. Test if the value of the variable count is greater than 10. If it is,
print “Count is greater than 10”, otherwise, print “Count is less
than 10”.
2. Test if the value of the variable number is an odd number. If it is,
print “This number is not an even number” otherwise, print “This
number is an odd number.”
3. Test if the value of the variable age is greater than 18 and less
than or equal to 40. If it is, print “You’re not getting old!”
otherwise, print “You’re not getting younger.”
26
NESTED if STATEMENT
NESTED if STATEMENT
break statement
• defines the end of a case.
switch case STATEMENT
default statement
• use in defining message to be displayed
that is outside of the given case.
switch case
int grade = 92;
switch case
switch (grade) {
case 100:
System.out.println(“Excellent!”);
break;
case 90:
System.out.println(“Good!”);
break;
case 80:
System.out.println(“Study Harder!”);
break;
default:
System.out.println(“You failed!”);
}
Write a program to output summation of a
set of numbers.
Example Output:
Please input how many numbers you
would like to add: 4
Check your Please enter a value: 3
Please enter a value: 1
Understanding Please enter a value: 2
Please enter a value: 3
The sum of the 4 values is 9
Write a program which
should be able to reproduce
the following example
Check your output:
Understanding
QUESTIONS?
Thank you!
Credits
⊳ https://www.edureka.co/blog/what-is-java/
⊳ http://ecomputernotes.com/java/what-is-java-
language/types-of-java-programs
⊳ https://app.codechum.com/teacher/classes/504/tim
eline/lessons/java-conditionals/if-statement
OUR LADY OF FATIMA UNIVERSITY
COLLEGE OF COMPUTER STUDIES