KEMBAR78
Control Statements | PDF | Control Flow | Computer Science
0% found this document useful (0 votes)
26 views5 pages

Control Statements

Uploaded by

samboxer985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Control Statements

Uploaded by

samboxer985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Control Statements | Control Flow in Java

Java compiler executes the code from top to bottom. The statements in the code are executed according to
the order in which they appear. However, Java provides statements that can be used to control the flow of
Java code. Such statements are called control flow statements.

1. Decision Making Statements --

a) If – else statements

b) Switch Statements

2. Looping --

a) For loop

b) While loop

c) Do-while loop

3. Jumping Statements --

a) Break statements

b) Continue statements

1. Decision Making Statements --- Which statement to execute and when. Decision-making statements
evaluate the Boolean expression and control the program flow depending upon the result of the condition
provided.

a) If-Statements – The control of the flow is diverted depending upon the specific condition. The condition
of the if statements gives a boolean value.

Class abc

public static void main(String args[])

int a=10,b=20;

if(a>b)

System.out.println(a);

else if(b>a)

System.out.println(b);

else

{
System.out.println(“ both are equal “);

b) Switch 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.

Class abcd

public static void main(String args[])

int day=3;

switch(day)

case 1:

System.out.println(“ Monday “);

break;

case 2:

System.out.println(“ Tuesday “);

break;

case 3:

System.out.println(“ Wednesday “);

break;

default :

System.out.println(“ wrong input “);

2. Looping Statements --The Java for loop is used to iterate a part of the program several times. If the
number of iteration is fixed, it is recommended to use for loop.
For Loop - A simple for loop is the same as C/C++. We can initialize the variable, check condition and
increment/decrement value. It consists of four parts:

1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we
can initialize the variable, or we can use an already initialized variable. It is an optional condition.

2. Condition: It is the second condition which is executed each time to test the condition of the
loop. It continues execution until the condition is false. It must return boolean value either true or
false. It is an optional condition.

3. Increment/Decrement: It increments or decrements the variable value. It is an optional


condition.

4. Statement: The statement of the loop is executed each time until the second condition is
false.

Class hello
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
Print a Table with the help of For loop
Class hello
{
public static void main(String args[])
{
int a=13,i;
for(i=1;i<=10;i++)
{
System.out.println(a + “ * ” + i + “ = ” + a * i);
}
}
}

While loop – The Java while loop is used to iterate a part of the program repeatedly until the specified
Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is
recommended to use the while loop.

Class hello2

public static void main(String args[])

int i=1;

while(i<=1)

System.out.println(i);

Do-while loop - The Java do while loop is used to iterate a part of the program repeatedly, until the specified
condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once,
it is recommended to use a do-while loop.

Class hello2

public static void main(String args[])

int i=1;

do

{
System.out.println(i);

} while(i>1);

3. Jumping Statements -
a) Break Statements – When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next statement following the loop. The
break statement is used to break loop or switch statement . If the breaks the current flow of the program to
specified condition. In case of inner loop, it breaks only inner loop.
Class hello
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
break;
System.out.println(i);
}
}
}

b) Continue Statements – The continue statement is used in loop control structure when you need
to jump to the next iteration of the loop immediately. It can be used with for loo[ or while loop. The Java
continue statement is used to current flow of the program and skips the remaining code at the specified
condition. In the case of inner loop, it continues the inner loop only.
Class hello
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
continue;
System.out.println(i);
}
}
}

You might also like