160 likes | 175 Views
Learn about switch structure in C++ programming, including syntax, execution flow, and illustrated examples. Explore repetition structures like while loops with practical examples and explanations.
E N D
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 • 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)
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; }
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; }
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; }
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; }
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.
While loops initialise loop counter; while (test loop counter using condition) { do this; and this; increment loop counter; }
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; }
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.
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; }
Points contd.. • infinite loops { int i=1; while(i<=10) cout<<i<<endl; }
Points contd… • Instead of incrementing loop counter, we can even decrement int i=5; while(i>=1) { cout<<i<<endl; i=i-1; }
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; }