Controls - Java

Everything you need to know about Control statements - Java

Controls - Java

This article includes the control statements in Java programming. This whole article would be the one-stop solution for you to understand Control statements in Java programming. We cover all the basic concepts, algorithm development for control statements, and exceptions and also solve a few quiz questions and coding questions to solve.

If Statement:

It tells the computer to take one of two alternative cases in action to execute an instruction. For example, you are going to code for a program, which inputs the user's age, and based on that you need to return a statement that informs whether the user can cast vote or not.

Syntax:

if ( condition ) {

<statement>

}

Note: Usually, it's not required to use the brackets after the condition when we have only one statement in the whole block.

Here, the condition would be that, concerning our first example, the user age should not be under eighteen years old.

Example:

int age = 19;

if ( age<= 18){ System.out.println(" You are eligible to cast your vote! ");

}

If we run the program, we get the output as " You are eligible to cast your vote!

Let's take another case if the user enters his/her age that's less than 18, then the above program does not display anything. It makes users get confused about eligibility. So, to avoid that, we can use the if else statement.

If else Statement:

Let's assume the same example, and try to code for that case.

int age = 17;

if ( age<= 18){ System.out.println(" You are eligible to cast your vote! ");

else {

System.out.println(" You are not eligible to cast your vote! "):

}

}

The output for this could be - You are not eligible to cast your vote!

Here, the else statement gets executed, when the " if " statement of code doesn't work or doesn't satisfy the condition.

Let's take case 2, in case we have to add another condition, that could be the different from if and the else statements, we can include else if.

else if:

Let us take the same example, if the user is greater than 90 years, then they are not allowed to cast their vote. So, we can utilize this else if statement as

int age = 91;

if ( age<= 18){ System.out.println(" You are eligible to cast your vote! ");

}

else if ( age >=90){ System.out.println(" You are not eligible to cast your vote! " ):

}

else {

System.out.println(" You are not eligible to cast your vote! "):

}

}

The output of the program would be - You are not eligible to cast your vote!

In this way, the conditional statements - if, else, else if can be used. Now, what if we need to check multiple conditions if a condition gets true? In simple language, we call it a " Nested conditional statement ".

Nested Conditional Statements:

Nested If:

Checking for a condition in a condition is called nested if. The syntax for nested if looks like,

if ( condition ) {

<statement>

if ( condition ) {

<Statement>

if ( condition ) {

<Statement>

} }

}

This is all about this blog, in the next upcoming blog, we'll revise or discuss the Loops and Switch statements.

Did you find this article valuable?

Support Manjunath Irukulla by becoming a sponsor. Any amount is appreciated!