Some more control statements :-
I.
Nesting of loops
II. Infinite loops
III. break statement
IV. continue statement
V. goto statement
VI. switch statement ( studied earlier )
II. Infinite loops
III. break statement
IV. continue statement
V. goto statement
VI. switch statement ( studied earlier )
control statements |
➤Nesting of loops :-
- When the loop is written inside the body of an another loop , then it is known as a nested loop.
- Any loop can written inside the any type of loop, there is no any special condition for it.
example as :- forloop in for loop
for (
initialization ; condition ; increment / decrement )
{
for (
initialization ; condition ; increment / decrement )
{
statements ;
}
}
P.example :- Program to print the series of perfect no. upto 100.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i , j , sum =0
;
for ( i = 1 ; i <= 100 ; i ++ )
{
for ( j = 1 ; j
< i ; j ++ )
{
if ( i % j ==
0 )
sum = sum +
j ;
}
if ( sum == i )
printf( “ %d \n
“, i );
sum =0 ;
}
printf( “ %d is the
perfect no. “ , i );
getch();
}
➤Infinite loop :-
- The type of loop that go on executing infinitely without terminating , is known as infinite loop.
- These type of loops are stopped only by ending the program.
- Some times these type of loops are written by mistake and sometime we use these loops in our program.
example of such mistake :-
for ( i = 0 ; i
<= 10 ; i --)
printf ( “ %d “ ,
i );
Here , this loop is
infinite loop as it never terminaes because, the condtion given is less than or
equal to 10 to terminate, but i initialized at 0 and decremented contuosly and
never become 10 or more than 10. So it is execute infinitely.
➤break statement :-
- This statement is used when it becomes necessary to come out of the loop even before the loop condition becomes false, at that time we use break statement to come out of the loop .
- This statement is used insisde the loops and inside the switch statements.
- This statement cause an immediate exit from the loop.
Syntax:-
break;
for examples visit switch statements (click for example).
➤continue statement :-
- This statement is used when we need to go to the next iteration of the loop after skipping some statements of the loop.
- Syntax :-
continue ;
- It is used with a condition for when to skipping the statements .
- When continue statement is encountered then the all remaining statement in the loop are not executed and the loop continues with the next iteration.
Difference
between break and continue statement :-
|
When break is encountered then the loop
terminates and moves to the next statement following the loop.
|
When continue is encountered then the loop isn’t
terminated , only the next statements of the loop is skipped and the next
iteration of the loop starts.
|
P. example:- Program to find the sum and average of 10 positive no.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i =1 , n , sum = 0;
float avg ;
printf( “ Enter 10
positive no. “ );
while ( i <= 10 )
{
printf( “ enter
number %d : “ , i);
scanf( “ %d”,
&n );
if ( n < 0 )
{
printf( “
enter only positive no.” );
continue;
}
sum += n;
i++;
}
avg= sum / 10.0;
printf( “ sum =
%d Avg = %f \n “, sum , avg);
getch();
}
➤goto statement :-
- This statement is an unconditional control statement that transfers the control to the another part of the program .
Syntax :-
goto label ;
….
…..
label :
statement
.….
…..
- Here label is any valid identifier followed by colon.
- When goto label ; statement is encountered then all control is transferred to the statements which is immediately after the label.
- It can be placed anywhere in the program, according to the need.
- If the label is after goto statement then control is transferred
forward and it is known as forward jump
or forward goto.
- If the label is before goto statement then control is
transferred backward and it is known as backward
jump or backward goto.
- More than one goto can be placed with the same label but we cannot have the same label at more than one place.
P.example :- Program to find whether a no. is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf( “ enter a
no. “);
scanf( “ %d” ,
&n );
if ( n%2 == 0 )
goto even ;
else
goto odd;
even:
printf( “ number
is even \n “ );
goto end ;
odd :
printf ( “
number is odd \n “ );
goto end ;
end :
printf ( “ \n “ );
getch();
}
Great saurav Bhai
ReplyDeleteThanks brother.
Delete