Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA PROGRAMMING

Selection Statements (if, switch)

Using  if-else 

The if-else statement is probably the most basic way to control program flow. The else is optional, so you can use if in two forms:

if(Boolean-expression)
statement
or
if(Boolean-expression)
statement
else
statement

The conditional must produce a Boolean result. The statement means either a simple statement terminated by a semicolon or a compound statement, which is a group of simple statements enclosed in braces. Anytime the word "statement" is used, it always implies that the statement can be simple or compound. 

Sample program using if





Using switch statement

Use the switch statement to conditionally perform statements based on an integer expression.

syntax of switch:

switch (expression)

{

case value1: 

statement sequence

break;

case value2: 

statement sequence

break;

default :

default statement sequence

}

An example code using switch






Previous

Next