assume that grade is a variable whose value is a letter grade-- anyone of the following letters: 'A', 'B', 'C', 'D', 'E', 'F', 'W', 'I'. Assume further there are the following int variables, declared an already initialized: account, bcount, ccount, dcount, ecount, fcount, wcount, icount. Write a switch statement that increments the apropiate variable (acount, bcount, ccount, etc..) depending on the value of grade. So if grade is 'A' then acount is incremented; if grade is 'B' then bcount is incremented and so on.

Respuesta :

Answer:

Following are the Switch statement in C language is given below:

switch (grade)  //switch case

{

case 'A':  // case A

++acount; // incremented of acount

break;  // break the switch case

case 'B':  // case B

++bcount;  // incremented of bcount

break; // break the switch case

case 'C':  // case C

++ccount; // incremented of ccount

break; // break the switch case

case 'D':  //case D

++dcount; // incremented of dcount

break; // break the switch case

case 'E': //case E

++ecount;  // incremented of ecount

break;  // break the switch case

case 'F':  // case F

++fcount; // incremented of fcount

break;   // break the switch case

case 'W': // case W

++wcount;// increment of wcount

break; // break the switch case

case 'I': // Case

icount++;  // increment of icount variable

break;  // break the switch case

default: // default case

printf(" incorrect letter");/

}

Explanation:

The switch is used when we have multiple conditions and we have chosen only one condition.

The description of the statement is given below

  • In the switch condition, we have passed the char "grade" variable i.e switch(grade).
  • Inside the Switch case block, we have Created the cases 'A'.In Case A we have incremented the value of "acount " variable and break the case. The Same step is followed to create the case of B', 'C', 'D', 'E', 'F', 'W', 'I' respectively.
  • Suppose If the user Read character 'C' then control moves to the case 'C' . In that case, it firstly increments the value of "ccount" and break the switch case. The break statement is used for breaking the switch when a particular case is matched.
  • If the user Read incorrect character which is not matched with the cases than control moves to the default state and print "incorrect letter