0 ratings 0% found this document useful (0 votes) 2 views 4 pages Java Notes 09
The document explains various control flow statements in Java, including nested if-statements, switch statements, and looping statements (for, while, and do-while loops). It details the syntax and usage of each statement type, emphasizing their specific conditions and functionalities. Additionally, it introduces jump statements, which are used to transfer control within the program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save Java Notes 09 For Later
else
{
statement 2; //executes when all the conditions are false
}
Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if or
else-if statement.
Syntax:
if(condition 1)
i
statement 1; //executes when condition 1 is true
if(condition 2)
{
statement 2; //executes when condition 2 is true
t
Else
{
statement 2; //executes when condition 2 is false
}
I
Switch Statement:
Switch statements are similar to if-else-if statements. The switch statement contains multiple
blocks of code called cases and a single case is executed based on the variable which is being
switched, The switch statement is easier to use instead of if-else-if statements, It also enhances
the readability of the program.
© The case variables can be int, short, byte, char, or enumeration. String type is also
supported since version 7 of Java
© Cases cannot be duplicateco Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
© Break statement terminates the switch block when the condition is satisfied.
itis optional, ifnot used, next case is executed.
© While using switch statements, we must notice that the case expression will be of the
same type as the variable. However, it will also be a constant value.
28‘Syntax:
switch (expression)
{
case value1:
statement];
break;
case valueN:
statementN;
break;
default:
default statement;
‘While using switch statements, we must notice that the case expression will be of the same type
as the variable. However, it will also be a constant value. The switch permits only int, string, and
Enum type variables to be used.
Looping Statements
Loop statements are used to execute the set of instructions in a repeated order. The execution
of the set of instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly.
1. for loop
2. while loop
3. do-while loop
for loop
It enables us to initialize the loop variable, check the condition, and increment/decrement in a
single line of code. We use the for loop only when we exactly know the number of times, we
want to execute the block of code.
29Syntax:
for(initialization, condition, increment/decrement)
{
J/olock of statements
}
while loop
The while loop is also used to iterate over the number of statements multiple times. However, if
‘we don't know the number of iterations in advance, itis recommended to use a while loop. Unlike
for loop, the initialization and increment/decrement doesn't take place inside the loop statement
in while loop.
It is also known as the entry-controlled loop since the condition is checked at the start of the
loop. Ifthe condition is true, then the loop body will be executed; otherwise, the statements after
the loop will be executed.
‘Syntax:
while(condition)
{
J/ooping statements
The do-while loop checks the condition at the end of the loop after executing the loop
statements. When the number of iteration is not known and we have to execute the loop at least
‘once, we can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance. The
syntax of the do-while loop is given below.
Jump Statements
Jump statements are used to transfer the control of the program to the specific statements. In
other words, jump statements transfer the execution control to the other part of the program,
There are two types of jump statements in Java, ie., break and continue.