Lab No. 3
saqib.rasheed@mail.au.edu.pk




  Air University
Write a program to check whether a triangle is
valid or not, when the three angles of the
triangle are entered through the keyboard. A
triangle is valid if the sum of all the three
Angles is equal to 180 degrees




                 Air University
switch statement
The control statement that allows
 us to make a decision from the
  number of choices is called a
              switch
switch ( variable name )
 {
     case „a‟ :
           statements;
     case „b‟ :
           statements;
     case „c‟ :
            statements;
     …
 }
switch statements
switch ( grade)
 {
     case „A‟ :
            cout << “ Excellent ” ;
     case „B‟ :
            cout << “ Very Good ” ;
     case „C‟ :
            …
     …
 }
switch statements
case „A‟ :
      cout << “ Excellent ” ;
      …
      …
switch ( grade)
{
       case „A‟ :
              cout <<   “ Excellent ” ;
       case „B‟ :
              cout <<   “ Very Good ” ;
       case „C‟ :
              cout <<   “Good ” ;
       case „D‟ :
              cout <<   “ Poor ” ;
       case „F‟ :
              cout <<   “ Fail ” ;
}
break;
switch ( grade )
{
        case „A‟ :
                cout << “ Excellent ” ;
                break ;
        case „B‟ :
                cout << “ Very Good ” ;
                 break ;
        case „C‟ :
                cout << “Good ” ;
                 break ;
        case „D‟ :
                cout << “ Poor ” ;
                 break ;
        case „F‟ :
                cout << “ Fail ” ;
                 break ;
}
default :
default :
 cout << “ Please Enter Grade from
 „A‟ to „D‟ or „F‟ “ ;
Flow Chart of switch statement

          switch (grade)

                    case ‘A’ :

                       Display
                     “Excellent”

                    case ‘B’ :


                       Display
                     “Very Good”
                    …



                    Default :


                      “……..”
int i = 2 ;
switch ( i )
{
   case 1 :
         cout<<"I am in case 1 n"; break;
   case 2 :
         cout<<"I am in case 2 n"; break;
   case 3 :
         cout<<"I am in case 3 n"; break;
   default :
         cout<<"I am in default n";
}



           Output
           I am in case 2
if (x == 1)
                              switch (x)
{
                              {
cout << "x is 1";
                               case 1:
}
                               cout << "x is 1";
else if (x == 2)
                               break;
{                              case 2:
 cout << "x is 2";             cout << "x is 2";
}                              break;
else                           default:
 {                             cout << "value of x unknown";
cout << "value of x unknown"; }
}
There are some things that you simply cannot
  do with a switch. These are:
 A float expression cannot be tested using a
  switch
 Cases can never have variable expressions
  (for example it is wrong to say case a +3 : )
 Multiple cases cannot use same expressions.
  Thus the following switch is illegal:
                 switch ( a )
                 {
                 case 3.2 :
                 ...
                 case 1 + 2 :
                 ...
                 }
int day;

   cout<<"Eneter Day Number";
   cin>>day;

switch (day)
  {
   case 1 : cout << "nSunday";
         break;
   case 2 : cout << "nMonday";
         break;
   case 3 : cout << "nTuesday";
         break;
   case 4 : cout << "nWednesday";
         break;
   case 5 : cout << "nThursday";
         break;
   case 6 : cout << "nFriday";
         break;
   case 7 : cout << "nSaturday";
         break;
  default : cout << "nNot an allowable day number";
         break;
}
switch (day)
 {
   case 1 :
   case 7 :
   cout << "This is a weekend day"; Break
                                         1
   break;
  case 2 :
  case 3 :
  case 4 :
  case 5 :
  case 6 : cout << "This is a weekday";
                                          Break
  break;                                    2
 default : cout << "Not a legal day"; break;
}
char ch ;
cout<< "Enter any of the alphabet a, b, or c ";
cin>>ch;
switch ( ch )
{
   case 'a' : case 'A' :
         cout<<"a is an apple" ; break ;
   case 'b' :
   case 'B' :
         cout<<"b as in brain";
   break ;
   case 'c' : case 'C' :
         cout<<"c as in cookie"; break ;
   default :
         cout<<"wish you knew what are alphabets" ;
}
 Yesand no. Yes, because it offers a better
 way of writing programs as compared to if,
 and no because in certain situations we are
 left with no choice but to use if. The
 disadvantage of switch is that one cannot
 have a case in a switch which looks like:

                case   i <= 20 :
 compiler   generates a jump table for a switch
  during compilation
 during execution it simply refers the jump
  table to decide which case should be
  executed .
 if-elses are slower because they are
  evaluated at execution time
