Oop Chaper 3
Oop Chaper 3
Oop Chaper 3
Objectives
2
After completing this chapter you will be able to,
if else statement
switch statement
Selection Statement - IF
7
If Statement:
The if statements can check conditions starting from very simple to
quite complex and execute statements.
Conditions are nothing but a single relational expression or the
combination of more than one relational expressions with logical
operators. If statement comes in three different constructs
A simple If Statement
8
Features of Simple If statement:
The simple if statement allows the execution of a single
statement or a block of statements enclosed within curly braces.
The if statement handles a very simple situation and executes
only when the condition is true otherwise the whole statement
body is skipped .
if
Illustration:
(condition
is true)
Yes
No Execute
statements.
If-else can handle two blocks of code, and only one of those blocks will be
executed based on the condition outcome.
Syntax
if( <condition1> ) {
statements
} If condition1 is satisfied
these statements are executed
else{
statements
If condition1 is not satisfied
}
these statements are executed
If-else Statement Example
12
if-else switch
This can test expressions This tests expressions
based on ranges of based only on a single
values or conditions. integer, enumerated
value, or String object.
Example: if(a==10 &&
b=21) Example: switch(i)//
where I is an int.
Based on the condition to be evaluated developers can either go for switch or if-
else.
Iteration Statement
24
What are Iteration statements?
Iteration Statements are used execute a block of statements
repeatedly as long as a certain condition is true.
A single relational expression or the combination of more than one
relational expression with logical operators are given as
conditions.
Java offers three iteration constructs
While Statement
25
Example:
while(countOfGuests>0){
System.out.println(“Welcome to my party”);
countOfGuests--;
}
While Statement
26
Facts about while statement:
The while loop is a statement or block of statements that is repeated
as long as some condition is satisfied
Syntax
while (boolean_expression) {
statement1;
statement 2;
…..}
The value of i is
printed for the first
time, even though it
does not match the
condition i< 5
Output:
Lend a Hand – do while
33
Syntax:
for(initialization; loopCondition; iteration) {
statements;
}
The initialization allows to declare and/or initialize loop variables, and is executed
only once.
The loopCondition compares the loop variable to some limit value. If the loop
condition is not met it is broken.
The iteration usually increments or decrements the values of the loop variables
Illustration of a for statement
36
Initialize Loop
counter
Update
Counter
Is
Yes Execute block
conditio
n true?
of commands
No
Program continues..
For Statement Example
class Example{
public static void main(String []args){
for(int i=1; i< 10; i++){
Statement
System.out.println(“The Number is “+i); Executed in
loop
}
Loop Value Condition for loop, Loop value
}
Initialized to Loop executed till incremented.
1. value is < 10
Lend a Hand – For Loop statement
38
Problem statement:
This program iterates through the 100 employees and calculate salary . If one
employee is minor age, i.e. age < 18 it should break the loop and stop the
execution.
while (employeecount<=100) {
if(employeeAge <18)
{
break;
}
calculateSalary();
}
Continue Statement
42
Continue Statements stops the processing the remaining code in the body of the
particular iteration Statement and continue with the next loop.
Problem statement:
This program iterates through the 100 employees and calculate salary . If one
employee is minor age, i.e. age < 18 it should SKIP the salary calculation
logic for the employee and proceed with other employees.
while (employeecount<100) {
if(employeeAge <18)
{
continue;
}
calculateSalary();
}
Continue Statement
43
Option 1: To return a value, simply put the value or expression that needs to
be returned after the return keyword.
return <value/expression>;
Option 2: When the return type of method is void, use the form of return that
does not return a value
return;
In this case, the execution of the method is stopped.
Lend a Hand - Return statement
45