Showing posts with label continue statement in c. Show all posts
Showing posts with label continue statement in c. Show all posts

Goto Break and Continue Statement in C


Goto Statement :

  • The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally.
Syntax :
goto label;
where the label is an identifier that is used to label the target statement to which the control is transferred. Control may be transferred to anywhere within the current function. The target statement must be labeled, and a colon must follow the label. Thus the target statement will appear as
label:statement;
Each labeled statement within the function must have a unique label, i.e., no two statement can have the same label.
C – GOTO STATEMENT EXAMPLE PROGRAM :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<conio.h>
void main()
{
int  number;
clrscr();
printf(www.);
goto x;
y:
printf(“.com”);
goto z;
x:
printf(“cprogramming);
goto y;
z:
printf(.bd);
getch();
}

Output:
www.cprogramming.com.bd

 

C programming break and continue statements



In this tutorial, you will learn about c programming break continue statements. Break and continue statements are used to jump out of the loop and continue looping.

Break and continue statements in c

Till now, we have learned about the looping with which we can repeatedly execute the code such as, for loop and while & do … while loop.
Just think what will you do when you want to jump out of the loop even if the condition is true or continue repeated execution of code skipping some of the parts?
For this C provides break and continue statements. By the help of these statements, we can jump out of loop anytime and able continue looping by skipping some part of the code.

The break statement in C



In any loop break is used to jump out of loop skipping the code below it without caring about the test condition.
It interrupts the flow of the program by breaking the loop and continues the execution of code which is outside the loop.
The common use of break statement is in switch case where it is used to skip remaining part of the code.

How does break statement works?

Structure of Break statement



In while loop
while (test_condition)
{
  statement1;
  if (condition )
     break;
  statement2;
}
In do…while loop
do
{
  statement1;
  if (condition)
     break;
  statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
  statement1;
  if (condition)
     break;
  statement2;
}
Now in above structure, if test_condition is true then the statement1 will be executed and again if the condition is true then the program will encounter break statement which will cause the flow of execution to jump out of loop and statement2 below if statement will be skipped.
Programming Tips
break statement is always used with if statement inside a loop and loop will be terminated whenever break statement is encountered.

Example: C program to take input from the user until he/she enters zero.

#include <stdio.h>
int main ()
{
 int a;
 while (1)
 {
   printf("enter the number:");
   scanf("%d", &a);
   if ( a == 0 )
      break;
  }
 return 0;
}
Explanation
In above program, while is an infinite loop which will be repeated forever and there is no exit from the loop.
So the program will ask for input repeatedly until the user will input 0.
When the user enters zero, the if condition will be true and the compiler will encounter the break statement which will cause the flow of execution to jump out of the loop.

The continue statement in C



Like a break statement, continue statement is also used with if condition inside the loop to alter the flow of control.
When used in whilefor or do...while loop, it skips the remaining statements in the body of that loop and performs the next iteration of the loop.
Unlike break statement, continue statement when encountered doesn’t terminate the loop, rather interrupts a particular iteration.

How continue statement work?

Structure of continue statement

In while loop
while (test_condition)
{
  statement1;
  if (condition )
     continue;
  statement2;
}
In do…while loop
do
{
  statement1;
  if (condition)
     continue;
  statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
  statement1;
  if (condition)
     continue;
  statement2;
}
Explanation
In above structures, if test_condition is true then the continue statement will interrupt the flow of control and block of statement2 will be skipped, however, iteration of the loop will be continued.

Example: C program to print sum of odd numbers between 0 and 10

#include <stdio.h>
int main ()
{
 int a,sum = 0;
 for (a = 0; a < 10; a++)
 {
   
   if ( a % 2 == 0 )
      continue;
  sum = sum + a;
  }
 printf("sum = %d",sum);
 return 0;
}
Output
sum = 25