Make a calculator using switch statement
 It should take two numbers from user.
 Ask the user the function to perform
 Display the result




                 Air University
Que
Write a menu driven program program in C++
using switch statement that contain option as
under.( The Menu should )

Enetr 1--> To   Find   Largest Number Among Three Variables.
Enetr 2--> To   Find   ODD or EVEN
Enetr 3--> To   Find   Condition of Water
Enetr 4--> To   Find   Grade Of Student



                         Air University

Lab # 3

  • 1.
  • 2.
    Write a programto check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three Angles is equal to 180 degrees Air University
  • 3.
    switch statement The controlstatement that allows us to make a decision from the number of choices is called a switch
  • 4.
    switch ( variablename ) { case „a‟ : statements; case „b‟ : statements; case „c‟ : statements; … }
  • 5.
    switch statements switch (grade) { case „A‟ : cout << “ Excellent ” ; case „B‟ : cout << “ Very Good ” ; case „C‟ : … … }
  • 6.
    switch statements case „A‟: cout << “ Excellent ” ; … …
  • 7.
    switch ( grade) { case „A‟ : cout << “ Excellent ” ; case „B‟ : cout << “ Very Good ” ; case „C‟ : cout << “Good ” ; case „D‟ : cout << “ Poor ” ; case „F‟ : cout << “ Fail ” ; }
  • 8.
  • 9.
    switch ( grade) { case „A‟ : cout << “ Excellent ” ; break ; case „B‟ : cout << “ Very Good ” ; break ; case „C‟ : cout << “Good ” ; break ; case „D‟ : cout << “ Poor ” ; break ; case „F‟ : cout << “ Fail ” ; break ; }
  • 10.
    default : default : cout << “ Please Enter Grade from „A‟ to „D‟ or „F‟ “ ;
  • 11.
    Flow Chart ofswitch statement switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..”
  • 12.
    int i =2 ; switch ( i ) { case 1 : cout<<"I am in case 1 n"; break; case 2 : cout<<"I am in case 2 n"; break; case 3 : cout<<"I am in case 3 n"; break; default : cout<<"I am in default n"; } Output I am in case 2
  • 13.
    if (x ==1) switch (x) { { cout << "x is 1"; case 1: } cout << "x is 1"; else if (x == 2) break; { case 2: cout << "x is 2"; cout << "x is 2"; } break; else default: { cout << "value of x unknown"; cout << "value of x unknown"; } }
  • 14.
    There are somethings that you simply cannot do with a switch. These are:  A float expression cannot be tested using a switch  Cases can never have variable expressions (for example it is wrong to say case a +3 : )  Multiple cases cannot use same expressions. Thus the following switch is illegal: switch ( a ) { case 3.2 : ... case 1 + 2 : ... }
  • 15.
    int day; cout<<"Eneter Day Number"; cin>>day; switch (day) { case 1 : cout << "nSunday"; break; case 2 : cout << "nMonday"; break; case 3 : cout << "nTuesday"; break; case 4 : cout << "nWednesday"; break; case 5 : cout << "nThursday"; break; case 6 : cout << "nFriday"; break; case 7 : cout << "nSaturday"; break; default : cout << "nNot an allowable day number"; break; }
  • 16.
    switch (day) { case 1 : case 7 : cout << "This is a weekend day"; Break 1 break; case 2 : case 3 : case 4 : case 5 : case 6 : cout << "This is a weekday"; Break break; 2 default : cout << "Not a legal day"; break; }
  • 17.
    char ch ; cout<<"Enter any of the alphabet a, b, or c "; cin>>ch; switch ( ch ) { case 'a' : case 'A' : cout<<"a is an apple" ; break ; case 'b' : case 'B' : cout<<"b as in brain"; break ; case 'c' : case 'C' : cout<<"c as in cookie"; break ; default : cout<<"wish you knew what are alphabets" ; }
  • 18.
     Yesand no.Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like:  case i <= 20 :
  • 19.
     compiler generates a jump table for a switch during compilation  during execution it simply refers the jump table to decide which case should be executed .  if-elses are slower because they are evaluated at execution time
  • 20.
    Make a calculatorusing switch statement  It should take two numbers from user.  Ask the user the function to perform  Display the result Air University
  • 21.
    Que Write a menudriven program program in C++ using switch statement that contain option as under.( The Menu should ) Enetr 1--> To Find Largest Number Among Three Variables. Enetr 2--> To Find ODD or EVEN Enetr 3--> To Find Condition of Water Enetr 4--> To Find Grade Of Student Air University