Skip to content

Control Flow Statements In Java Programming Language

In this article, you will be learning control flow statements used in java programming language.

Control flow statements are used to execute complex programs by breaking up the execution process and incorporating conditional statements.

These statements enable us to run a particular statement multiple numbers of times until it meets a specified condition.

Also Read how to Modifiers Used In Java Programming Language

TYPES OF CONTROL FLOW STATEMENTS

Basically, there are three types of control flow statements in java programming languagewhich are listed below.

  1. Loop Statements
  2. Decision-making Statements
  3. Branching/ Loop Control Statements

These statements are described below in detail with an example of each.

[1] LOOP STATEMENTS

(A) For loop Statement

In java programming language, for loop is used to perform an operation multiple numbers of times until a particular condition is satisfied.

Syntax:

for(initialization ; termination ; increment) {
Statement
}

Execution Flow:

  • Initialization- It means initializing the loop by assigning a variable with a value. It is a single time execution process that ends up with a semi-colon.
  • Termination- It represents a boolean expression. If the evaluation goes true, further execution will take place. If the evaluation goes false, the loop will be terminated. It also ends with a semi-colon.
  • Increment- This expression increments the value assigned to the variable until the termination of the process. It doesn’t end with a semi-colon.

Example:

class ForLoopExample {
public static void main(String args[]) {
for(int a = 8 ; a < 12 ; a++) {
System.out.println(“Value of a=” + a) ;
}
}
}

Output:

Value of a = 8
Value of a = 9
Value of a = 10
Value of a = 11

(B) While loop Statement

In java programming language, while loop performs the same task as ‘for loop’ do. However, in while loop, if the termination expression(for example, x < 10) is omitted, a compilation error will be generated. This is not in the case of ‘for loop’, it will execute the code infinitely instead.

Syntax: 

while(termination) {
Statement
}

Example:

class WhileLoop {
public static void main(String args[]) {
int b = 15 ;
while(b <= 18) {
System.out.println(“Value of b =” + b) ;
b++ ;
}
}
}

Output:

Value of b = 15
Value of b = 16
Value of b = 17
Value of b = 18

Infinite loop: 

An infinite loop using while statement can also be executed by putting a boolean value ‘true’ inside the termination expression instead of other conditional expression. Try it in your own using the below syntax.

while(true) {
Statement
}

(C) Do-while loop Statement

In a do-while loop, the ‘do’ block statement is executed first before being progressed to the boolean(termination) expression which is declared at the end of the loop. This makes the codes always run atleast for a single time.

Syntax: 

do {
Statement
}while(termination) ;

Example: 

class DoWhileLoop {
public static void main(String args[]) {
int x = 20 ;

do {
System.out.println(“Outcome =” + x) ;
x++ ;
}while(x < 25) ;
}
}

Output: 

Outcome = 20
Outcome = 21
Outcome = 22
Outcome = 23
Outcome = 24

(D) Enhanced For/ For-Each loop Statement

It is the enhanced form of for loop statement and performs the similar function. It uses a collection of numbers or simply arrays.

Syntax: 

