Switch , Else–if, if–else, if, Goto and label statement

Switch , Else–if, if–else, if, Goto and label statement

Switch , Else–if, if–else, if, Goto and label statement
To view other topics related to Computer. Click Here.

Switch statement: -

The switch statement is also called the conditional statement like else – if statement. But by this statement we can take multi values with our condition.

Its syntax is:

Switch (condition)

{

Case value 1:

Statement 1;

Break;

Case value 2:

Statement 2;

Break;

Case value 3 :

Statement 3;

Break;

Default:

Statement 4;

}

In this structure first of all value of condition is checked, if it methods with case value 1 then statement is executed. If it method with case value 2, then statement 2 is executed. If the method with case value 3, then statement 3 is executed otherwise in false situation, default statement is executed.

The else – if statement: -

This is also a conditional statement like a “if – else” statement, but in this statement there is also a part of else – if statement.

Its syntax will be as:

If (condition)

Statement 1;

Else – if (condition)

Statement 2;

Else

Statement 3;

So in this structure first of all condition 1 is checked if it becomes true the statement 1 is executed. But if it become false then condition 2 with else if in checked and if it become true then statement 2 is executed otherwise statement 3 is executed.

The “if – else” statement: -

This statement is also called a conditional statement structure like simple if statement. But there is some extra statement ELSE is used with false case.

The syntax will be as:

If (condition)

Statement 1;

Else

Statement 2;

So in the above structure first of all condition is checked if it become true then statement 1 will be executed and if it become false then statement 2 will be executed.

If this kind of statement will compound then its syntax will be as:

If (condition)

{

Statement 1;

Statement 2;

}

Else

{

Statement 3;

Statement 4;

}

The “if” statement: -

The “if statement” is used to execute a set of statements after testing a condition. The “if statement” evaluates the condition. If the given condition is true, the statement (or a block of statements) following the “if statement” is executed. If the condition is false, the statement (or block of statements) following the “if statement” is ignored and the control transfers to the statement immediately after the if structure.

The syntax of the “if statement” is:

If (condition)

Statement - 1;

Statement - 2;

In the above syntax, statement -1 will be executed if the given condition is true. If the given condition is false, statement - 1 is ignored and statement - 2 will be executed.

The execute a block of statements following the “if statement” the block of statements is enclosed in curly, i.e. within {}.

The syntax of “if statement” for executing a block of statements is:

If (condition)

{

Statement - 1;

Statement - 2;

Statement - 3;

}

Goto and label statement: -

A goto statement can cause the program control to the end up almost any where in the program. The general syntax of goto statement is:

Goto label name;

…...

Label name:

Statement 1:

Statement 2:

…….

Where label name is a label name that tell goto statement where to jump. You have to place label name in two places. One is at that place following the goto statement and the other is the place where the goto statement is going jump.

Also the program place for the goto statement to jump can be appeared either before or after the statement.

Post a Comment

Previous Post Next Post