Control Statements in Java
Control Statements in Java
Java provides us control structures, statements that can alter the flow of a sequence of instructions.
Compound statements
Compound statements or block is a group of statements which are separated by semicolons (;) and grouped together in a block enclosed in braces { and } is called a compound statement.
Example:
{ temp = a; a = b; b = temp; }
Types of control statements
Java supports two basic control statements.
- Selection statements
- Iteration statements
Selection statements
This statement allows us to select a statement or set of statements for execution based on some condition.
The different selection statements are:
- if statement
- if-else statement
- nested statement
- switch statement
if statement
This is the simplest form of if statement. This statement is also called as one-way branching. This statement is used to decide whether a statement or a set
of statements should be executed or not. The decision is based on a condition which can be evaluated to TRUE or FALSE.
Syntax:
if(expression) Statement;
Example
if–else statement
This statement is also called as two-way branching. It is used when there are alternative statements need to be executed based on the condition. It executes some set of statements when the given condition is TRUE and if the condition is FALSE then other set of statements to be executed.
Syntax:
if(expression) Statement; else Statement;
Example
Nested-if statement
An if statement may contain another if statement within it. Such an if statement is called as nested if statement.
Syntax:
if(condition1) Statement If(condition2) statement
Example:
if-else-if statement
This structure is also called as else-if ladder. This structure will be used to verify a range of values. This statement allows a choice to be made between different possible alternatives.
Syntax:
if(condition) Statement; else if (condition) Statement; . . else Statement;
Example
Switch statement
Java has a built-in multiple-branch selection statement, switch. This successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that code is executed.
Example:
Switch ( Expression ) | ||
{ | ||
Case Label-1: | Statement 1; | |
Break; | ||
Case Label-2: | Statement 1; | |
Break; | ||
………….. | ||
Case Label-N: | Statement N; | |
Break; | ||
Default | : | Default- Statement; |
Example:
Click Here-> Get Prepared for Interviews
Iteration statements
Iteration statements are also called as loops. Loop is a statement that allows repeated execution of a set of instructions until a certain condition is satisfied. This condition may be predefined or post-defined. Loops have a purpose to repeat a statement or set of statements a certain number of times or some condition is fulfilled.
- while loop
- do-while loop
- for loop
while loop
This looping structure is also called as
while loop
This looping structure is also called as pre-tested looping structure. This statement repeats the execution of a set of statements while the condition is TRUE.
Syntax:
while ( Test Condition) Statement;
Example:
c = 1; while(c <= 10) System.out.printl(“cvalue is”,+c);
do-while loop
This looping structure is also called a post-tested looping structure. Unlike while loop that tests the loop condition at the beginning, the do-while loop checks the condition after the execution of the statements in it. This means that a do-while loop always executes at least once. Its functionality is exactly the same as the while loop, except that the condition in the do-while loop is evaluated after the execution of the statement, instead of before.
Syntax:
do { Statement 1 Statement 2 …….. Statement N } while ( Test Condition);
Example:
c = 1; do { System.out.printl(“c value is”,+c); } while(c <= 10);
for loop
This statement is called as the fixed execution looping statement. It is normally used when we know in advance exactly how many times a set of statements should be repeatedly executed again and again. It provides initialization, loop-end-condition and increment/decrement process statements in a single line. When one is aware of fixed number of iterations, then this looping structure is best suited.
Syntax:
for ( Expression 1; Expression 2; Expression 3) Statement;
Example:
sum = 0; for ( i=1; i<=10; i++) sum = sum + i;
This statement repeats the execution of a set of statements while the condition is TRUE.
Syntax:
while ( Test Condition) Statement;
Example:
c = 1; while(c <= 10) System.out.printl(“cvalue is”,+c);
do-while loop
This looping structure is also called a post-tested looping structure. Unlike while loop that tests the loop condition at the beginning, the do-while loop checks the condition after the execution of the statements in it. This means that a do-while loop always executes at least once. Its functionality is exactly the same as the while loop, except that the condition in the do while loop is evaluated after the execution of the statement, instead of before.
Syntax:
do { Statement 1 Statement 2 …….. Statement N } while ( Test Condition);
Example:
c = 1; do { System.out.printl(“c value is”,+c); } while(c <= 10);
for loop
This statement is called as the fixed execution looping statement. It is normally used when we know in advance exactly how many times a set of statements should be repeatedly executed again and again. It provides initialization, loop-end-condition and increment/decrement process statements in a single line. When one is aware of fixed number of iterations, then this looping structure is best suited.
Syntax:
for ( Expression 1; Expression 2; Expression 3) Statement;
Example:
sum = 0; for ( i=1; i<=10; i++) sum = sum + i;
Jump statements or Transfer of control from within loop
Transfer of control within looping are used to
- Terminate the execution of loop
- Exiting a loop
- Halfway through to skip the loop.
All these can be done by using break, exit, and continue statements.
break – stop what you’re doing and continue after the current loop
continue – stop what you’re doing and continue with the next loop cycle
return – stop what you’re doing and go back to calling method
throw – stop what you’re doing and generate a run-time exception
Example
for break:for (n=0; n<100; n++) { cout<<n; if(n==10) break; }
Example program to continue:
Example program for Return: class Return
Example:
‘A’
There is another class of characters which cannot be displayed on the screen (non-graphic characters) but they have special meaning and are used for special purpose. Such character constants are called as escape sequences.
Example:
\’ \” \\ \0 \a
Click Here-> Get Java Training with Real-time Projects