0% found this document useful (0 votes)
16 views

Control Statements.pdf

The document provides an overview of control flow statements in Java, which include decision-making, loop, and jump statements. It details various types of if statements, switch statements, and different looping constructs such as for, while, and do-while loops, along with examples and syntax. Additionally, it explains jump statements like break and continue that alter the flow of control in loops.

Uploaded by

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

Control Statements.pdf

The document provides an overview of control flow statements in Java, which include decision-making, loop, and jump statements. It details various types of if statements, switch statements, and different looping constructs such as for, while, and do-while loops, along with examples and syntax. Additionally, it explains jump statements like break and continue that alter the flow of control in loops.

Uploaded by

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

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. It is one of the fundamental
features of Java, which provides a smooth flow of program.

Java provides three types of control flow statements.

1.Decision Making statements

if statements

switch statement

2.Loop statements

do while loop

while loop

for loop

for-each loop

3.Jump statements

break statement

continue statement

If Statement:

In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value, either
true or false. In Java, there are four types of if-statements given below.

1. Simple if statement

2. if-else statement

3. if-else-if ladder

4. Nested if-statement

1) Simple if statement:

It is the most basic statement among all control flow statements in Java. It evaluates a Boolean
expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement

if(condition) {

statement 1; //executes when condition is true

Flowchart

program

public class Csif

public static void main(String[] args)

int x = 10;

int y = 12;

if(x+y > 20)

System.out.println("x + y is greater than 20");

}
}

Output:

x + y is greater than 20

2) if-else statement

The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else
block. The else block is executed if the condition of the if-block is evaluated as false.

Syntax:

