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

Passing arrays to functions in C programming

Like any other variables, we can also pass entire array to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call by reference because the array name is address for that array. /* Program to illustrate passing array to function */ #include<stdio.h> void display(int) ; /* function prototype */ main( ) { int num[5] = {100, 20, 40, 15, 33, i ; clrscr( ) ; printf (“\n The content of array is \n”) ; for (i=0; i<5; i++) display (num[i]) ; /*Pass array element fo fun */ getch{ } ; } void display(int n) { printf (“\t%d”, n ) ; } Output:     The content of array is 100      20       40       15 3 /* Program to read 10 numbers from keyboard to store these num into array and then c...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...

BIOS

BIOS stands for Basic Input Output System. It is is kind of OS pre- attached in the system Design to detect every I/O devices It holds basic drivers -checks all I/O devices -Loads main OS -Responsible for starting System switched on manufactured by AMI,Phonix,award,Baiostar POST error test, boots trap load, BIOS is unintrusive system. CMOS and BIOS are two different components of the motherboard. CMOS work as storage if BIOS. A basic software program containing all BIOS is permanently stored in the ROM. every essencial holds program Tresh passing without knowinig is called intrusive.