int[ ] array = { 1, 2, 3………} ;
for (datatype variable : array) {

Example:

class EnhancedForLoop {
public static void main(String args[]) {
int[ ] numbers = { 10, 20, 30, 40, 50} ;
for (int figure : numbers) {
System.out.println(“Number is :” + figure) ;
}
}
}

Output:

Number is : 10
Number is : 20
Number is : 30
Number is : 40
Number is : 50

[2] DECISION-MAKING STATEMENTS

(A) If Statement

In an ‘if’ statement, the block of codes inside the statement will be executed if the expression goes true.

Syntax:

if (termination) {
Statement
}

Example:

class IfStatement {
public static void main(String args[]) {
int A = 20 ;
if ( A < 30 ) {
System.out.println(“Statement ‘if’ is true”) ;}
}
}

Output:

Statement ‘if’ is true

(B) If-else Statement

In this decision-making control flow statement, the block of codes(if) inside the statement1 will be executed if the expression goes true. And if the expression goes false, the block of codes(else) inside the statement2 will be executed.

Syntax:

if (termination) {
Statement1
} else {
Statement2
}

Example:

class IfElseStatement {
public static void main(String args[]) {
int A = 40 ;
if ( A >= 30 ) {
System.out.println(“Statement ‘if’ is true”) ;} else {
System.out.println(“Statement ‘else’ is true”);}
}
}

Output:

Statement ‘else’ is true

(C) If else-if else Statement

In this decision-making control flow statement, the ‘if’ block of code is followed by one or more ‘else if’ block of codes and an ‘else’ block of code in the end.

The program will be executed and terminated if the boolean expression in any of the ‘if’ and ‘else if’ statement goes true. But if all these expressions evaluate false, then the last statement ‘else’ will be executed.

Syntax:

if (termination1) {
Statement1
} else if (termination2) {
Statement2
} else if (termination3) {
Statement3
} else {
Statement4
}

Example:

class IfElseStatement {
public static void main(String args[]) {
int A = 30 ;
if ( A < 20 ) {
System.out.println(“Statement ‘if’ is true”) ;} else if ( A==30 ) {
System.out.println(“Statement ‘else if’ is true”);} else {
System.out.println(“Statement ‘else’ is true”);
}
}
}

Output:

Statement ‘else if’ is true

(D) Nested if Statement

In ‘nested if’ control flow statement, the ‘if’ or ‘else if’ statements can be used inside the other ‘if’ or ‘else if’ statement.

Syntax: 

if (termination1 ) {
Statement1
if (termination2 ) {
Statement2
}
}

Example:

class NestedIfStatement {
public static void main(String args[]) {
int A = 50 ;int B = 60 ;
if ( A == 50 ) {
System.out.println(” ‘if’ Statement1 is true and A = 50 “) ;if ( B == 60 ) {
System.out.println(” ‘if’ Statement2 is true and B = 60 “);}
}
}
}

Output:

‘if’ Statement1 is true and A = 50
‘if’ Statement2 is true and B = 60

 (E) Switch Statement

‘switch’ control flow statement consists of one or more case values and an optional ‘default’ case. The execution process is terminated when the value of the given variable matches the value of any of the declared cases.

It also consists an optional ‘break’ statement which is used to terminate the switch statement.

If a break statement is not used at the end of the corresponding block of codes, it will output the resulting case value along with a number of case values until it reaches a break statement. In simple words, it will go on executing the case values until it reaches a break statement.

If the value doesn’t match with any of the cases, then ‘default’ case is executed and the process is terminated. A break statement is not used with the default case.

Data types such as int, char, byte, short, string and enums can be used with switch statement.

Syntax: 

switch ( variable ) {

case value :
Statement
break ;

case value :
Statement
break ;

default ;
Statement
}

Example:

class SwitchStatement {
public static void main(String[] args) {

int day = 5 ;

switch (day) {
case 1 :
System.out.println(“Monday”) ;
break ;
case 2 :
System.out.println(“Tuesday”) ;
break ;
case 3 :
System.out.println(“Wednesday”) ;
break ;
case 4 :
System.out.println(“Thursday”) ;
break ;
case 5 :
System.out.println(“Friday”) ;
break ;
case 6 :
System.out.println(“Saturday”) ;
break ;
case 7 :
System.out.println(“Sunday”) ;
break ;
default :
System.out.println(“Invalid day”) ;
break ;
}
}
}

Output:

Friday

[3] BRANCHING/ LOOP CONTROL STATEMENTS

(A) Break Statement

In java programming language, a ‘break’ statement is used to terminate a loop(for, enhanced for, while and do while) and a case value in the switch statement. We have already shown you an example of ‘break’ in the switch statement above. Now, we will show you how to use it in a loop statement.

The use of break statement involves declaration of a value inside an ‘if’ statement to terminate the loop at that declared value.

Syntax:

if ( termination ) {
break ;
}

Example:

class BreakStatement {
public static void main(String args[]) {
for(int a = 8 ; a < 12 ; a++) {
if ( a == 10 ) {
break ;
}
System.out.println(“Value of a =” + a) ;}
}
}

Output:

Value of a = 8
Value of a = 9

 (B) Continue Statement

In java, a continue statement is used in loop control flows to bypass a value of the given variable. The value to be bypassed is first declared using an ‘if’ statement before applying the keyword ‘continue’ followed by a semicolon.

Syntax:

if ( expression ) {
continue ;
}

Example:

class ContinueStatement {
public static void main(String args[]) {
for(int x = 10 ; x <= 14 ; x++) {
if ( x == 12 ) {
continue ;
}
System.out.println(“Value of x =” + x) ;
}
}
}

Output:

Value of x =10
Value of x =11
Value of x =13
Value of x =14

Also Check : Operators Used In Java Programming Language

We have provided you the description on control flow statements used in java programming language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.

Facebook
Twitter
LinkedIn
Pinterest