Programming Fundamentals Programming Fundamentals Instructor : Zuhair Qadir Lecture # 11 30th-November-2013 1
Programming Fundamentals Programming Fundamentals Lecture # 11 2
Switch Control substitute for long if statements that compare a variable to several "integral" values. expression is any legal C++ expression. statements are any legal C++ statements or block of statements. switch evaluates expression and compares the result to each of the case values. evaluation is only for equality; relational operators may not be used here, nor can Boolean operations. switch (expression) case valueone: case valuetwo:... case valuen: default:
Switch If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. If there is no default and there is no matching value, execution falls through the switch statement and the statement ends. switch (expression) case valueone: case valuetwo:... case valuen: default:
int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<< Enter Your Selection: ; cin>>input; switch ( input ) case 1: // Note the colon, not a semicolon cout<< press enter to play game ; case 2: cout<< enter you name to load game ; case 3: cout<< enter all players names to load game ; case 4: cout<< press esc to exit game ; Default: cout<<" bad input, quitting\n ;
Loops & Its Logic What is a Loop? An iterative statement is called loop. A type of control structure that repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met. Repetition A single statement A set of statements loop consists of three main statements:- (1) Initialization (2) Condition (3) Incrementation 611/12/2014
Types of Loop Types of Loop Counter loop (for loop) Conditional loops (while, do while) For Loop Types of Loop While Loop Do While Loop 711/12/2014
For Loop Operation Initialization Expression Test Expression Body of Loop True Increment Expression False Exit 811/12/2014
For Loop Structure Test Expression Initialization Expression keyword for ( j=0 ; j < 5 ; j++ ) Updation Expression Single Statement Loop Body 911/12/2014
11/12/2014 For Loop Structure Initialization Expression Test Expression Updation Expression keyword for ( j=0 ; j < 5 ; j++ ) Multiple Statement Loop Body 10
11/12/2014 For Loop // demonstrate simple for loop # include <iostream> #include<conio.h> void main() int j; for (j=0 ; j < 5 ; j++) cout<<j * j; getch(); Output: 0 1 4 9 16 11
For Loop Example //print number from 1 to 10 using for loop. #include<stdio.h> #include<conio.h> Void main() int i; for ( i=1; i<=10; i++) Cout<< \n I ; getch(); 12
For Loop Example //program to calculate the sum of number from 1 to 5 using for loop. #include<stdio.h> #include<conio.h> Void main() Int i,sum; for ( i=1; i<=5; i++) sum=sum+i; Cout<< sum is = << sum; getch(); 13
11/12/2014 For Loop Example //program to print odd number between 1 to 10. #include<stdio.h> #include<conio.h> Void main() Int i; for( i=1;i<10;i+=2) cout<< even numbers are<< i << \n\n ; getch(); 14
For Loop Example of reverse loop. //print number from 10 to 1 using for loop. #include<stdio.h> #include<conio.h> Void main() Int i; for ( i=10; i>=1; i--) Cout<< i << endl; getch(); 15
11/12/2014 For Loop Variations Multiple initialization and increment Expressions for (x=0, j=0 ; j < 5 ; j++, x++) controlling Increment or Decrement for ( j=5 ; j > 0 ; j-- ) //decrement by 1 for ( j=0 ; j <10 ; j+=2 ) //increment by 2 16
11/12/2014 The Infinite loop A loop that does not terminate is called Infinite loop. A loop who's condition always remain true is called infinite loop. e.g. for(int i=0;i>0;i++) cout<<i<<endl; 17
Resources to read from http://www.cplusplus.com/doc/tutorial/