Lecture 7
Lecture 7
Unit-I
Principles of Object-Oriented Programming
2
Content
(Unit I)
Data Types
Chapter 1.2:
Lecture 1.2.3-
1. Java Control Statement
2. Types of Control Statement
3
Objectives/Outcomes
4
Control Statement
5
Types of Control Statement
Types of control / decision making statements in Java:
1. If
2. If-else
3. Nested-if
4. If-else-if ladder
5. Switch-case
6. Jump – break, continue, return
6
If Statement
• if statement is the most simple decision making statement. It is used to decide
whether a certain statement or block of statements will be executed or not i.e. if a
certain condition is true then a block of statement is executed otherwise not.
• SYNTAX
if(condition)
{
//statements to be executed if the condition is true
}
Fig 1: If Statement[1]
7
Example of ‘if’ statement
8
If-else Statement
• We can use the else statement with if statement to execute a block of code when the
condition is false.
• SYNTAX
if(condition)
{
//execute if the condition is true
}
else
{
//execute if the condition is false
}
Fig 2: If –else Statement[2]
9
Example of ‘if-else’ statement
10
Nested if
• Nested if statements means an if statement inside an if statement.
• SYNTAX
if(condition1)
{
//execute if the condition1 is true
}
if(condition2)
{
//execute if the condition is false
}
11
Example of ‘nested-if’
12
4. if-else-if ladder
• Here, a user can decide among multiple options.
• SYNTAX
if(condition) {
statement;
}
else-if(condition) {
statement;
}
else {
statement;
}
Fig 4: If –else-if Statement
13
Example of ‘if-else-if’
14
Switch case
• The switch statement is a multi-way branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
Syntax:
switch (expression) {
case value1:
statement1;
break;
case value2: statement2;
break;
.
default:
statement
Default;
} Fig 5: Switch Statement[5]
15
Example of ‘switch case’
16
Jump statement
Java supports three jump statement: break, continue and return. These three
statements transfer control to other part of the program.
(a) BREAK
In Java, break is majorly used for:
Terminate a sequence in a switch statement (discussed above).
To exit a loop.
Used as a “civilized” form of goto.
18
Jump statement contd.
(b) CONTINUE
Sometimes it is useful to force an early
iteration of a loop. That is, you might
want to continue running the loop but
stop processing the remainder of the code
in its body for this particular iteration.
This is, in effect, a goto just past the body
of the loop, to the loop’s end. The
continue statement performs such an
action.
19
Example of ‘continue’
20
Jump statement contd.
(c) RETURN
The return statement is used to
explicitly return from a method.
That is, it causes a program
control to transfer back to the
caller of the method.
21
References
• Fig 1:https://www.geeksforgeeks.org/decision-making-javaif
-else-switch-break-continue-jump/
• Fig 2:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
• Fig 3:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
• Fig 4:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
• Fig 5:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
• Fig 6:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
• Fig 7:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
22
References
• https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-ju
mp/
• https://www.w3schools.com/java/java_conditions.asp
23
THANK YOU
24