Break, Continue, Go-to, etc Statements by G Krishna Chauhan


break Statement:



The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered. The break statement is used with decision making statement such as if...else.

Syntax of break statement

break;


Flow Chart:





Example Program:

// Program to calculate the sum of maximum of 10 numbers
// Calculates sum until user enters positive number
# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
// If user enters negative number, loop is terminated
if(number < 0.0)
{
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf",sum);
return 0;
}


Output:



Enter a n1: 2.4
Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30


continue Statement:

The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.

Syntax of continue Statement:

continue;

Flowchart of continue Statement:





goto Statement:

In this tutorial, you will learn to create goto statement in C programming. Also, you will learn when to use a goto statement and when not to use it.

Syntax of goto statement:

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;


Program Example:

// Program to calculate the sum and average of maximum of 5 numbers
// If user enters negative number, the sum and average of previously entered positive number is displayed
# include <stdio.h>
# include <conio.h>
int main()
{
const int maxInput = 5;
int i;
double number, average, sum=0.0;
for(i=1; i<=maxInput; ++i)
{
printf("%d. Enter a number: ", i);
scanf("%lf",&number);
// If user enters negative number, flow of program moves to label jump
if(number < 0.0)
goto jump;
sum += number; // sum = sum+number;
}
jump:
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}

Output

1. Enter a number: 3
2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9
Sum = 16.60


Reasons to avoid goto statement:



The use of goto statement may lead to code that is buggy and hard to follow. 


exit and return statements:

exit () is used to exit the program as a whole. In other words it returns control to the operating system.

After exit () all memory and temporary storage areas are all flushed out and control goes out of program. In contrast, the return () statement is used to return from a function and return control to the calling function.

Also in a program there can be only one exit () statement but a function can have number of return statements. In other words there is no restriction on the number of return statements that can be present in a function.

exit () statement is placed as the last statement in a program since after this program is totally exited. In contrast return statement can take its presence anywhere in the function. It need not be presented as the last statement of the function.

It is important to note that whenever a control is passed to a function and returns back, some value gets returned. Only if one uses a return statement the correct value would get returned from the called function to the calling function.


No comments:

Post a Comment