The if structure
if ( condition )
{
statement(s)
}
Example :
/* make sure x is positive */
if (x < 0)
{
x = -x;
}
The if/else structure
if ( condition )
{
statement(s)
}
else
{
statement(s)
}
Example :
/*assign a grade */
if (score >= 70)
{
grade = 'P';
}
else
{
grade = 'F';
}
The switch structure
switch (E)
{
case C1 :
statements_1;
break;
case C2 :
statements_2;
break;
.
.
.
default :
statements_n;
break;
}
More about switch
E is the control expression which
should evaluate to an integer.
the Ci's should be integer constants.
case C1: statements_1; break; is a
case clause
default: statements_n; break; is a
default clause
Semantics
- E is evaluated
- If E's value is equal to one of the Ci's, then control is
immediately transfered to that case.
- ...Otherwise, if there is a default clause, control is transferred
there.
- ......Otherwise, exit the switch.
- The statements in that selected case are evaluated.
- If a break is evaluated, control is transfered to the next
statement after the switch
Good programming practice
- include a break in every clause
- always include a default clause
More switch examples
The Program
/****************************************************
* File: cardrank.c
* Author: S. Bogar
* Date: Spring 1942
* Section 0101
* E-Mail: bogar@cs.umbc.edu
*
* Reads in a number between 1 and 13 and writes
* out the appropriate symbol for a playing card
* of that rank.
*******************************************************/
#include
int main()
{
int rank;
/* Prompt user and get card rank */
printf("What is the rank of the card (1-13)? ");
scanf ("%d", &rank);
/* Print appropriate name */
switch (rank)
{
/* Special card names for 1 and 11-13 */
case 1:
printf("Ace\n");
break;
case 11:
printf("Jack\n");
break;
case 12:
printf("Queen\n");
break;
case 13:
printf("King\n");
break;
/* 'default' handles 2 - 10; just print the value */
default:
printf("%d\n", rank);
break;
}
return 0;
}
The Sample Run
linux1[75]% a.out
What is the rank of the card (1-13)? 11
Jack
linux1[76]% a.out
What is the rank of the card (1-13)? 1
Ace
linux1[77]% a.out
What is the rank of the card (1-13)? 8
8
linux1[78]% a.out
What is the rank of the card (1-13)? 12
Queen
linux1[79]% a.out
What is the rank of the card (1-13)? 13
King
linux1[80]% a.out
What is the rank of the card (1-13)? 7
7
linux1[81]% a.out
What is the rank of the card (1-13)? 14
14
linux1[82]%
Oops
- We need to check for bad input.
cardrank2
The Program
/**********************************************
* File: card2.c
* Author: Sue Bogar
* Date: Summer 1942
* Section: 0101
* E-Mail: bogar@cs.umbc.edu
*
*
* Reads in a number between 1 and 13 and writes
* out the appropriate symbol for a playing card
* of that rank.
* Checks for and handles invalid input
*****************************************/
#include
int main()
{
int rank;
/* Prompt user and get card rank */
printf("What is the rank of the card (1-13)? ");
scanf ("%d", &rank);
/* Print appropriate name */
switch (rank)
{
/* special card names for 1, 11-13 */
case 1:
printf("Ace\n");
break;
case 11:
printf("Jack\n");
break;
case 12:
printf("Queen\n");
break;
case 13:
printf("King\n");
break;
/* for 2 - 10, just print rank */
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
printf("%d\n", rank);
break;
/* 'default' catches invalid input */
default:
printf("Bad card number: %d\n", rank);
break;
}
return 0;
}
The Sample Run
linux1[83] % a.out
What is the rank of the card (1-13)? 1
Ace
linux1[84] % a.out
What is the rank of the card (1-13)? 8
8
linux1[85] % a.out
What is the rank of the card (1-13)? 14
Bad card number: 14
linux1[86] % a.out
What is the rank of the card (1-13)? 13
King
linux1[87] %