14. What will continue do inside of a for?
It causes the program to update the loop counter and jump to the top of the loop.
15. What will continue do inside of a switch? (This is a trick question.)
This is a trick question because you can’t put a continue inside a switch statement. The
continue statement only works inside a loop such as a for or while loop.
The only way you can put a continue inside a switch is to put the switch inside a loop .
This will put the continue inside a loop. When executed the continue will start at the top
of the loop.
16. What will break do inside of a while?
It causes the program to exit the loop.
17. What will break do inside of a for?
It causes the program to exit the loop.
18. What will break do inside of a switch? (This is not a trick question.)
It causes the program to exit the switch.
19. Discuss the pros and cons of putting the default statement at the end of every switch. Is there a
case where putting the default statement somewhere else might be useful?
Advantage: If you always put the default at the end of the switch , you can always find i t
easily.
Disadvantage: Suppose you want the fifth case of a ten case switch statement to fal l
through to the default case. In this case the “natural” place for the default statement is just
after the fifth case, not the end.
Page 26
Chapter 9: Variable Scope
and Functions
But in the gross and scope of my opinion
This bodes some strange eruption to our state.
— Shakespeare Hamlet, Act I, Scene I
Teacher’s Notes
Note: This is one of the longer chapters. You might want to spend extra time on it.
So far we’ve been using only global variables. That’s because they’re simple and we didn’t want to
confuse the student with a lot of extra material.
In this chapter we introduce students to local variables. First familiarize the students with the concepts of
when variables are created and destroyed. This will prepare them for classes where variable creation and
destruction my result in a function call.
Note: The term class used to refer to a variable’s class has nothing to do with the C++ keyword class.
Up to now, our programs have been in one big section. At this point we start dividing up the programs
into functions.
We’ve been using one function all along called main. Except for the special name, this function is
defined like any other. It has no parameters and returns an integer value. The only tricky part is that
because its name is main, it is “called” by running the program and returns a status to the operating
systems.
Practical C++ Programming by manish baranwal
Start from the beginning
