Tuesday, 27 March 2018

Jumps out of the Loop

The break statement
  As we have already seen, the 'break' statement is used to exit out of the switch statement.It is having another useful application in loop control structures.That is, the 'break' statement is used to jump out of the loop.When the 'break' is executed inside the loop, the program control will not execute the loop for further iterations.Generally,   the 'break' is associated with if-statement.
General form of using 'break' is :-
break;


The dotted line shows the path where the Program control will transfer.
For example:
/*Example*/
int x;
for(x = 10; x<20; x++)
{
if(x%4 ==0)
break;
printf("%d\n",x);
}

The output of this program will be:
10
11
Because, when value of x becomes 12, the condition will be evaluated to true and break get executed.

The 'continue' statement
  The continue statement is used continue the next iteration of the loop by skipping the statements in between.That is, when the 'continue' is executed the statements following is will not be executed, the program control will directly transfer to next iteration of the loop by increment or decrement.Generally, the 'continue' is associated with if-statement.General form of using 'continue' is-
continue;

The dotted line shows the path where the program control will transfer.
For Example:-
int x;
for(x = 10; x < 20; x++)
{
if(x%4 ==0)
continue;
printf("%d");
}

The output of this program will be:
10   11   13  14  15  17  18  19

The 'switch' Statement
This is another form of the multi-way decision statement.It is well structured, but can only be used in certain cases. The switch statement tests value of a given variable (or expression) against the list of case values and when the match is found, a block of statements associated with that statements are executed. The general form of switch statement is as follows:
switch(variable)
{
case value-1:
//statement 1;
break;
case value-2:
//statements 2;
break;
case value-3
//statements 3
break;
--------
default:
//default-statements;
break;
}
statements-x;
In the above given syntax, value-1, value-2, value3..... are the set the constants. When the value of the variable given in the switch brackets is matched with any one of these values, the particular set of statements are executed and then program control transfer out of the switch block. For example, if value of variable is matched with 'value-2' then statements2 are executed and the break statement transfer the program control out of the switch.If the match is not found, the 'default' block is executed. Remember, statements-x are executed in any case. Generally, switch case are executed for menu selection problems.


Rules of switch statement
1. The switch variable must be an integer or character type.
2. Case labels must be constants of constants expressions.
3. Case labels must be unique. No two labels can have the same value.
4. Case labels must end with a colon.
5. The break statement transfers the program control out of the switch block.
6. The break statement is optional. When the break is not written in any 'case' then the statements following the next case are also executed until the 'break' is not found.
7. The default case is optional. If present, it will be executed when the match with any 'case' is not found.
8. There can be at most one default label.
9. The default may be placed anywhere but generally written at the end . When placed at end it is not compulsory to write the 'break' for it.
10. We can nest the switch statements.

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)