Java Control Statements

Posted: June 10, 2012 by bhumika289 in JAVA

Selection Statements

1. if

2.switch

Allow you to control the flow of your program’s execution based upon conditions during run time.

1. if :

  • It is a conditional branch statement.
  • Can be used to route program execution through two different paths.

Syntax :

if (condition)

{

statements;

}

  • When the condition is true the Statement within the if is executed and execution continues with the next statement after the if statement.
  • If the condition is false then the statement within the if is not executed and the execution continues with the statement after the if statement.
  • If the number of statement in if block is only one then parentheses are optional but if its more than one than parentheses are compulsory.

if else :

Syntax :

if (condition)

{

statement1;

}

else

{

statement2;

}

  • If the condition is true, then statement1 is executed.
  • Otherwise, statement2 is executed.

if-else-if Ladder :

Syntax :

if(condition)

statements;

else if(condition)

statements;

else if(condition)

statements;

else

statements;

  • As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder statements are bypassed.
  • If none of the conditions is true, then the final else statement will be executed.

Nested if :

A nested if is an if statement that is the target of another if or else.

Syntax :

if(condition)

{

if(condition)

statements….

else

statements….

}

else

{

if(condition)

statements….

else

statements….

}

2. switch :

  • Multiway branch statement.
  • Provides an easy way to dispatch execution to different parts of your code based on the value of an expression.

Syntax :

switch (expression)

{

case value1 :

statement1 ;

break;

case value2 :

statement2 ;

break;

case valuen :

statementn ;

break;

default :

statements ;

break;

}

  • The expression must be of type byte, short, int, or char and each of the values specified in the case statements must be of a type compatible with the expression.
  • Each case value must be a unique literal.
  • The value of the expression is compared with each ‘case’ values. If a match is found, the corresponding statement or statements are executed.
  • If no match is found, statement or statements in the default case are executed. Default statement is optional.
  • If default statement is absent, then if no matches are found, then the switch statement completes without doing anything.
  • The break statement is used inside the switch to terminate a statement sequence.

Looping Statements

1. for

2. while

3. do while

A loop repeatedly executes the same set of instructions until a termination condition is met.

1. for :

  • Repeats a set of statements a certain number of times until a condition is matched.
  • Entry controlled loop, as condition is checked while entering the loop.

Syntax :

for (initialization; condition; expression)

{

Set of statements;

}

  • In the first part a variable is initialized to a value.
  • The second part consists of a test condition that returns only a Boolean value.
  • The last part is an expression, evaluated every time the loop is executed.

2. while :

  • Executes a set of code repeatedly until the condition returns false.
  • Entry-controlled loop.

Syntax :

while (condition)

{

body(statements) of the loop

}

  • <condition> is the condition to be tested.
  • If the condition returns true then the statements inside the <body of the loop> are executed.
  • Else, the loop is not executed.

3. do while :

  • Similar to the while loop except that the condition to be evaluated is given at the end.
  • Hence the loop is executed at least once even when the condition is false.
  • Exit-controlled loop.

Syntax :

do

{

body of the loop

} while (condition);

In do while loop semicolon(;) is compulsory after while.

Jump Statements

1. break

2. continue

3. return

These statements transfer control to another part of your program.

1. break:

  • This statement is used to jump out of a loop.
  • On encountering a break statement within a loop, the execution continues with the  next statement outside the loop and the remaining statements which are after the break and within the loop are skipped.
  • Break statement can also be used with the label of a statement.
  • A statement can be labeled as follows.

statementName : SomeJavaStatement

  • When we use break statement along with label the execution continues with the statement having the label.

break statementName;

2. Continue:

  • Used only within looping statements.
  • When the continue statement is encountered, the next iteration starts.
  •  The remaining statements in the loop are skipped. The execution starts from the top of loop again.

3. return:

  • Used to explicitly return from a method.
  • Causes program control to transfer back to the caller of the method.
  • Immediately terminates the method in which it is executed.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s