Control Statements

 

    A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program.it can be put into the following categories:

·         Selection

·         iteration, and

·         jump

Java’s Selection Statements

Java supports two selection statements:

·         if and

·         switch

These statements allow you to control the flow of our program’s execution based upon conditions known only during run time.

If statement

The if statement is Java’s conditional branch statement

Syntax:

if (condition) statement1;

else statement2;

Nested ifs

                A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else

The if-else-if Ladder

Sequence of nested ifs is the if-else-if ladder.

Syntax:

if(condition)               

statement;

 else if(condition)       

 statement;

else if(condition)        

statement;

. . .

. . .

 

else

Statement;

 

The if statements are executed from the top down. 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 is bypassed.   If none of the conditions is true, then the final else statement will be executed.

 

Switch

The switch statement is multiway branch statement.It provides an easy way to dispatch execution to different parts of our code based on the value of an expression

Syntax:

switch (expression)

{

case value1:

// statement sequence break;

case value2:

                        // statement sequence break;

. . .

. . .

case valueN

// statement sequence break;

default:

                        // default statement sequence

 }

Working of switch

                The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken

 

Iteration Statements

Java’s iteration statements are

·         for,

·         while, and

·         do-while

commonly call loops

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

while

The while loop is Java’s most fundamental loop statement. It is entry control loop

Syntax:

while(condition)

{

            // body of loop

 }

 

do-while

The do-while loop always executes its body at least once as its conditional expression is at the bottom of the loop.It is exit control loop

Syntax:

do

 {

// body of loop

}

 while (condition);

 

For

It is a powerful and versatile construct.there are two forms of the for loop.

Traditional form of for loop:

Syntax:

for(initialization; condition; iteration)

{

// body

 }

The For-Each Version of the for Loop

Also referred to as the enhanced for loop.

Syntax:

for(type itr-var : collection) statement-block

Here, type specifies the type , itr-var specifies the name of an iteration variable that will receive the elements from a collection, one at a time, from beginning to end

 

PROGRAM








OUTPUT

    Value  is :  1

    Value  is :  2

    Value  is :  3

    Value  is :  4

    Value  is :  5

    Value  is :  6

    Value  is :  7

    Value  is :  8

    Value  is :  9

    Value  is :  10

    Summation : 55

Jump Statements

Java supports three jump statements:

·         break,

·         continue, and

·         return.

These statements transfer control to another part of our program

 

Using break

·         It terminates a statement sequence in a switch statement

·         it can be used to exit a loop

 

Using break to Exit a Loop

 









No comments:

Post a Comment