What is flow control in Java? - onlyxcodes

Saturday 17 August 2024

What is flow control in Java?

Hi, In this tutorial, I will explain flow control in Java. The sequence in which specific statements, commands, or function calls are carried out or assessed within a program is called flow control in Java.


Essentially, flow control sets the duration for a program's execution from beginning to end. Composing Java programs that are both effective and efficient requires a solid understanding of flow control.


what is flow control in java

A program's flow can be managed using several Java constructs, which are generally divided into:


1. Sequential Flow


2. Conditional (Branching) Flow


3. Iterative (Looping) Flow


4. Jump Statements


1. Sequential Flow

Java's default style of execution, known as sequential flow, involves running statements one after the other in the order that they appear in the program. There is no decision-making or looping involved in this simple flow.


In this example, each statement is executed sequentially, from the declaration of variables a and b to calculate their sum, and finally, printing the result.


public class Test 
{
    public static void main(String[] args) 
	{
        int a = 5;
		int b = 10;
		int sum = a + b;
		System.out.println("Sum: " + sum);

    }
}

Output:


Sum: 15

2. Conditional (Branching) Flow

Within a program, conditional flow control refers to the process of making decisions based on specific circumstances. Conditional flow can be handled in Java in a few different ways, mostly using the if-else, switch, and if statements.


if-else Statement


The program can run specific code segments based on whether a condition is true or false thanks to the if-else statement.


Syntax:


if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Example:


public class Test 
{
    public static void main(String[] args) 
	{
        int number = 10;
		
		if (number > 0) 
		{
			System.out.println("Positive number");
		} 
		else 
		{
			System.out.println("Negative number");
		}
    }
}

Output:


Positive number

else if Ladder


When multiple conditions need to be checked, an else if ladder can be used. It allows you to test several conditions in sequence.


Example:


public class Test 
{
    public static void main(String[] args) 
	{
        int number = 0;
		
		if (number > 0) 
		{
			System.out.println("Positive number");
		} 
		else if (number < 0) 
		{
			System.out.println("Negative number");
		} 
		else 
		{
			System.out.println("Zero");
		}

    }
}

Output:


Zero

switch-case Statement


Another method for managing conditional flow, particularly in cases where a variable can have more than one possible value is to use a switch statement. When testing the same variable against numerous constants, it is more effective and legible than a long else if ladder.


Syntax:

switch (expression) {
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if none of the cases are true
}

Example:


public class Test 
{
    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("Other day");
		}

    }
}

Output:


Wednesday

3. Iterative (Looping) Flow

Iterative flow control entails rerunning a code block several times. Java has a variety of loop constructs, such as while, do-while, and for loops.


for Loop


The for loop is used when the number of iterations is known before entering the loop.


Syntax:


for (initialization; condition; update) {
    // code to be executed
}

Example:


public class Test 
{
    public static void main(String[] args) 
	{
		for (int i = 0; i < 3; i++) 
		{
			System.out.println("Iteration " + i);
		}
    }
}

Output:


Iteration 0
Iteration 1
Iteration 2

while Loop


The while loop is used when the number of iterations is not known beforehand and the loop continues as long as the condition is true.


Syntax:


while (condition) {
    // code to be executed
}

Example:


public class Test 
{
    public static void main(String[] args) 
	{
		int i = 0;
		while (i < 3) 
		{
			System.out.println("Iteration " + i);
			i++;
		}
    }
}

Output:


Iteration 0
Iteration 1
Iteration 2

do-while Loop


The do-while loop is similar to the while loop, but it guarantees that the loop body is executed at least once, as the condition is checked after the loop body is executed.


Syntax:


do {
    // code to be executed
} while (condition);

Example:


public class Test 
{
    public static void main(String[] args) 
	{
		int i = 0;
		
		do {
			System.out.println("Iteration " + i);
			i++;
		} while (i < 3);
    }
}

Output:


Iteration 0
Iteration 1
Iteration 2

4. Jump Statements

Jump statements move control to another area of the program, changing the typical flow of control. There are multiple jump statements available in Java, such as break, continue, and return.


break Statement


The break statement is used to exit a loop or a switch statement immediately, regardless of the loop's condition or remaining iterations.


Example:


public class Test 
{
    public static void main(String[] args) 
	{
		for (int i = 0; i < 10; i++) 
		{
			if (i == 5) 
			{
				break;  // exit loop when i equals 5
			}
			System.out.println("Iteration " + i);
		}
    }
}

Output:


Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

continue Statement


The continue statement skips the current iteration of a loop and continues with the next iteration.


Example:


public class Test 
{
    public static void main(String[] args) 
	{
		for (int i = 0; i < 10; i++) 
		{
			if (i % 2 == 0) 
			{
				continue;  // skip even numbers
			}
			
			System.out.println("Odd number: " + i);
		}
    }
}

Output:


Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

return Statement


The return statement exits from the current method and returns control to the calling method. If the method has a return type, the return statement should return a value of that type.


Example:


public class Test 
{
	public int addNumbers(int a, int b) 
	{
		return a + b;  // return the sum of a and b
	}

	
    public static void main(String[] args) 
	{
		Test obj = new Test();
		int i = obj.addNumbers(10,5);
		System.out.println("Sum is: " + i);	
    }
}

Output:


Sum is: 15

No comments:

Post a Comment

Post Bottom Ad