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...

CPU (Central Processing Unit)

PGA reffered as pin grid array in which pins of CPU are lined up in a straight format. SPGA referred as staggered pin grid array. in which pins of CPU are arranged staggered format. LGA reffered to as Land Grid Array in which pins are available within inside the socket but not in the CPU in other words in line grid array, in CPU ther are no pinsbut insteadpins areattached with in a socket which contact with with the CPU. for e.g. LGA775 socket(no pins on cpu) has better cooling system. better contact and better locking(climbing). LGA1155: Letest generation I socket, also reffered as sandy bridge, Turbo boost overclocking. More resent than LGA 1156 socket. LGA1156: ability to north bridge Doul channel DDR3 optional integreted graphics PCI express LGA1366: hi end core i series socket integreted tiple channal memmory controller external control bridge(HUB) Upgrading CPU: need to check its core suppert check multiprocessor supports or not check on the ...

Recursive Function in C programming

             If a statement within the body of a function calls the same function, the function is called recursive function. Actually, recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. This process is used for repetitive computations in which each action is stated in term of previous result. Many iterative or repetitive problems can be written in this form.                To solve a problem using recursive method, two conditions must be satisfied. They are: 1)       Problem could be written or defined in term of its previous result. 2)       Problem statement must include a stopping condition. /*   An example of recursive function to calculate factorial of a number.*/    #include<stdio.h>    #include<conio.h...