c programming continue statement

C Programming Continue Statement

c programming continue statement

C Programming Continue Statement

Table of Contents

continue Statements are used in programming to change the general flow of loops. In this chapter you continue will learn how to change the flow of various loops in C programming using C programming statements.

Sometime it is necessary to skip some statements inside the loop. In this case continue statement is used.

C programming continue statement

The continue statement skips some statement withing the loop. The continue statement is used in statements used to make decisions as if…else statements.

Syntax of continue statement in C programming

				
					continue;
				
			


Example 1: C programming continue statement

				
					#include<stdio.h>
#include<conio.h>

int main()
{
int i=1; // Local variable is initialized.
clrscr();

for(i=1; i<=10; i++) // Loop from 1 to 10
{
if(i==7) // The loop will run if the value of i is equal to 7
{
continue;
}
printf("%d \n",i);
} //end of for loop

getch();
}
				
			
output
				
					1
2
3
4
5
6
7
8
9
10
				
			
c programming continue statement


Example 2: C programming continue statement

				
					// Program to add up to 5 numbers
//The sum continues until the user enters a positive number.

# include <stdio.h>
int main()
{
 int i;
 double number, sum = 0.0;

 for(i=1; i <= 5; ++i)
 {
 printf("Enter a number%d: ",i);
 scanf("%lf",&number);

 // The continue statement is executed only when the user enters a negative number
 //and skips this statement and checks the next test expression.
 if(number < 0.0)
 {
 continue;
 }

 sum += number; // sum = sum + number;
 }

 printf("Sum = %.2lf",sum);

 return 0;
}
				
			
output
				
					Enter a number1: 5.6
Enter a number2: -2.5
Enter a number3: 10
Enter a number4: 15.5
Enter a number5: -2.2
Sum = 31.10
				
			

In the above program, when the user enters a positive number, sum += number; the sum is calculated through the statement.

But when the user enters a negative number, continue the statement executes and excludes the negative number from the calculation.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *