HudaTutorials.com

Java Control Flow Statements

Last updated on

Control Flow Statements in Java

It is sometimes necessary to perform repeated actions or skip some statements in a program. For these actions certain control statements are available. These statements control the flow of execution of the programs. The statements inside Java code are executed from top to bottom, in the order that they appear. Java control flow statements, change or break the flow of execution by implementing decision making statements, looping statements, and branching statements in your program to execute particular blocks of code based on the conditions. In this tutorial you can learn about the decision making statements (if, if - else, switch) supported by the Java programming language.

Decision Making Statements in Java

  1. Java if Statement
  2. Java if else Statement
  3. Java switch Statement

branching statement in Java

Java programming language provides following types of decision making or branching statements in Java . Java programming language provided decision making statements or branching statements as follows .

Java if Statement

An if statement consists of a boolean expression followed by one or more statements .

Java if else Statement

An if statement can be followed by an optional else statement , which executes when the boolean expression is false .

Java Nested if Statement

You can use one if or else if statement inside another if or else if statements .

Java switch Statement

A switch statement allows a variable to be tested for equality against a list of values.

Java if Statement

The if statement is the most basic of all the control flow statements in Java. It tells your program to execute a certain section of code only if a particular test condition evaluates to true. In Java, the opening and closing braces are optional. But this is applicable only if the block of code to be execute is just a single statement. It is recommended to put the brackets around the statements, even if there is only one statement to execute. Why because, in the beginning you may start with one statement and later during the development phase you may add more statements. In the following Java if statement example you can learn how to use if statement in Java.

Java if Statement Syntax :

if( < condition > )
{
	Statement 1;
	Statement 2;
	Statement n;
}

Java if Statement Example

/*  Java if Statement Example
    Save with file name IfStatement.java */
public class IfStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println("Java if Statement Example ");
		if(a > b)
			System.out.println("a is greater than b");
		System.out.println("Greater Value"); // OUT PUT STATEMENT
		if(a < b)
			System.out.println("a is less than b"); // OUT PUT STATEMENT
		System.out.println("Lesser Value"); // OUT PUT STATEMENT
		System.out.println("------- ANOTHER WAY -------");
		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		if(a < b)
		{
			System.out.println("a is less than b"); // OUT PUT STATEMENT
			System.out.println("Lesser Value"); // OUT PUT STATEMENT
		}
	}
}

Java if Statement Example 2

/*  Java if Statement Example 2
    Save with file name IfStatement.java    */
public class IfStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println("Java if Statement Example ");
		if(a > b)
			System.out.println("a is greater than b");
		System.out.println("Greater Value"); // OUT PUT STATEMENT
		if(a < b)
			System.out.println("a is less than b"); // OUT PUT STATEMENT
		System.out.println("Lesser Value"); // OUT PUT STATEMENT
		System.out.println("------- ANOTHER WAY -------");
		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		if(a < b)
		{
			System.out.println("a is less than b"); // OUT PUT STATEMENT
			System.out.println("Lesser Value"); // OUT PUT STATEMENT
		}
	}
}

Java if else Statement

The if else statement provides a secondary path of execution when an "if" clause evaluates to false. In other words if a particular test condition evaluates to false then else block will be executed. In the following Java if else statement example you can learn how to use if else statement in Java.

Java if else Statement Syntax :

if( < condition > )
{
	Statement 1;
	Statement 2;
	Statement n;
}
else
{
	Statement 1;
	Statement 2;
	Statement n;
}

Java if else Statement Example

/*  Java if else Statement Example
    Save with file name IfElseStatement.java  */
public class IfElseStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println("Java if Else Statement Example ");
		if(a > b)
			System.out.println("a is greater than b");
		else
			System.out.println("a is less than b"); // OUT PUT STATEMENT
		System.out.println("------- ANOTHER WAY -------");
		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		else
		{
			System.out.println("a is less than b"); // OUT PUT STATEMENT
			System.out.println("Lesser Value"); // OUT PUT STATEMENT
		}
	}
}

Java switch Statement

Unlike if and if - else statements, the switch statement can have a number of possible execution paths. The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression within the brackets, then executes all statements that follow the matching case label. break statement is necessary in switch statement. Without break statement, statements in switch blocks fall through. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Java switch Statement Syntax:

switch( < value > )
{
	case 1:
	{
	}
	case 2:
	{
	}
	case n:
	{
	}
	default case:
	{
	}
}

Java int switch Statement Example

/*  Java int switch Statement Example
    Save with file name IntSwitchStatement.java */
public class IntSwitchStatement
{
	public static void main(String args[])
	{
		int a = 2, b = 40;
		System.out.println("Java int Switch Statement Example ");
		// int value parameter single statement in case
		switch(a)
		{
			case 1:
				System.out.println("a value is One");
			case 2:
				System.out.println("a value is Two");
			default:
				System.out.println("a value is Default Value");
		}
		// int value parameter Multiple statements in case
		switch(a)
		{
			case 1:
			{
				System.out.println("Case 1 executed");
				System.out.println("a value is One");
				break;
			}
			case 2:
			{
				System.out.println("Case 2 executed");
				System.out.println("a value is Two");
				break;
			}
			default:
			{
				System.out.println("Default Case executed");
				System.out.println("a value is Default Value");
				break; // NOT REQUIRE BECAUSE LAST STATEMENT
			}
		}
	}
}

Java char switch Statement Example

/*  Java char switch Statement Example
    Save with file name CharSwitchStatement.java    */
public class CharSwitchStatement
{
	public static void main(String args[])
	{
		char c = 'b'; // char DATA TYPE DECLARATION AND ASSIGN A VALUE
		System.out.println("Java char Switch Statement Example ");
		// char value parameter single statement in case
		switch(c)
		{
			case 'a':
				System.out.println("case a");
			case 'b':
				System.out.println("case b");
			case 'c':
				System.out.println("case c");
			default:
				System.out.println("default case");
		}
		// char value parameter Multiple statements in case
		switch(c)
		{
			case 'a':
			{
				System.out.println("Case a");
				break;
			}
			case 'b':
			{
				System.out.println("Case b");
				break;
			}
			case 'c':
			{
				System.out.println("Case c");
				System.out.println("a value is Two");
				break;
			}
			default:
			{
				System.out.println("Default Case");
				break; // NOT REQUIRE BECAUSE LAST STATEMENT
			}
		}
	}
}