The “ continue “ Statement:
=> The continue statement is used to bypass the remainder of the current pass through a loop.=> The loop does not terminate when a continue statement is encountered.
=> Instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.
=> The continue statement can be included within a while, a do-while, a for statement.
=> It is simply written as “continue”.
=> The continue statement tells the compiler “Skip the following Statements and continue with the next Iteration”.
=> In „while? and „do? loops continue causes the control to go directly to the test – condition and then to continue the iteration process.
=> In the case of „for? loop, the updation section of the loop is executed before testcondition, is evaluated.
(1) while (Test condition)
{
- - - - - - - -
if ( - - - - - - -)
continue;
--------------------
--------------------
}
(2) do
{
--------------------
if ( - - - - - - )
continue;
-------------------
-------------------
} while(test – condition);
(3) for(initialization; test condition; increment)
{
- - - - - - - - - -
if( - - - - - - -)
continue;
-------------------
-------------------
}
Program to show the use of continue statement
# include<stdio.h>
void main( )
{
int i=1, num, sum =0;
for(i=0; i < 5; i ++)
{
printf(“ Enter an integer:”);
scanf( “%d”, &num);
if(num < 0)
{
printf(“you have entered a negative number”);
continue ; /* skip the remaining part of loop */
}
sum + = num;
}
printf(“The sum of the Positive Integers Entered = % d \ n”, sum);
}
Run 1:
Enter an integer: 7
Enter an integer: 3
Enter an integer: 10
Enter an integer: 15
Enter an integer: 30
The sum of the positive integers entered = 65
Run -2:
Enter an integer: 10
Enter an integer: -15
You have entered a negative number
Enter an integer: 15
Enter an integer: - 100
You have entered a negative number
Enter an integer: 30
The sum of the positive integers entered = 55.
Break and Continue: (Program)
Create an infinite for loop. Check each value of the for loop. If the value is even, display it other wise continue the iterations. Print “Even” numbers from 1 to 21. Use break statement to terminate the program.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i = 1;
clrscr( ) ;
printf(“ \n \t \tTable of Even numbers from 1 to 20”);
printf(“ \n \t \t = = = = = = = = = = = = = = = = = = = \n”);
for ( ; ;) /*=> Infinite loop (No Arguments).*/
{
if( i = = 21)
break;
else if( i % 2 = = 0)
{
printf(“%d \t”, t);
i++ ;
continue;
}
else
{
i++ ;
continue;
}
}
getch( );
}
Output:
Table of Even numbers from 1 to 20
= = = = = = = = = = = = = = = = = = = = = = == = = =
“Programming with C” course material
Department of CSE, GIT, GU Page 72
2 4 6 8 10 12 14 16 18
0 comments:
Post a Comment