1-7-Conditional Statements in Java
1-7-Conditional Statements in Java
statements
JAVA
By,
Ashwini K S
Introduction
Program
{
………………………..
………………………..
…………………………
Flow of control
………………………..
………………………..
……………………….
}
Conditional flow of control
Program
{
………………………..
Statement 2
………………………..
…………………………
Flow of control
………………………..
………………………..
Statements 6
……………………….
}
If constructs
– The if constructs are used to direct the flow of control to the block of code that
is to be executed.
– There 4 types of if statements
– If statement(simple if)
– If-else statement
– If-else-if statement
– Nested if statement
If statement
– In this statement there will be 2 blocks of code. One is true block and another is
false block.
– If the condition is true then true block will be executed.
– If the condition is false then the false block will be executed.
– Example:
if (a>b)
System.out.println( “a is greater”);
else
System.out.println(“b is greater”);
If - else-if statement
If (marks>35)
{
if(marks>59)
{
if (marks>84)
System.out.println(“ Distinction”);
else
System.ou.println(“First class”);
}
else
System.out.println(“ Second class”);
}
else
System.out.println(“Fail”);
Unusual termination of a program
Unlike if statement, if there are multiple blocks in any case , then switch case
statement has to be used to handle this multi branching flow of control.
n4
Op
o
tio
ti
Op
n2
Option 1 J Option 2
Option 1 J Option 4
Syntax
– https://www.youtube.com/watch?v=NdAMIOVOGLo
– https://www.youtube.com/watch?v=XYlifs5oPaQ