Java Flow Control - If-Else

Advertisements

Java Flow Control

Flow control statements are used, when we have to take decision, depending upon certain condition. Syntax of flow control statements are similar with C / C++ language.

Types of Flow Control Statements in java

  • if Statement
  • if...else
  • if...else Ladder
  • Nested if...else

if Statement

if statement takes condition in parenthesis and a block of statements within braces. If condition is true, it will return non-zero value, and statements given in if block will get execute.

Syntax of if statement


	if(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

if...else Statement

if statement takes condition in parenthesis and a block of statements within braces. If condition is true, it will return non-zero value, and statements given in if block will get execute. If condition is false, it will returns zero, and statements given in else block will get execute.

Syntax of if-else statement


	if(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}
	else
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

Example of if-else statement


	//IfElseDemo.java

	import java.util.Scanner;

	class IfElseDemo
	{
		public static void main(String args[])
		{
			int num;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter any number : ");
			num = Integer.parseInt(sc.nextLine());

			if(num>0)
				System.out.print("\n\t" + num + " is Positive.");
			else
				System.out.print("\n\t" + num + " is Negative.");
		}
	}

	Output :

			Enter any number : 24
			24 is Positive

if...else Ladder

if...else ladder is used for checking multiple conditions, if the first condition will not satisfy, compiler will jump to else block and check the next condition, whether it is true or not and so on.

Syntax of if-else ladder statement


	if(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}
	else if(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}
	else if(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}
	else
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

Example of if-else ladder statement


	//IfElseLadderDemo.java

	import java.util.Scanner;

	class IfElseLadderDemo
	{
		public static void main(String args[])
		{
			int amt,discount;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter amount : ");
			amt = Integer.parseInt(sc.nextLine());

			if(amt>20000)
				discount=15;

			else if(amt>15000)
				discount=10;

			else if(amt>10000)
				discount=5;

			else
				discount=0;

			System.out.print("\n\tYou will get " + discount + "% discount.");
		}
	}

	Output :

			Enter amount : 14000
			You will get 5% discount.

Nsted if...else

In nested if...else, one if...else statement contains another if...else statement.

Syntax of nested if-else statement


	if(condition)
	{
		if(condition)
		else
	}
	else
	{
		if(condition)
		else
	}

Example of nested if-else statement


	//NestedIfElseDemo.java

	import java.util.Scanner;

	class NestedIfElseDemo
	{
		public static void main(String args[])
		{
			int a,b,c;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter value of A : ");
			a = Integer.parseInt(sc.nextLine());

			System.out.print("\n\tEnter value of B : ");
			b = Integer.parseInt(sc.nextLine());

			System.out.print("\n\tEnter value of C : ");
			c = Integer.parseInt(sc.nextLine());

			if(a>b)
			{
				if(a>c)
					System.out.print("\n\tA is Greatest");
				else
					System.out.print("\n\tC is Greatest");
			}
			else
			{
				if(b>c)
					System.out.print("\n\tB is Greatest");
				else
					System.out.print("\n\tC is Greatest");
			}
		}
	}

	Output :

	Enter value of A : 45
	Enter value of B : 89
	Enter value of C : 78

	B is Greatest

Advertisement