If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

27
If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard

Transcript of If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Page 1: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

If…Else Assessment

By: Yinyu Zhou, Kyle

Toler, and Ben Howard

Page 2: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 1. What does an if-else statement do?– A. It threatens the computer.– B. It tells the program to choose and execute a

fragment of code.– C. It tells the program what to do without input

from user.– D. It terminates the program.

Page 3: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is B. The if-else statement tells the program to choose and execute one or another fragments of code depending on the value of some variable or expressions.

Page 4: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 2. What is conditional branching?– A. It creates a never-ending loop.– B. It branch the program to make more

possibilities in a function.– C. It is when the program jumps to another part

of the program when a condition is met.– D. It hacks into the Legislative branch.

Page 5: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is D. No actually we lied – its C. It is when when a program jumps to a different part of a program base on a certain condition being met.

Page 6: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 3. When is the else block executed?– A. When the program starts.– B. When the user is feeling hungry.– C. When it gets there in the program– D. When the if block is false.

Page 7: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is D. The else is block executed if the if block is false.

Page 8: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment • 4. What is wrong with this program?

Page 9: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

#include <iostream.h>

#include <math.h>

int main()

{

double radius;

//get user input

cout<<"Please enter the radius : ";

cin>>radius;

//act on user input

If radius < 0.0;

cout<<"Cannot have a negative radius"<<endl;

else

cout<<"The area of the circle is "<<3.1416 * pow(radius,2)<<endl;

return 0;

}

Page 10: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• A. Everything is wrong.

• B. The variables aren’t correctly declared.

• C. If radius < 0.0; is wrong.

• D. Everything is right.

Page 11: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is C. It should be if(radius < 0.0). There should be no semicolons after the parentheses and you need the parentheses.

Page 12: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 5. What is the final value of A in the following sequence of statements?

Page 13: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

A : = 5;

B : = 6;

if A > B then

A : = 7;

else

A : = A – 2;

end if;

Page 14: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• A. 3

• B. 5

• C. 7

• D. Error

Page 15: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is A…. 3.

Page 16: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 6. Every if statement must be paired with an else.

A. True

B. False

C. Maybe

Page 17: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The answer is B false

Page 18: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 7. Nested if statements are if statements with another if statements inside the condition.

A. True

B. False

C. Maybe

Page 19: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The answer is B which is false.

Page 20: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 8. What is the value of n after the following C++ code segment is executed?

int n = 2;

if (n) n = 0;

else n = 1;

Page 21: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The answer is 0

Page 22: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 9. What is the value of n after the following C++ code segment is executed?

int n = 1;

if (n) n = 0;

Page 23: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The answer is 0

Page 24: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• 10. Write a complete C++ program that asks the user for the number of watts in a light bulb.  The program should then use a nested if-else if statement (if-else if chain) statement that assigns to the variable lumens the expected brightness of the light bulb whose wattage has been stored in variable watts.  The following table gives the brightness for standard light bulbs:

• Watts            Brightness (in Lumens)   25                     215   60                     880   75                    1000  100                   1675

Page 25: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• Bonus question- What is Mr. Shaw’s favorite Simpsons episode?– A. The first Tree House of Horror episode.– B. When Mr. Burns adopted Bart Simpson.– C. When Lisa turned anorexic.– D. When George Bush moved next door.– E. All of the above.

Page 26: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• The correct answer is D. It is the episode where George Bush moves next door.

Page 27: If…Else Assessment By: Yinyu Zhou, Kyle Toler, and Ben Howard.

Assessment

• Minus 0 = 125% You actually paid attention…. GEEK!

• Minus 1 = 75%

• Minus 2 = 50%

• Minus 3 = 25%

• Minus 4 = 0% Go back and read the PowerPoint again!