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.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (745) Python Coding Challenge (198) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses