C switch Statement :

switch :

switch statement is use to execute a block of statements depending on the value of a variable or an expression.
Note :

  1. The braces { } can be omitted when there is only one statement available in the statement block.
  2. The break statement is used to transfer the control to the end of switch statement.
  3. The default block is executed when none of the case labels matches with the value of the expression / variable.
  4. It should also be noted that a switch statement is compact and can be used to replace a nested if statement.

Syntax :

  1. switch(<expression>)
  2. {
  3. case <label 1>: {
  4. <statement block 1>
  5. break; }
  6. case <label n>: {
  7. <statement block n>
  8. break; }
  9. default: {
  10. <default statement block>
  11. break; }

Example :

  1. /* Description: chose one or two use switch statement */
  2. #include<stdio.h>
  3. main()
  4. {
  5. int a;
  6. printf("Please enter a no between 1 and 2: ");
  7. scanf("%d",&a);
  8. switch(a)
  9. {
  10. case 1:
  11. printf("You chose One");
  12. break;
  13. case 2:
  14. printf("You chose Two");
  15. break;
  16. default :
  17. printf("Invalid Choice. Enter a no between 1 and 2");
  18. break;
  19. }
  20. getch();
  21. }

Output :

c language - chose one or two use switch statement

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels