Control Flow Statements in Java

In this Java article we want to learn about Control Flow Statements in Java, Control flow statements are fundamental aspect of any programming language. they are used to control the flow of execution of program. in Java there are several types of control flow statements that you can use to control execution flow of your program. in this article we want to talk about these statements and how they are used.

 

 

Types of Control Flow Statements

There are three types of control flow statements in Java:

decision-making
looping
branching

 

 

Decision-making statements are used to make decisions based on certain conditions. there are two types of decision-making statements in Java.

if statements
switch statements

 

 

Looping statements are used to execute a block of code repeatedly. there are three types of looping statements in Java: 

for loops
while loops
do-while loops

 

 

Branching statements are used to transfer control of a program to different part of the program. there are two types of branching statements in Java:

break statements
continue statements

 

 

If Statements

if statement is used to make a decision based on certain condition. if the condition is true, then the code in the if statement is executed. if the condition is false, then the code in the if statement is skipped.

In this example condition is x < 10. since x is less than 10, the code within the if statement is executed, which prints out x is less than 10.

 

 

This is the result

Control Flow Statements in Java
Control Flow Statements in Java

 

 

Switch Statements

switch statement is used to make a decision based on a certain value. switch statement evaluates the value of an expression and then executes the code associated with the matching case.

In this example the value of day is 4. the code within the case labeled 4 is executed, which sets dayName to Thursday. break statement is used to exit the switch statement once a case has been matched.

 

 

For Loops

for loop is used to execute a block of code repeatedly for fixed number of times. the syntax of  for loop is like this:

The initialization statement is executed once before the loop starts. condition is checked before each iteration of the loop. if the condition is true, then the code within the loop is executed. also update statement is executed after each iteration of the loop.

 

 

This is an example of for loop.

In this example the loop will execute 10 times. i variable is initialized to 0, and the loop will continue to execute as long as i is less than 10.

 

 

This will be the result

Control Flow Statements in Java
Control Flow Statements in Java

 

 

While Loops

while loop is used to execute a block of code repeatedly as long as certain condition is true. syntax of a while loop is like this, the code within the loop will continue to execute as long as the condition is true.

 

 

This is the example of while loop in java

In this example the loop will execute 10 times. the code within the loop will continue to execute as long as i is less than 10. i++ statement is used to increment the value of i after each iteration of the loop.

 

 

Break Statements

break statement is used to exit a loop or switch statement before the loop or switch statement has completed its full execution. syntax of a break statement is like this:

 

 

This is the complete code within for loop

In this example the loop will execute 5 times. when i is equal to 5, break statement is executed which exits the loop before it has completed its full execution.

 

 

This will be the result

Java Break Statement
Java Break Statement

 

 

Continue Statements

continue statement is used to skip over certain iteration of a loop. syntax of a continue statement is like this:

 

 

This is an example of a continue statement within  for loop:

In this example the loop will execute 10 times, but the number 5 will be skipped over. when i is equal to 5, continue statement is executed, which skips over the code within the loop for that iteration.

 

 

So we can say that In Java control flow statements are used to control the flow of execution of a program. there are several types of control flow statements, including decision-making statements, looping statements and branching statements. 

 

 

Learn More

Leave a Comment