Vous êtes sur la page 1sur 13

COMPUTING FOR ENGINEERS (EE209) Laboratory report LAB ACTIVITY 3: CONTROL STRUCTURES

Walid Nabil Osman Ali ID #: 1000924355

SCHOOL OF ENGINEERING FACULTY OF ENGINEERING, ARCHITECTURE & BUILT ENVIRONMENT

16/2/2012
Computing For Engineers Control Structures

Computing For Engineers


Lab Activity3: Control Structures Objectives: To be familiar with one of the control structures type: selection statements.

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.

Apparatus: PC with C++ compiler

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"); }

Computing For Engineers Control Structures

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; }

Sample Code 4 (b): Switch Case


Computing For Engineers Control Structures

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.

Computing For Engineers Control Structures

Sample Code 3 : Switch case

# 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"); }

Computing For Engineers Control Structures

Sample Code 4a: If else statement

# include <iostream> using namespace std; int main ( ) { int y = 0; cout << " Enter a value : " << endl; cin >> y; cout << endl;

if ( y == 0) { cout << "you entered zero" << 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

Num Yes %2 == 0 Display number is even

No

Num %2! = 0

Yes

Display number is odd

No

End
Computing For Engineers Control Structures

Flowchart 2

Start

Enter a number

Yes Num <= 10 Number is less and equal to 10

No

Number is greater than 10

End

Computing For Engineers Control Structures

Flowchart 3
Start

Are you happy?

Enter y for yes, or N for No

Yes Y y I am happy too!

No

Yes N n

Cheer up!

No

You entered a wrong character.

End

Computing For Engineers Control Structures

Start

Flowchart 4a

Enter a character

A a

Yes

Range is: 100 - 70

No

B b

Yes

Range is: 69 - 40

No

Yes C c

Range is: 39 0

No

Default

End

Computing For Engineers Control Structures

Start

Flowchart 4b

Enter number

Case 0

Yes

You entered zero.

No

Case 1

Yes

You entered one.

No

Yes Case 5

You entered five.

No

Default

End

Computing For Engineers Control Structures

Vous aimerez peut-être aussi