Conditional
statements
JAVA
By,
Ashwini K S
Introduction
– Programs is a set statements or instructions.
– When the execute command is issued , the control reaches the first line of
program .
– It keeps executing the statements one by one till the end.
– The movement of control from one line to another is known as ‘Flow control’
– The flow of control is maintained in the following ways.
– Normal flow of control.
– Conditional flow of control.
Normal flow of control
– The normal flow of control among statements is sequential, proceeding from
one statement to the next.
Program
{
………………………..
………………………..
…………………………
Flow of control
………………………..
………………………..
……………………….
}
Conditional flow of control
– A set of statements that are grouped together in a program are known as a
block.
– During execution it may happen that we want to execute a block by ignoring
another block base on the given condition.
– In such situation the control is diverted and sent to the block of code we want
to execute.
– It is known as conditional 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
– If statement checks for the condition first.
– If the condition is true, it executes a block of statements. this block is usually called
true block
– If the condition is false, it just ignores the true block and continues executing reaming
statements of program.
– If there are more than one statement in the block then they should be written within
the curly brackets.
– Example :
if (a>10)
c=a*a
Sytem.out.println(c);
If-else 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
– In this there will be multiple conditions
– There will be multiple blocks of code.
– Upon the conditions result, any one of the blocks will be executed.
– Example: greatest of 3 numbers
if (a>b)
{ if(a>c)
system.out.println(“ a is greater”);
else
Syetm.out.prinln(“ c is greater “); }
elseif(b>c)
system.out.println(“b is greater”);
else
system.out.println(“c is greater”);
Nested if statement
– When if statement is used within another if statement , then the construct is
said to be nested if statement.
– In this if the condition is true then the control flows to another condition and it
goes on..
Nested if statement-example
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
– Sometimes we may need terminate the execution of a function, even if the
code can be executed.
– sometimes we may expect run time errors like divide by zero or trying to find
square root of a negative number.
– In such situations , to make program error free , we check in advance for the
possible errors .
– It there is a chance of getting error , then we stop executing that function and
return back to main program.
– To do so we use System.exit(0)
Example-1
System.out.println(“enter a number “);
n= in.nextInt();
If(n<0)
{
System.out.println(“It is a negative number”);
System.ou.println(“The program terminates”);
System.exit(0);
}
Sqt=Math.sqrt(n);
System.out.println(“Squar root of “+n+”=“+sqt);
Example 2- compute y in the expression
System.out.println(“Enter the value of a and b”);
a=in.nextInt();
b=in.nextInt();
If ((a-b)==0)
{
System.out.println(“Division by zero is not possible. Program terminates”);
System.exit(0);
}
y=(a+b)/(a-b);
System.out.println(“ Y=“+y);
Switch Statement
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
switch(n) Control variable
{ switch(n)
case 1: // code block {
break; Break statement case 1: // code block
case 2: // code block -----------------
break; case 2: // code block No break
statement
case n: // code block ------------------
break; default: // code block Fall through
default: // code block Default case }
}
Terms related to switch statement
– Control Variable: it is the variable passed as an argument.
– Break statement : This statement is used to exit from the switch case.
– Default statement : If no case is matched then the switch block will executed
the default case.
Working of switch statement
– The variable used in a switch statement can only be integers, convertible
integers (byte, short, char), strings and enums.
– You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
– The value for a case must be the same data type as the variable in the switch
and it must be a constant or a literal.
– When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
Working of switch statement
– When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
– Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
– A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when
none of the cases is true. No break is needed in the default case.
– Test example: https://
compiler.javatpoint.com/opr/test.jsp?filename=SwitchMonthExample
References
– https://www.youtube.com/watch?v=NdAMIOVOGLo
– https://www.youtube.com/watch?v=XYlifs5oPaQ