Loops and Jump Statements
Loops
Loop is a block of code or sequence of statements which repeates until a specific condition statisfied. There are 3 types of Loops in C++- while()
- do...while()
- for()
- Nested for()
- Nested for()
While Loop
While Loop is also known as Entry Control Loop.In while loop the Condition is checked before entering into the Loop if the condition is true then it will execute the statements otherwise terminates the Loop.
Syntax
while(condition)
{
Statements;
inc/dec;
}
do..While Loop
do..While Loop is also known as Exit Control Loop.In do..while loop the condition is checked after Executing the statement once.
It will execute the statements once even the condition is false.
Syntax
do
{
Statements;
inc/dec;
}while(condition);
for Loop
for Loop is Used to reduce the Line of code in program.In for loop the initialization, condition and increment/decrement is given in a single line.
Syntax
for(initialization; condition; inc/dec)
{
Statements;
}
-----------------------------------------------------------------------------------------------------------------
initialization;
for(; condition; inc/dec)
{
Statements;
}
-----------------------------------------------------------------------------------------------------------------
initialization;
for(; condition; )
{
Statements;
inc/dec;
}
-----------------------------------------------------------------------------------------------------------------
for Infinite Loop
for(; ; )
{
Statements;
}
Nested for Loop
Nested for loop is a loop which is used inside another loop.There is an Outer loop and an Inner Loop. Where if the Condition of Outer loop is true then will execute the Inner Loop. Otherwise Terminates the Loop.
Syntax
for(initializaion; condition; inc/dec)
{
for(initializaion; condition; inc/dec)
{
Statements;
}
}
Programs
while loop
Write a Program to print "Programming World!!" 10 times.do while loop
Write a Program to print number from 10-1.Nested for Loop
* *
* * *
* * * *
* * * * *
Jump Statements
break statement:
break statements is used to stop the execution of a particular block.It can be used with Conditional Statements as well as Loops.
Program
Write a Program to show the used of break keyword.continue statement:
continue statements is used with loops where continue is used to skip the particular staement at a given condition.Program
Write a program to print numbers from 1-50 and skip the numbers which are dicisible by 9goto Statement
It is a type of jump statement use to jump from one statement to another.Syntax
goto label;
- - - - - - - - - -
- - - - - - - - - -
label:
- - - - - - - - - -
- - - - - - - - - -
Write a program to show the use of goto statements
Write a program to show the use of goto statements
Comments
Post a Comment
If you have any doubts, Please let me Know