Lecture 7 - Lecture 7 Switch Structure switch(expression case constant 1 do this case constant 2 do this case constant 3 do this default do this

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 7 Sept 23, 2002 Switch Structure switch (expression) { case constant 1: do this; case constant 2: do this; case constant 3: do this; default: do this; } General form: 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 () { int i=2; switch(i) { 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; } output: I am in case 2 I am in case 3 I am in default Example 2 int main() { 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; } output: I am in case 2 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() { 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; } output: I am in case x 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. Types of Repetition structure Repetition Structures While for do while While loops initialise loop counter; while (test loop counter using condition) { do this; and this; increment loop counter; } 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; } /* Calculation of simple interest for 3 sets of p, n and r */ Example 1 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 2. The statements within a loop may be a single statement or a block of statements. eg: while (i<=10) i=i+1; 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=i1; } 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; } ...
View Full Document

This document was uploaded on 07/06/2011.

Ask a homework question - tutors are online