i

JAVA Complete Course

Control Statements

In Java we generally have three main types of control statements: Conditional, Looping and Unconditional.

Conditional control statements allows program to choose an alternative way according to some condition. There are two different terms for such statements: decision-making statements and selection statements.

One of the example of conditional control statement is if statement, which looks like this:

 

But we also might want to have statement which chooses one between two alternatives, for example we want to print “EVEN” if number is even and “ODD” if it is odd. For this we should use if-else statement, which looks like this:

In else block we can have additional conditional statement and it is called if else-if statement

There is another conditional statement switch which allows us to choose a way between multiple statements. Switch helps us to avoid multiple nested if-else blocks. Below is the syntax of switch statement.

There are few points we should know about switch statement:

  • Based on the switch statement’s argument, proper case value will be selected and then executed.

  • If there is no case which matches argument of switch, then the default case will be executed.

  • It is up to developer to write a break statement as a last command of each case statement. But if there is no break at the end of each statement and it somehow will happen that next statement also applies, both statements will run together.