if(condition) {

statement 1; //executes when condition is true

else{

statement 2; //executes when condition is false

Flowchart:

program

public class Csifelse

{
public static void main(String[] args)

int x = 10;

int y = 12;

if(x+y < 10)

System.out.println("x + y is less than 10");

else

System.out.println("x + y is greater than 20");

} Output:

x + y is greater than 20

3) if-else-if ladder:

The if-else-if statement contains the if-statement followed by multiple else-if statements. In other
words, we can say that it is the chain of if-else statements that create a decision tree where the program
may enter in the block of code where the condition is true. We can also define an else statement at the
end of the chain.

Syntax:

if(condition 1) {

statement 1; //executes when condition 1 is true

else if(condition 2) {

statement 2; //executes when condition 2 is true

}
else {

statement 2; //executes when all the conditions are false

Flowchart:

program

public class Csifelseifladder

public static void main(String[] args)

String name = "vinay";

if(name == "mahi")

System.out.println("name is mahi");

else if (name== "mani")

System.out.println("name is mani");
}

else if(name == "mohan")

System.out.println("name is mohan");

else

System.out.println(name);

} Output:

vinay

4. Nested if-statement

 Nested if in Java refers to having one if statement inside another if statement. If the outer
condition is true the inner conditions are checked and executed accordingly. Nested if condition
comes under decision-making statement in Java, enabling multiple branches of execution.
 Normal if condition checks condition independently which means each condition works on its
own.
 Whereas nested if checks conditions that depend on each other, which means one condition is
only checked if another condition is true.

Syntax

if (condition1) {

if (condition2) {

if (condition3) {

// statements;

}
If the outer condition satisfies then only the inner condition will be checked. Along with if condition, else
condition can also be executed.

Flow chart:

Program

public class CsNestedIf

public static void main(String[] args)

int number = 10;

if (number > 0)

System.out.println("The number is positive.");

if (number % 2 == 0)

System.out.println("The number is even.");

}
else

System.out.println("The number is odd.");

else if (number < 0)

System.out.println("The number is negative.");

else

System.out.println("The number is zero.");

Output:

The number is positive.

The number is even.

switch statement

The switch statement allows us to execute a block of code among many alternatives.

Syntax:

switch (expression) {

case value1:

// code

break;
case value2:

// code

break;

...

...

default:

// default statements

How does the switch-case statement work?

The expression is evaluated once and compared with the values of each case.

If expression matches with value1, the code of case value1 are executed. Similarly, the code of case
value2 is executed if expression matches with value2

If there is no match, the code of the default case is executed

Flowchart:
program

class SwitchCase

public static void main(String[] args)

int expression = 9;

switch(expression)

case 2:

System.out.println("Small Size");

break;

case 3:

System.out.println("Large Size");

break;

default:

System.out.println("Unknown Size");

Output

Unknown Size

Loops/Iteration statements:

Looping in programming languages is a feature that facilitates the execution of a set of


instructions/functions repeatedly while some condition evaluates to true. Java provides three ways for
executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and
condition-checking time.

In Java, there are three types of Loops :


 for loop
 while loop
 do-while loop

1. for loop:

The for loop is used when we know the number of iterations (we know how many times we want to
repeat a task). The for statement consumes the initialization, condition, and increment/decrement in
one line thereby providing a shorter, easy-to-debug structure of looping.

Syntax:

for (initialization; condition; increment/decrement) {

// code to be executed

flow chart of a for loop:

• Initialization condition: Here, we initialize the variable in use. It marks the start of a for
loop. An already declared variable can be used or a variable can be declared, local to
loop only.
• Testing Condition: It is used for testing the exit condition for a loop. It must return a
boolean value. It is also an Entry Control Loop as the condition is checked prior to the
execution of the loop statements.
• Statement execution: Once the condition is evaluated to true, the statements in the
loop body are executed.
• Increment/ Decrement: It is used for updating the variable for next iteration.
• Loop termination:When the condition becomes false, the loop terminates marking the
end of its life cycle.

program

import java.io.*;

class Numbers

public static void main(String[] args)

for (int i = 0; i<= 10; i++)

System.out.print(i + " ");

Output

0 1 2 3 4 5 6 7 8 9 10

2. while Loop

A while loop is used when we want to check the condition before running the code.

Syntax:

while (condition) {

// code to be executed

Flow chart:
• While loop starts with the checking of Boolean condition. If it evaluated to true, then the loop
body statements are executed otherwise first statement following the loop is executed. For this reason
it is also called Entry control loop

• Once the condition is evaluated to true, the statements in the loop body are executed. Normally
the statements contain an update value for the variable being processed for the next iteration.

• When the condition becomes false, the loop terminates which marks the end of its life cycle.

Program

import java.io.*;

class Sample2

public static void main(String[] args)

int i = 0;

while (i<= 10)

System.out.print(i + " ");

i++;

}
}

Output

0 1 2 3 4 5 6 7 8 9 10

do-while Loop:

The do-while loop in Java ensures that the code block executes at least once before the condition is
checked.

Syntax:

do {

// code to be executed

} while (condition);

• do while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.

• After the execution of the statements, and update of the variable value, the condition is checked
for true or false value. If it is evaluated to true, next iteration of loop starts.

• When the condition becomes false, the loop terminates which marks the end of its life cycle.

• It is important to note that the do-while loop will execute its statements atleast once before any
condition is checked, and therefore is an example of exit control loop.

Flow chart
Program

import java.io.*;

class Samples

public static void main(String[] args)

int i = 0;

do

System.out.print(i + " ");

i++;

while (i<= 10);

Output

0 1 2 3 4 5 6 7 8 9 10

Nested for Loop

If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes
completely whenever outer loop executes.

The structure allows us to perform complex iterations by running a for loop for each iteration of the
outer for loop. Nested for loops are particularly useful when working with multi-dimensional data
structures like arrays or when we need to perform operations that require multiple levels of looping,
such as generating a matrix or table.

Syntax:

for (initialization; termination; increment) {

// Outer loop block


for (initialization; termination; increment) {

// Inner loop block

Flowchart:

Program:

public class NestedForExample

public static void main(String[] args)

for(int i=1;i<=3;i++)

for(int j=1;j<=3;j++)

System.out.println(i+" "+j);
}

Output:

1 1

1 2

1 3

2 1

2 2

2 3

3 1

3 2

3 3

For-each loop in Java

The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify
iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is
commonly used when the exact index of an element is not required.

Syntax of For-each Loop

for (type var : array) {

statements using var;

Parameters:

• type: The data type of the elements in the array or collection.

• var: The variable that holds the current element during each iteration.

• array: The array or collection being iterated over.


Program

Below is a basic example of using the for-each loop to iterate through an array and print each element.
A for-each loop directly accesses elements without needing index variables.

import java.io.*;

class ForEach

public static void main(String args[])

int arr[] = { 1, 2, 3, 4, 5 };

for (int e : arr)

System.out.println(e + " ");

Output

12345

In this example, the for-each loop iterates over the array “arr" and prints each element. We use the
variable element "e" to access each value in the array, making the loop more concise compared to using
a traditional for loop with an index.

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, i.e., break and continue.

break statement

the break statement is used to break the current flow of the program and transfer the control to the
next statement outside a loop or switch statement. However, it breaks only the inner loop in the case of
the nested loop.
The break statement cannot be used independently in the Java program, i.e., it can only be written
inside the loop or switch statement.

program

public class BreakStatement

public static void main(String[] args)

for (int i = 0; i< 10; i++)

if (i == 6)

break;

System.out.println(i);

Output:

continue statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.

public class ContinueExample

public static void main(String[] args)

for (int i = 0; i< 10; i++)

if (i == 4)

continue;

System.out.println(i);

Output:

You might also like