C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Maximum no. of inputs

4

index-31_1.jpg

index-31_2.jpg

Enter n1: 1.5

Enter n2: 12.5

Enter n3: 7.2

Enter n4: -1

Average=7.07

In this program, when the user inputs number less than zero, the loop is terminated using break statement with executing the statement below it i.e., without executing sum=sum+num.

In C, break statements are also used in switch...case statement. You will study it in C switch...case statement chapter.

continue Statement

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.

Syntax of continue Statement

continue;

Just like break, continue is also used with conditional if statement.

For better understanding of how continue statements works in C programming. Analyze the figure below which bypasses some code/s inside loops using continue statement.

Example of continue statement

Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it.

//program to demonstrate the working of continue statement in C programming

# include <stdio.h>

int main(){

int i,num,product;

for(i=1,product=1;i<=4;++i){

printf("Enter num%d:",i);

scanf("%d",&num);

if(num==0)

continue; / *In this program, when num equals to zero, it skips the

statement product*=num and continue the loop. */

product*=num;

}

printf("product=%d",product);

return 0;

}