Sie sind auf Seite 1von 6

Dr.S.J.M.

Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-1

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-2

Loop / Iteration Often we need to repeat the execution of a portion of the code for a given number of times or until a specific condition is met. There are three types of loop in c++ to serve this purpose easily. for loop while loop do ...... while loop for loop Assume that sum and i are declared as int. sum =0; for(i=1 ; i<4 ; i++){ sum + = i; } sum = 0; i = 1, sum = 0 + 1 = 1 i = 2, sum = 1 + 2 = 3 i = 3, sum = 3 + 3 = 6 This expression is executed once at the beginning of the loop. It is typically used to
initialize one or more loop variables

This expression is evaluated at the beginning of each loop iteration. If it is true the loop continues, and if it is false execution continues with the statement after the loop

for ( initializing_ expression ; condition ; iteration_ expression ) { statements ........................ This expression is } The semicolons are evaluated at the end of each mandatory but there loop iteration. It is typically statements may be no expression More examples of for loop used to modify the values ....................... or condition of variables initialized in the first expression sum =0; for(int count =1; i<= limit ; i+2){ sum + = i; } --------------for( ; ; ) { ----------------------------} --------------

// Program - P23 #include<iostream> using namespace std; void main( ){ int sum=0; cout<<"sum = "<<sum<<endl; for(int i=1; i<4; i++) { cout<<"i = "<<i; sum +=i; cout<<"\tsum = "<<sum<<endl; }

Infinite loop

Sometimes it may be needed to end a loop prematurely or when a particular situation arises. It can be done by use of a break statement. Note : a loop defines a scope, so variables declared within a loop are not accessible outside the loop. In particular, variables declared in the initialization expression of a for loop are not accessible outside the loop.

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-3

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-4

// Program - P22 /* Shear and moment at every 0.5 ft of a simply supported beam subjected to uniform load */ #include<iostream> using namespace std; void main( ){ double span, udl, reaction, shear, moment; cout<<"\n Span length of the beam in ft = ? "; cin>>span; cout<<"\n Uniformly distributed load = ? "; cin>>udl; reaction = udl*span/2.0; cout<<"\nDistance (ft)\t Shear force (lbs)\tMoment (k-ft)\n"; for(float i=0; i <= span ; i+=0.5){ //upward on the left of section positive shear = reaction - udl*i; //upperface compression +ve moment = reaction*i - udl*i*i/2.0; cout<<"\t"<<i<<"\t\t"<<shear<<"\t\t\t"<<moment<<"\n"; } }

The structure of the while loop is

while (condition ){ statements } statements

Here the condition is evaluated at the beginning of each loop iteration. If it is true, the loop continues and if it is false execution continues with the statement after the loop

The structure of do...while is Here the condition is evaluated at the end of each loop iteration. If it is true, the loop continues and if it is false execution continues with the statement after the loop. The loop statements are always executed at least once

do { statements } while (condition );

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-5

A program segment containing for loop is shown below. sum =0; for(i=1 ; i<4 ; i++){ sum + = i; }

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-6

It can be written using while loop as shown below. sum=0; i=1; while(i < 4) { sum +=i; i++; } sum = 0, i = 1, <-initial values before the while loop i<4? -> true, sum =0 + 1= 1, i =1 + 1 = 2 i<4? -> true, sum =1 + 2 =3, i =2 + 1 = 3 i<4? -> true, sum =3 + 3 =6, i =3 + 1 = 4 i<4 ? -> false -> no more loop see P24 It can be written using do...while as shown below.

sum = 0, i = 1, sum= 0 + 1 =1, sum =1 + 2 =3, sum =3 + 3 =6, no more loop

<-initial values before the do loop i =1 + 1 = 2, i<4? -> true, i =2 + 1 = 3, i<4? -> true, i =3 + 1 = 4, i<4? -> false, see P25

// Program - P24 #include<iostream> using namespace std; void main( ){ int sum=0, i=1; cout<<"sum = " <<sum<<endl; while( i<4 ) { cout<<"i = "<<i; sum +=i; i++; cout<<"\tsum = " <<sum<<endl; } }

sum=0; i=1; do{ sum +=i; i++; } while(i < 4);

//Program - P25 #include<iostream> using namespace std; void main( ){ int sum=0, i=1; cout<<"sum = " <<sum<<endl; do { cout<<"i = "<<i; sum +=i; i++; cout<<"\tsum = " <<sum<<endl; }while( i<4 ); }

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-7

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-8

The switch statement We have seen that we can branch the flow of a program by use of if-else statements or goto statement. switch statement provides a means to select from a fixed set of options depending on the value of an integer expression.

value of expression ?

switch(expression) { case value1 : statement block1 break; case value2 : statement block2 break; ............................ ............................ case valueN : statement blockN break; default : statement blockP break; } statement Q -----------------

switch, case, break, default are keywords. The expression is evaluated first. If one of the case values matches the value of the expression, execution jumps to the statements following that case value. If the no case values matches the value of expression, execution jumps to the default statement. default statement is optional. If there is no default and there is no matching value, execution is transferred to the statement after the switch block. Whenever a break statement is encountered control is transferred to the statement after the switch block. If there is no break statement after a block execution continues to the block for the next case value. Value of expression = value1 Value of expression = value2 -------------Value of expression = valueN Value of expression do not match any of value1... valueN statement block1 no break statement block2 break break

no break ----------break

no break statement blockN break

no break statement blockP statement Q

If no default block

flow of program control for a switch statement

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-9

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-10

//Program - P26 #include<iostream> using namespace std; void main( ){ int i; cout<<"\n i = ? "; cin>>i; switch (i) { case 5: cout<<" block for case 5"<<endl; break; case 6: cout<<" block for case 6"<<endl; break; case 9: cout<<" block for case 9"<<endl; break; default: cout<<" block for default"<<endl; } cout<<" Statement after switch block" <<endl; }

Program P27 is same as P26 except that the break statement for case 6 is not present in P27

//Program - P27 #include<iostream> using namespace std; void main( ){ int i; cout<<"\n i = ? "; cin>>i; switch (i) { case 5: cout<<" block for case 5"<<endl; break; case 6: cout<<" block for case 6"<<endl; case 9: cout<<" block for case 9"<<endl; break; default: cout<<" block for default"<<endl; } cout<<" Statement after switch block" <<endl; }

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-11

Dr.S.J.M.Yasin/CE206_lecture_06_loop&switch / 28 March 2009/P-12

//Program - P29 #include<iostream> using namespace std; void main( ){ char letter; int vowel=0; int consonant=0; cout<<"\n Enter a letter : "; cin>>letter; switch(tolower(letter)){ case 'a': case 'e': case 'i': case 'o': case 'u': vowel += 1; cout<<"\n Vowel\n\n"; break; default: cout<<"\n Consonant \n\n"; } }

//Program - P28 #include<iostream> #include<string> using namespace std; void main( ){ string text; int vowel=0; int consonant=0; cout<<"\n Enter a null terminated line of text ended by \\n, then press enter"<<endl; getline(cin, text); for(int i=0; i<text.length(); i++){ if(isalpha(text[i])) switch(tolower(text[i])){ case 'a': case 'e': case 'i': case 'o': case 'u': vowel += 1; break; default: consonant += 1; } } cout<<" No. of vowels = "<<vowel<<endl; cout<<" No. of consonants = "<<consonant<<endl; }

Note : A switch statement has its own block between braces that encloses the case statements. Any variable declared within a block ceases to exist at the end of the block.

Das könnte Ihnen auch gefallen