Académique Documents
Professionnel Documents
Culture Documents
16/2/2012
Computing For Engineers Control Structures
Learning Outcomes: At the end of the session, the students are able: To understand and use the control structures in programming. To understand the concept of if, if else, if else if - else and switch - case statement.
Sample Code 1: If statement The program code shown below reads an integer and determines and prints whether it is odd or even.(Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.) #include<iostream> using namespace std; int main() { int num; cout<<"Enter a number:"<<endl; cin>>num; if(num %2==0) cout<<"The number"<<num<<"is even"<<endl; if(num %2!=0) cout<<"The number"<<num<<"is odd"<<endl; system("pause"); }
Sample Code 2: If else statement int main() { int num; cout<<"Enter a number:"<<endl; cin>>num; if(num <=10) { cout<<num<<"is less and equal to 10"<<endl; } else { cout<< num<<"is greater than 10 "<<endl; } system("pause"); } Sample Code 3 : If else-if - else int main () { char mood; cout<<"Please answer (Y) for yes, and (N) for no."<<endl; cout<<"Are you happy today?"; cin>>mood; cout<<endl; if ((mood=='y')||(mood == 'Y')) { cout<<"I am happy too!"<<endl; } else if ((mood=='n')||(mood == 'N')) { cout<<"Cheer up!!"<<endl; }
Computing For Engineers Control Structures
else { cout<<"You entered wrong answer.."<<endl; } cout<<endl; return 0; } Sample Code 4 (a): Switch Case The program code shown below uses the switch - case using numbers. int main() { int y = 0; cout<<"Enter number "<<endl; cin>>y; switch(y) { case 0: cout<<"You entered zero "<<endl; break; case 1: cout<<"You entered one "<<endl; break; case 5: cout<<"You entered five "<<endl; break; default:cout<<"Number is not in the list "<<endl; } return 0; }
The program code shown below uses the switch case using characters. int main() { Char y; cout<<"Enter character "<<endl; cin>>y; switch(y) { case'A': case'a': cout<<"Range is: 100 - 70 "<<endl; break; case'B': case'b':cout<<"Range is: 69 - 40 break; "<<endl;
case'C': case'c':cout<<"Range is: 39 - 0 "<<endl; break; default:cout<<"Character not in the list "<<endl; } }
Questions 1) Draw the flowchart for each sample code. 2) Change the if else-if else statement in sample code 3 to switch-case statement. 3) Change switch-case statement in the sample code 4(a) and (b) to if else-if else statement.
# include <iostream> using namespace std; int main () { char mood; cout << "please answer (Y) for yes, or (N) for no."<< endl; cout << " are you happy today ?" ; cin >> mood; cout << endl;
switch (mood) {
case 'Y': cout << " i am happy too "<< endl; break; case 'N': cout << " cheer up !!" << endl; break;
default: cout << " you entered wrong answer .." << endl; } system ("pause"); }
# include <iostream> using namespace std; int main ( ) { int y = 0; cout << " Enter a value : " << endl; cin >> y; cout << endl;
else if (y ==1) { cout << " you entered one " << endl; } else if ( y == 5) { cout <<" you entered five" << endl; } else { cout << " you entered a valuse that is not on the list " << endl; } system ("pause"); }
Computing For Engineers Control Structures
Sample Code 4b : If else-if - else # include <iostream> using namespace std; int main () { char y;
cout << "Enter a character :" << endl; cin >> y; cout << endl;
if (( y == 'A') || ( y== 'a')) { cout << " Range is: 100 - 70" << endl; } else if (( y == 'B')||(y == 'b')) { cout << "Range is 69 - 40" << endl; } else if (( y == 'C')||( y == 'c')) { cout << "Range is 39 - 0" << endl; } else { cout << "character is not on the list " << endl; } system ("pause"); }
Computing For Engineers Control Structures
Flowchart 1
Start
Enter a number
No
Num %2! = 0
Yes
No
End
Computing For Engineers Control Structures
Flowchart 2
Start
Enter a number
No
End
Flowchart 3
Start
No
Yes N n
Cheer up!
No
End
Start
Flowchart 4a
Enter a character
A a
Yes
No
B b
Yes
Range is: 69 - 40
No
Yes C c
Range is: 39 0
No
Default
End
Start
Flowchart 4b
Enter number
Case 0
Yes
No
Case 1
Yes
No
Yes Case 5
No
Default
End