Archive for Java

Java Control Statements

Tuesday, May 17th, 2011

By: Pankaj Yadav

Java Control statements in a programming language are very useful as they allow a programmer to change the flow of program execution i.e. altering the normal program flow to jump directly on some statement(s) or to skip a statement(s). In Java control statements are divided into following 3 categories:

Selection/Decision making Statements

Using these statements, a piece of code would be executed only if a certain condition(s) is true. These are of 3 types:

1. if

Statement(s) between the set of curly braces ‘{ }’ will be executed only if the condition(s), between the set of brackets ‘( )’ after ‘if’ keyword, is/are true.

Syntax:

if (Condition) {
	// statements;
}

2. if-else

If the condition(s) between the brackets ‘( )’ after the ‘if’ keyword is/are true then the statement(s) between the immediately following set of curly braces ‘{ }’ will be executed else the statement(s) under, the set of curly braces after the ‘else’ keyword will be executed.

Syntax:

if (condition) {
	// statements;
} else {
	// statements;
}

3. switch

When there is a long list of cases & conditions, then if/if-else is not good choice as the code would become complicated.

Syntax:


		switch (expression)
 		{
			case value1:
			//statement;
			break;
			case value2:
			//statement;
			break;
			default:
			 //statement;
		}

In the above piece of code, the user’s choice (add/sub/mul) will be stored in variable ‘ch’. The moment user enters his choice, it will be matched with the cases’ names & program execution will jump to the matching ‘Case’ & the statement under that case will be executed till the keyword ‘break’ comes. It is very important else the other unwanted cases will also get executed. After the last case there is ‘default’ keyword. Statements between ‘default:’ and the closing bracket of switch-case region will be executed only if the user has entered any wrong value as his choice i.e. other than the cases’ names.

Loop/ Iteration Statements

4. while

while statement continually executes a block of statements while a particular condition is true. Entry controlled

Syntax:

while(conditions)
{
//Loop body
}

5. do-while

It will enter the loop without checking the condition first and checks the condition after the execution of the statements. That is it will execute the statement once and then it will evaluate the result according to the condition. Exit controlled

Syntax:

do
{
//Loop body
}while(condition);

6. for

The concept of Iteration has made our life much easier. Repetition of similar tasks is what Iteration is and that too without making any errors. Until now we have learnt how to use selection statements to perform repetition.

Syntax:

for(initialization;test condition;increment)
{
//Loop body
}

Branching/ Transfer statements

7. break

Sometimes we use Jumping Statements in Java. Using for, while and do-while loops is not always the right idea to use because they are cumbersome to read. . Break statement skips the following code lets the execution jump to point after, where the program execution has jumped from to switch-case region.

Syntax:

		for(initialization;test condition;increment)
		{
			if(condition)
			{
			//statements
			break;
			}
		}

8. continue

Continue statement is just similar to the break statement in the way that a break statement is used to pass program control immediately after the end of a loop and the continue statement is used to force program control back to the top of a loop.

Example

		for(initialization;test condition;increment)
		{
			if(condition)
			{
			//statements
			continue;
			}
		}