User Tools

Site Tools


cs:c_language:switch
Return to Home page

Differences

This shows you the differences between two versions of the page.


cs:c_language:switch [2024/04/08 22:35] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +====== Switch statement ======
 +**Concepts:**\\
 +''switch'' and ''if'' statements, different ways to write and indent a program
  
 +**Text:**\\
 +Write a program using the ''if'' or the ''switch'' statement that, given in input a number, recognize the following commands:
 +^Command ^Input Number ^
 +| Down | 2 |
 +| Up | 8 |
 +| Left | 4 |
 +|Right | 6 |
 +
 +
 +**Solution:**\\
 +A number of 5 equivalent solutions are reported in the following. Probably, the last one based on the ''switch'' statement, is the more readable.
 +
 +**Solution 1:**\\
 +This solution is based on the ''if'' statement and on a flag variable ''cmd_executed'' used to specify if a command (i.e., ''2'', ''4'', ''6'', ''8'') has been correctly recognized.
 +
 +<file C switch_a.c>
 +#include <stdio.h>
 +
 +int main(){
 +  int cmd;
 +  int cmd_executed=0;
 +
 +  printf("Insert a command: ");
 +  scanf("%d", &cmd);
 +
 +  if(cmd==2){
 +    printf("Down\n");
 +    cmd_executed=1;
 +  }
 +  if(cmd==8){
 +    printf("Up\n");
 +    cmd_executed=1;
 +  }
 +  if(cmd==4){
 +    printf("Left\n");
 +    cmd_executed=1;
 +  }
 +  if(cmd==6){
 +    printf("Right\n");
 +    cmd_executed=1;
 +  }
 +  if(!cmd_executed){
 +    printf("ERROR: incorrect command!\n");
 +  }
 +
 +  return 0;
 +}
 +</file>
 +
 +
 +**Solution 2:**\\
 +This solution is based only on the ''if'' statement, without the used of the flag variable
 +
 +<file C switch_b.c>
 +#include <stdio.h>
 +
 +int main(){
 +  int cmd;
 +
 +  printf("Insert a command: ");
 +  scanf("%d", &cmd);
 +
 +  if(cmd==2){
 +    printf("Up\n");
 +  }else{
 +    if(cmd==8){
 +      printf("Down\n");
 +    }else{
 +      if(cmd==4){
 + printf("Left\n");
 +      }else{
 + if(cmd==6){
 +   printf("Right\n");
 + }else{
 +     printf("ERROR: incorrect command!\n");
 + }
 +      }
 +    }
 +  }
 +
 +  return 0;
 +}
 +
 +</file>
 +
 +
 +**Solution 3:**\\
 +Same as //Solution 2//, but without braces in the ''if'' statement.
 +
 +<file C switch_c.c>
 +#include <stdio.h>
 +
 +int main(){
 +  int cmd;
 +
 +  printf("Insert a command: ");
 +  scanf("%d", &cmd);
 +
 +  if(cmd==2)
 +    printf("Down\n");
 +  else
 +    if(cmd==8)
 +      printf("Up\n");
 +    else
 +      if(cmd==4)
 + printf("Left\n");
 +      else
 + if(cmd==6)
 +   printf("Right\n");
 + else
 +     printf("ERROR: incorrect command!\n");
 +  
 +  return 0;
 +}
 +</file>
 +
 +
 +**Solution 4:**\\
 +This solution is exactly equivalent to the one reported in //Solution 3//, only indentation changes
 +
 +<file C switch_d.c>
 +#include <stdio.h>
 +
 +int main(){
 +  int cmd;
 +
 +  printf("Insert command: ");
 +  scanf("%d", &cmd);
 +
 +  if(cmd==2)
 +    printf("Down\n");
 +  else if(cmd==8)
 +    printf("Up\n");
 +  else if(cmd==4)
 +    printf("Left\n");
 +  else if(cmd==6)
 +    printf("Right\n");
 +  else
 +    printf("ERROR: incorrect command!\n");
 +  
 +  return 0;
 +}
 +</file>
 +
 +
 +**Solution 5:**\\
 +Finally, this solution is based on the use of the ''switch'' statement
 +
 +<file C switch_e.c>
 +#include <stdio.h>
 +
 +int main(){
 +  int cmd;
 +
 +  printf("Insert command: ");
 +  scanf("%d", &cmd);
 +
 +  switch(cmd){
 +  case 2:
 +    printf("Down\n");
 +    break;
 + 
 +  case 8:
 +    printf("Up\n");
 +    break;
 +
 +  case 4:
 +    printf("Left\n");
 +    break;
 + 
 +  case 6:
 +    printf("Right\n");
 +    break;
 +
 +  default:
 +    printf("ERROR: incorrect command!\n");
 +  }
 +
 +  return 0;
 +}
 +</file>

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/switch?rev=1551188114&do=diff

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy