Sei sulla pagina 1di 2

COMP 2013 – Fundamentals of Problem Solving and Programming 1

JOHN JOSEPH B. YEE SIR. MICHAEL dela FUENTE


BSCS 1-1N
Self-check

Trace the execution of the following nested if statement for a salary of $23,500.00. What will be the value of tax?
if (salary < 0.0)
tax = -1.0;
else if (salary < 15000.00)
tax = 0.15 * salary;
else if (salary < 30000.00)
tax = (salary – 15000.00) * 0.18 + 2250.00;
else if (salary < 50000.00)
tax = (salary – 30000.00) * 0.22 + 5400.00;
else if (salary < 80000.00)
tax = (salary – 50000.00) * 0.27 + 11000.00;
else if (salary < 150000.00)
tax = (salary – 80000.00) * 0.33 + 21600.00;
else
tax = -1.0;

OUTPUT:

Tax: $3,780.00

Write a nested if statement for the decision diagrammed in the following flowchart. Use a multiple-alternative if for
intermediate decisions where possible.

false true
pH >
7

pH is
true pH < true
7 12

false “Neutral” false “Alkaline



true “Very
pH >
2 alkaline”

false “Acidic”

“ Very
Acidic”

CODE FRAGMENT FOR FLOWCHART ABOVE:

if (pH > 7){


if (pH < 12)
printf("Alkaline");
else
printf("Very Alkaline");
)
else{
if (pH == 7)
printf("Neutral");
else
if(pH > 2)
printf("Acidic");
else
printf("Very Acidic");
}
COMP 2013 – Fundamentals of Problem Solving and Programming 1

What will be printed by this carelessly constructed switch statement if the value of color is ‘R’?
switch (color) {
case ‘R’: printf(“red\n”);
case ‘B’: printf(“blue\n”);
case ‘Y’: printf(“yellow\n”);
}

OUTPUT:
red
blue
yellow

Predict the output of this program fragment:

i = 0;
while (i <= 5) {
printf(“%3d %3d\n”, i, 10-i);
i=i+1;
}

OUTPUT:
0 10
1 9
2 8
3 7
4 6
5 5

Trace the execution of the loop that follows for n = 8. Show values of odd and sum after the update of the loop
counter for each iteration.

sum=0;
for (odd = 1; odd < n; odd +=2)
sum = sum + odd;
printf(“Sum of positive odd numbers less than %d is %d.\n”, n, sum);

Trace of Execution:
for (odd = 1; odd < n; odd+=2)
sum= sum + odd

odd= 1+2(T)+2(T)+2(T)+2(F) = 9
sum = 16
1=1+3=4
4=4+5=9
9 = 9 + 7 = 16

Potrebbero piacerti anche