Practical C++ Programming by manish baranwal

Start from the beginning
                                        

8. What is grep and how would you use it to find all references to the variable total_count?

The utility grep searches for words (actually strings) in a set of files. To find everywhere

total_count is used on DOS execute the command :

grep “total_count” *.cpp *.h

or on UNIX type:

grep “total_count” *.cc *.h

Page 23

Chapter 8: More Control

Statements

Grammar, which knows how to control even kings...

— Molière

Teacher’s Notes

Here’s where we teach students about the remainder of the control statements. These include for,

switch, and break.

The for statement you find in other languages is nothing like the for statement in C++. It allows the

programmer to explicitly control the way the control variable moves through the data. For example, in

the STL chapter we will use something like:

for (cur_item = list.begin(); cur_item != list.end();

++cur_item)

// ...

to loop through a linked list.

Some languages prohibit you from changing the control variable inside of a loop. C++ contains no such

limitation. You can change the control variable at will. The figure on Slide 4 is very good illustration of

how C++ treats the for statement. It’s merely a repackaging of statements that could easily go into a

while statement.

The switch statement is somewhat complex. There are several style rules we always follow to make our

code easier to read and more reliable. These are:

• Always end each case statement with break or // Fall Through.

• Always put a break at the end of the last case.

• Always include a default case even if it is only:

default:

// Do nothing

break;

Finally we introduce the switch, break, and continue statements. These statements can be tricky. The

break statement exits a switch or looping statement (for, while). The continue statements starts a loop

over from the top. It works only on loops not on switches.

Live Demonstration

Slide 5 cent/cent.cpp

Slide 6 seven/seven.cpp

Page 24

Slide 10 calc-fig/calc.cpp

Classroom Presentation Suggestions

Emphasis should be placed on the fact that for is merely a repackaging of the while loop. This is

illustrated by the figure on Slide 5.

If possible, show the programs on Slide 5 and Slide 6 live. Single step through them to show the students

what happened.

You've reached the end of published parts.

⏰ Last updated: Nov 04, 2012 ⏰

Add this story to your Library to get notified about new parts!

Practical C++ Programming by manish baranwalWhere stories live. Discover now