Download presentation
Presentation is loading. Please wait.
1
Lecture 7 Sept 23, 2002
2
Switch Structure ► General form: switch (expression) switch (expression) { case constant 1: case constant 1: do this; do this; case constant 2: case constant 2: do this; do this; case constant 3: case constant 3: do this; do this; default: default: do this; 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: {output: int i=2;I am in case 2 int i=2;I am in case 2 switch(i)I am in case 3 switch(i)I am in case 3 {I am in default {I am in default case 1: case 1: cout<<“I am in case 1”<<endl; cout<<“I am in case 1”<<endl; case 2: case 2: cout<<“I am in case 2”<<endl; cout<<“I am in case 2”<<endl; case 3: case 3: cout<<“I am in case 3<<endl; cout<<“I am in case 3<<endl; default: default: cout<<“I am in default<<endl; cout<<“I am in default<<endl; } return 0; return 0; }
5
Example 2 ► int main()output: {I am in case 2 {I am in case 2 int i=2; int i=2; switch (i) switch (i) { case 1: case 1: cout<<“ I am in case 1”<<endl; cout<<“ I am in case 1”<<endl; break; break; case 2: case 2: cout<<“ I am in case 2”<<endl; cout<<“ I am in case 2”<<endl; break; break; case 3: case 3: cout<<“ I am in case 3”<<endl; cout<<“ I am in case 3”<<endl; break; break; default: default: cout<<“ I am in default”<<endl; cout<<“ I am in default”<<endl; } return 0; return 0; }
6
Example 3 #include #include int main() int main() { int a; int a; cout<<“Enter a number”; cout<<“Enter a number”; cin>>a; cin>>a; switch(a) switch(a) { case 1: case 1: cout<<“Life is good\n”; cout<<“Life is good\n”; break; break; case 2: case 2: cout<<“Life is bad\n”; cout<<“Life is bad\n”; case 3: case 3: case 4: case 4: case 5: case 5: cout<<“Life is beautiful\n”; cout<<“Life is beautiful\n”; } return 0; return 0; }
7
Example 4 ► int main()output: {I am in case x {I am in case x char c = ‘x’; char c = ‘x’; switch (c) switch (c) { case ‘v’: case ‘v’: cout<<“ I am in case v\n”); cout<<“ I am in case v\n”); break; break; case ‘a’: case ‘a’: cout<<“ I am in case a\n”); cout<<“ I am in case a\n”); break; break; case ‘x’: case ‘x’: cout<<“ I am in case x\n”); cout<<“ I am in case x\n”); break; break; default: default: cout<<“I am in default”<<endl; cout<<“I am in default”<<endl; } return 0; return 0; }
8
Repetition Structures ► Programming till now involved input, output, assignment and selection capabilities. and selection capabilities. ► Many problems require a repetiton capability in which the same calculation or sequence of instructions is repeated same calculation or sequence of instructions is repeated ► Repetition structures are used for this purpose.
9
Types of Repetition structure Repetition Structures Whilefordo while
10
While loops initialise loop counter; initialise loop counter; while (test loop counter using condition) while (test loop counter using condition) { do this; do this; and this; and this; increment loop counter; increment loop counter; }
11
Example 1 ► /* Calculation of simple interest for 3 sets of p, n and r */ int main() int main() { int p,n,count; int p,n,count; float r, si; float r, si; count=1; count=1; while(count<=3) while(count<=3) { cout<<“Enter values of p,n,r”<<endl; cout<<“Enter values of p,n,r”<<endl; cin>>p>>n>>r; cin>>p>>n>>r; si = p*n*r/100; si = p*n*r/100; cout<<“Simple interst = “<<si<<endl; cout<<“Simple interst = “<<si<<endl; count=count+1; count=count+1; } return 0; 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 1. The condition being tested may use relational or logical operators 2. The statements within a loop may be a single statement or a block of statements. eg: while (i<=10) eg: while (i<=10) i=i+1; i=i+1; is same as is same as while(i<=10) while(i<=10) { i=i+1; i=i+1; }
14
Points contd.. ► infinite loops { int i=1; int i=1; while(i<=10) while(i<=10) cout<<i<<endl; cout<<i<<endl; }
15
Points contd… ► Instead of incrementing loop counter, we can even decrement int i=5; int i=5; while(i>=1) while(i>=1) { cout<<i<<endl; cout<<i<<endl; i=i-1; i=i-1; }
16
Ponts contd… ► It is not necessary that a loop counter must only be an int float a=10.0; float a=10.0; while (a<=10.5) while (a<=10.5) { cout<<“hi”<<endl; cout<<“hi”<<endl; cout<<“bye”<<endl; cout<<“bye”<<endl; a=a+0.1; a=a+0.1; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.