0% found this document useful (0 votes)
9 views4 pages

Exercise W6Answer

This document contains examples of if/else statements and asks the student to trace outputs and write if/else statements to solve problems. It includes examples of checking conditions, assigning values, and displaying text based on different conditions being met or not met.

Uploaded by

aidilzuraidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Exercise W6Answer

This document contains examples of if/else statements and asks the student to trace outputs and write if/else statements to solve problems. It includes examples of checking conditions, assigning values, and displaying text based on different conditions being met or not met.

Uploaded by

aidilzuraidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise W6part1

Name : ______________________________________________________
Matric: _______________________________
Group:________________________________

1. Trace output for if statement bellow : memory


jumlah = 0; 0

x=12;
if (x > 0)
{
jumlah = jumlah + x;
12
cout<<jumlah<<endl;
}
cout<<”jumlah = “<<jumlah;

output
12
jumlah = 12
2. Trace output for if statement bellow :
jumlah = 0;
0
x=12;
y=9;
if (x > y)
{
jumlah = jumlah + y;
9

cout<<jumlah<<endl;
}
if (x > 2)
{
jumlah = jumlah + x;
21

cout<<jumlah<<endl;
}
cout<<”jumlah = “<<Jumlah;

Output
9
21
Jumlah = 21

3. Write if statement to do the following:

a) Display “healthy” if the body temperature is less than 37 degree


Celsius.

Output
if (temperature < 37)
cout<<”healthy”;

b) Display output as bellow if patient’s age is more than 50


Do the following test :
1) Blood test
2) Blood pressure test
3) Sugar test

Output
if (age > 50)
{
cout<<”Do the following test :”<<endl;
cout<<”1) Blood test”<<endl;
cout<<”2) Blood pressure test”<<endl;
cout<<”3) Sugar test”<<endl;
}

4. Trace output for if else statement bellow :


a= 12;
b = 10;
if (a>b)
cout<<”Girls”;
else
cout<<”Boys”;

Output
Girls

5. Trace the output for the program segment bellow


int number = -10;
if (number < 0)
cout << "Here I am!" << endl;
else
cout << "No, I’m here!" << endl;
cout << "No, actually, I’m here!" << endl;

output
Here I am!
No, actually, I’m here!

6. Write if statement to do the following:

a) Display “excellent” if mark is more than 90, otherwise display


“moderate”;
output
if (mark > 90 )
cout<<”excellent”;
else
cout<<”moderate”;

b) Display “you are allowed to sit for final exam” if attendance is more
than 80 percent, otherwise display “you are not allowed to sit for final
exam”;

output
if (attendance > 80 )
cout<<” you are allowed to sit for final exam”;
else
cout<<” you are not allowed to sit for final exam”;

c) Write an if/else statement that assigns 1 to x if y is equal to 100.


Otherwise it should assign 0 to x.

output
if ( y == 100 )
x = 1;
else
x = 0;

You might also like