X

Java Control Flow Statements

Control flow statements in Java allow you to run or skip blocks of code when special conditions are met. You will use control statements a lot in your programs and this tutorial will explain how to do this.

The “if” Statement

The “if” statement in Java works exactly like in most programming languages. With the help of “if” you can choose to execute a specific block of code when a predefined condition is met. The structure of the “if” statement in Java looks like this:

if (condition) {
	// execute this code
}

The condition is Boolean. Boolean means it may be true or false. For example you may put a mathematical equation as condition.  Look at this full example:

public class FlowControlExample {
	public static void main(String[] args) {
		int age = 2;
		System.out.println("Peter is " + age + " years old");

		if (age < 4) {
			System.out.println("Peter is a baby");
		}
	}
}

The output is:

Peter is 2 years old
Peter is a baby

In the example above we check if the age is lower than 4. As the age is set to be 2, the boolean condition 2 < 4 is true and as result we print “Peter is a baby”. If we change the age to any value greater than 3 the code in the block wont execute anymore and “Peter is a baby” is not printed.



Comparison Operators in Java

Use this operator to create boolean results

<     less than

<=    less than or equal to

>     greater than

>=    greater than or equal to

==    equal to

!=     not equal to

Conditional Operators in Java

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions.

int a = 2;
int b = 2;
int c = 5;
	
if (a == 2 && b == 2) {
	System.out.println("A and B are equeal to 2");
}

if (a == 5 || c == 5) {
	System.out.println("A or C is equal to 5");
}

the result is

A and B are equeal to 2
A or C is equal 5

The “if else” Statement

Whit this statement you can control what to do if the condition is met and what to do otherwise. Look at the code below

public class FlowControlExample {
	public static void main(String[] args) {
		int age = 10;
		System.out.println("Peter is " + age + " years old");

		if (age < 4) {
			System.out.println("Peter is a baby");
		} else {
			System.out.println("Peter is not a baby anymore");
		}
	}
}

The result is

Peter is 10 years old
Peter is not a baby anymore

Because the value we give to age is greater then 3 the else statement is executed

I will show you one more example with “if else” statement and conditional operators

public class FlowControlExample {
	public static void main(String[] args) {
		int age = 14;
		System.out.println("Peter is " + age + " years old");

		if (age < 4) {
			System.out.println("Peter is a baby");
		} else if (age >= 4 && age < 14) {
			System.out.println("Peter is a child");
		} else if (age >= 14 && age < 18) {
			System.out.println("Peter is a teenager");
		} else if (age >= 18 && age < 68) {
			System.out.println("Peter is adult");
		} else {
			System.out.println("Peter is an old men");
		}
	}
}

The switch Statement

In some cases you can avoid using multiple if-s in your code and make your code look better. To achieve this you can use the switch statement. Look at the following java switch example

public class SwitchExample {
	public static void main(String[] args) {
		int numOfAngles = 3;
		
		switch (numOfAngles) {
		case 3:
			System.out.println("triangle");
			break;
		case 4:
			System.out.println("rectangle");
			break;
		case 5:
			System.out.println("pentagon");
			break;
		default:
			System.out.println("Unknown shape");
		}
	}
}

The switch has a key and one ore more cases. In our example the key is numOfAngles and we handle the ceases when we give 3, 4 and 5 as values to the switch statement. If we pass a different value than 3, 4 or 5 default will be executed. Also note the break at the end of each case. If we don’t include break the program will run to the next case. For example if we remove the break in case 3, case 3 and case 4 will be executed in the example above.

In my next tutorial Java Loops I will explain how to use loops in Java.

5 1 vote
Article Rating
filip:
Related Post