Tutorial 5.4: switch Selection Structure

switch Selection Statement
The switch statement allow us to select one from multiple options. It is especially useful when we want to write a code that makes a selection based on an expression or a variable. switch selection structure consists of a series of case labels and optional default case as shown below.
switch(var) {
 case constant1:
  statement;
  ...
  break;
 case constant2:
  statement;
  ...
  break;
 default:
  statement;
  ...
  break;
}
switch statement works by comparing the value of variable or expression within parentheses with values specified in each case. If there is a match, the statements following the case will be executed. If there is no match occurs, the default statement is executed. At the end of each case, keyword break must be placed to exit from the switch immediately after statements execution.

Lets look at an example.
#include <stdio.h>

int main()
{
 int choice;
 printf("1. Create a new database\n");
 printf("2. Edit a database\n");
 printf("3. Delete a database\n");
 printf("4. Merge a database\n");
 printf("5. Exit system\n");
 printf("Choose an option: ");
 scanf("%d", &choice);
 
 switch(choice) {
 case 1:
  printf("Creating...\n");
  break;
 case 2:
  printf("Editing...\n");
  break;
 case 3:
  printf("Deleting...\n");
  break;
 case 4:
  printf("Merging...\n");
  break;
 case 5:
  printf("Thank you. Bye.\n");
  break;
 default:
  printf("Invalid input!\n");
  break;
 }
 
 return 0;
}
Sample output:

1. Create a new database
2. Edit a database

3. Delete a database
4. Merge databases

5. Exit system
Choose an option: 2
Editing...


Explanation:
int choice;
printf("1. Create a new database\n");
printf("2. Edit a database\n");
printf("3. Delete a database\n");
printf("4. Merge a database\n");
printf("5. Exit system\n");
printf("Choose an option: ");
scanf("%d", &choice);
We declare an integer variable choice. Then we display the options that user can choose and ask the user to input his/her choice. scanf() reads the input value and store it in variable choice.
 
switch(choice) {
case 1:
 printf("Creating...\n");
 break;
case 2:
 printf("Editing...\n");
 break;
case 3:
 printf("Deleting...\n");
 break;
case 4:
 printf("Merging...\n");
 break;
case 5:
 printf("Thank you. Bye.\n");
 break;
default:
 printf("Invalid input!\n");
 break;
}
Next we have the switch statement that will select from the specified cases. A case is selected depending on the entered value of choice. Statement belong to the selected case will be executed and the break at the end of the case causes the program to jump to the statement following the switch block. In the case of user enters a value other than one (1) to five (5), default case will be executed displaying a message or invalid input.

Next: while Loop

1 comment:

Unknown said...

hello. i tried to write this program but it not working i have 9 erros:
1- incorect use of default in function.
2-case statement missing: in function
3-Duplication case in function. i m using turbo c as compiler.please help me

Post a Comment