Lab No. 3 [email_address] Air University
<ul><li>Write a program to check whether a triangle is  </li></ul><ul><li>valid or not, when the three angles of the  </li...
<ul><li>switch statement </li></ul>The control statement that allows us to make a decision from the number of choices is c...
<ul><li>switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; </li></ul><ul><li>case ‘c’ :  statements;...
<ul><li>switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } </li></ul...
<ul><li>case ‘A’ : </li></ul><ul><li>cout << “ Excellent ” ; … … </li></ul>switch statements
<ul><li>switch ( grade) </li></ul><ul><li>{ case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’...
<ul><li>break; </li></ul>
<ul><li>switch ( grade ) </li></ul><ul><li>{ case ‘A’ : </li></ul><ul><li>cout << “ Excellent ” ; </li></ul><ul><li>break ...
<ul><li>default : </li></ul><ul><li>cout << “ Please Enter Grade from ‘A’ to ‘D’ or  ‘F’  “ ; </li></ul>default :
switch (grade) Display  “ Excellent” case ‘B’ : case ‘A’ : Display  “ Very Good” Default : “…… ..” Flow Chart of switch st...
<ul><li>int i = 2 ;  </li></ul><ul><li>switch ( i )  </li></ul><ul><li>{  </li></ul><ul><li>case 1 :  </li></ul><ul><li>co...
<ul><li>if (x == 1)  </li></ul><ul><li>{  </li></ul><ul><li>cout << &quot;x is 1&quot;;  </li></ul><ul><li>} </li></ul><ul...
<ul><li>There are some things that you simply cannot do with a  switch. These are:  </li></ul><ul><li>A float expression c...
<ul><li>int day; </li></ul><ul><li>cout<<&quot;Eneter Day Number&quot;; </li></ul><ul><li>cin>>day; </li></ul><ul><li>swit...
<ul><li>switch (day) </li></ul><ul><li>{  </li></ul><ul><li>case 1 :  </li></ul><ul><li>case 7 :  </li></ul><ul><li>cout <...
<ul><li>char ch ;  </li></ul><ul><li>cout<< &quot;Enter any of the alphabet a, b, or c &quot;;  </li></ul><ul><li>cin>>ch;...
<ul><li>Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain s...
<ul><li>compiler generates a jump table for a switch during compilation  </li></ul><ul><li>during execution it simply refe...
<ul><li>Make a calculator using switch statement </li></ul><ul><li>It should take two numbers from user. </li></ul><ul><li...
<ul><li>Que  </li></ul><ul><li>Write a menu driven program program in C++  </li></ul><ul><li>using switch statement that c...
Upcoming SlideShare
Loading in …5
×

c++

1,003 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,003
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
59
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

c++

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

×