Skip to main content

Statements: switch, break, continue, goto in C programming

Switch Statement:
This is a multi-directional conditional control statement. Sometimes, there is a need in program to make choice among number of alternatives. For making this choice, we use the switch statement.
The general syntax is
switch(expression)
{ case constant1:
statements ;
break ;
- - - -
- - - -
case constantN:
statements ;
break,
default:
statements ;
}

Here, switch, case and default are keywords. The ‘expression’ following the switch keyword can be any C expression that yields an integer value or a character value. It can be value of any integer or character variable, or a function call returning on integer, or arithmetic, logical, relational, bitwise expression yielding integer value.
The constants following the case keywords should be of integer or character type. These constants must be different from each other. The statements under case can be any valid C statements like if...else, while, for or even another switch statement. Writing a switch statement inside another is called nesting of  switches.
Firstly, the switch expression is evaluated then value of this expression is compared one by one with every case constant. If the value of expression matches with any case constant, then all statements under that particular case are executed. If none of the case constant matches with the value of the expression then the block of statements under default is executed.

|* Program to understand the switch control statment *|
#include<stdio.h>
main()
{ int choice ;
printf (“Enter your choice :” ) ;
scanf (“%d”, & choice)
switch(choice)
{
case 1 :
printf (“first|n”) ;
case 2 :
printf (“second|n”) ;
case 3 :
printf (“third|n”) ;
default :
printf (“wrong choice\n”) ;
}
Output:
Enter your choice : 2
Second
Third
Wrong Choice

Here value of choice matches with second case so all the statements after case 2 are executed sequentially. The statements of case 3 and default are also executed in addition to the statements of case2. This is called falling through cases. Suppose we don’t want the control to fall through the statements of all the cases under the matching case, then we can use break statement.


Break statement:

Break statement is used inside lops and switch statements. Sometimes it becomes necessary to come out of the loop even before the loop condition becomes false. In such a situation, break statement is used to terminate the loop. This statement causes an immediate exit from that loop in which this statement appears. It can be written as (i.e. general syntax) :
break,
If a break statement is encountered inside a switch, then all the statements following break are not executed and the control jumps out of the switch.
/* Program to understand the switch with break statement */
#include<stdio.h>
main()
{
int choice ;
printf (“Enter your choice : ”) ;
scanf (“%d”, & choice) ;
switch (choice)
{
case1:
print (“First\n”) ;
break ; /* break statement */
case2:
printf(“Second\n”) ;
break ;
case3:
printf (Third\n”) ;
break ;
default :
printf (“Wrong choice\n”) ;
}
} /* End of main() */
Output:
Enter your choice : 2

/* Program to perform arithmetic calculation on integers */
#include<stdio.h>
main()
{ char op ;
int a, b ;
printf (“Enter a number, operators and another num :”) ;
scanf (“%d%c%d, &a, &op, &b) ;
switch (op)
{
case ‘+’ :
printf (“Result = %d\n”, a+b) ;
break ;
case ‘-‘ :
printf (“Result = %d\n”, a-b) ;
case ‘*’ :
printf (“Result = %d\n”, a*b) ;
case ‘/’
printf (“Result = %d\n”, a/b) ;
case ‘%’
printf (“Result = %d\n”, a%b) ;
default:
printf (“Enter your valid operation”) ;
} /* end of switch */
} /* end of main() */




/* Program to find whether the alphabet is a vowel or consonant */
#include<stdio.h>
main()
{ char ch ;
printf (“Enter an alphabet :” ) ;
scanf (“%c”, &ch) ;
switch (ch)
{
case ‘a’ : case ‘A’ :
case ‘e’ : case ‘E’ :
case ‘i’ : case ‘I’ :
case ‘o’ : case ‘O’ :
case ‘u’ : case ‘U’ :
printf (“Alphabet is a vowel\n”) ;
break ;
default :
printf (“Alphabet is a constant\n”) ;
} /* end of switch */
} * end of main() */


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 general syntax :
continue ;

/* Program to demonstrate continue statement */
#include<stdio.h>
main()
{
int i, num ;
printf (“\n Enter a number :”) ;
scanf (%d”, &num) ;
printf (“\n The even numbers from 2 to %d are : \n”, num) ;
for (i=1, ; i<=num ; i++)
{ if(i%2!=0)
continue ;
printf (“\t%d”, i) ;
{ /* end of for loop */
} / * end of main() */
Output:
Enter a number : 20
The even numbers from 2 to 20 are
0     4 6 8 10 12 14 16 18 20


goto statement:

The goto statement is used to alter the normal sequence of program execution by unconditionally transferring control to some other part of the program. The goto statement transfers the control to the labeled statement somewhere in the current function.
 The general syntax of goto statement:
goto label ;
- - - - -
- - - - -
label :
statement ;
- - - - -
- - - - -
Here, label is any valid C identifier and it is followed by a colon. Whenever, the statement goto label, is encountered, the control is transferred to the statement that is immediately after the label.
Generally, the use of goto statement is avoided as it makes program illegible and unreliable. This statement is used in unique situations like
• Branching around statements or group of statements under certain conditions
• Jumping to the end of a loop under certain conditions, thus bypassing the remainder of loop during current pass.
• Jumping completely out of the loop under certain conditions, terminating the execution of a loop.

/* Program to print whether the number is even or odd */
#include<stdio.h>
main()
{ int n ;
printf( “Enter the number :”) ;
scanf (“%d”, &n) ;
if (n%2 = = 0)
goto even ;
else
goto odd;
even :
printf (“Number is even”) ;
goto end ;
odd :
printf (“Number is odd”) ;
end :
printf (“\n) ;
}
Output:
Enter the number : 6
Number is even.

Comments

Popular posts from this blog

Database Management System(DBMS)

Data and Information  Data is an unprocessed raw fact which does  not give complete meaning about any object,person, entity etc. But when various inter-related data arranged in a row, it gives a complete meaning which is referred to as information. For Example: ID Name Address Program 1101 Ram Shrestha Biratnagar BScIT         is information of a specific person. Database Database is a repository(storage) for the organized collection of related information  of data items. It Consist of meaningful arrangement of data, relationship, constraints, schema etc. In modern days we define database as a computerized record-keeping system used to store information and allows users to retrieve and update these information on their request. Some example of databases are telephone directory, data of SLC result, student and employee records etc. Database Management System(DBMS) Database is the softw...

What is Java Applets?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without further interaction with the user. If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser. Applets are intended to be small programs. They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server. In essence, the applet allows some functionality to be moved from the server to the client.

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...