1 / 16

Lecture 7

Lecture 7. Sept 23, 2002. Switch Structure. General form: switch (expression) { case constant 1: do this; case constant 2: do this; case constant 3: do this; default: do this; }. Explanation.

jswindle
Download Presentation

Lecture 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7 Sept 23, 2002

  2. Switch Structure • General form: switch (expression) { case constant 1: do this; case constant 2: do this; case constant 3: do this; default: do this; }

  3. Explanation • Expression following the keyword switch is evaluated. • The value it gives is then matched, one by one, against the constant values that follow the case statements. • Matching between the value evaluated by expression and case value takes place. • When a match is found, the program executes the statements following that case and all subsequent case and default statements as well. • If no match, only the statements following the default are executed. • The cases can be put in any order you please. • break keyword is used to get out case. (Imp illustrated example 2)

  4. Example 1 • int main () { output: int i=2; I am in case 2 switch(i) I am in case 3 { I am in default case 1: cout<<“I am in case 1”<<endl; case 2: cout<<“I am in case 2”<<endl; case 3: cout<<“I am in case 3<<endl; default: cout<<“I am in default<<endl; } return 0; }

  5. Example 2 • int main() output: { I am in case 2 int i=2; switch (i) { case 1: cout<<“ I am in case 1”<<endl; break; case 2: cout<<“ I am in case 2”<<endl; break; case 3: cout<<“ I am in case 3”<<endl; break; default: cout<<“ I am in default”<<endl; } return 0; }

  6. Example 3 #include<iostream.h> int main() { int a; cout<<“Enter a number”; cin>>a; switch(a) { case 1: cout<<“Life is good\n”; break; case 2: cout<<“Life is bad\n”; case 3: case 4: case 5: cout<<“Life is beautiful\n”; } return 0; }

  7. Example 4 • int main() output: { I am in case x char c = ‘x’; switch (c) { case ‘v’: cout<<“ I am in case v\n”); break; case ‘a’: cout<<“ I am in case a\n”); break; case ‘x’: cout<<“ I am in case x\n”); break; default: cout<<“I am in default”<<endl; } return 0; }

  8. Repetition Structures • Programming till now involved input, output, assignment and selection capabilities. • Many problems require a repetiton capability in which the same calculation or sequence of instructions is repeated • Repetition structures are used for this purpose.

  9. Types of Repetition structure

  10. While loops initialise loop counter; while (test loop counter using condition) { do this; and this; increment loop counter; }

  11. Example 1 • /* Calculation of simple interest for 3 sets of p, n and r */ int main() { int p,n,count; float r, si; count=1; while(count<=3) { cout<<“Enter values of p,n,r”<<endl; cin>>p>>n>>r; si = p*n*r/100; cout<<“Simple interst = “<<si<<endl; count=count+1; } return 0; }

  12. Explanation • The logic for calculating the simple interest is written within a pair of braces. These statements form the ‘body of the loop’. • The parentheses after the while contains a condition. So long as this condition remains true all statements within the body keep getting executed repeatedly. • To begin with the variable count is initialised to 1. • Every time the simple interest logic is executed the value of count is incremented by 1.

  13. Other points for while • The condition being tested may use relational or logical operators • The statements within a loop may be a single statement or a block of statements. eg: while (i<=10) i=i+1; is same as while(i<=10) { i=i+1; }

  14. Points contd.. • infinite loops { int i=1; while(i<=10) cout<<i<<endl; }

  15. Points contd… • Instead of incrementing loop counter, we can even decrement int i=5; while(i>=1) { cout<<i<<endl; i=i-1; }

  16. Ponts contd… • It is not necessary that a loop counter must only be an int float a=10.0; while (a<=10.5) { cout<<“hi”<<endl; cout<<“bye”<<endl; a=a+0.1; }

More Related