12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

▲<br />

default:<br />

dflt_statements;<br />

break;<br />

}<br />

Wad<strong>in</strong>g In Deeper<br />

The switch statement offers a way to execute different blocks of code depend<strong>in</strong>g on various<br />

values of an expression (expr). The block of code represented by statements_1 is executed<br />

when expr is equal to value_1, the block of code represented by statements_2 when expr is<br />

equal to value_2, and so on through the block of code represented by statements_n when<br />

expr is equal to value_n. When expr is not equal to any of value_1 through value_n, the block<br />

of code at dflt_statements is executed. The break statements are optional.<br />

Learn<strong>in</strong>g About Scope<br />

The term scope refers to the visibility of variables with<strong>in</strong> different parts of your program. Most<br />

variables have local scope. This means that the variable is visible only with<strong>in</strong> the code block<br />

<strong>in</strong> which it is declared. Take a look at the program <strong>in</strong> List<strong>in</strong>g 2.3.<br />

NEW TERM<br />

List<strong>in</strong>g 2.3. SCOPE.CPP.<br />

The term scope refers to the visibility of variables <strong>in</strong> different parts of your program.<br />

1: #<strong>in</strong>clude <br />

2: #<strong>in</strong>clude <br />

3: #pragma hdrstop<br />

4: <strong>in</strong>t x = 20;<br />

5: void CountLoops(<strong>in</strong>t);<br />

6: <strong>in</strong>t ma<strong>in</strong>(<strong>in</strong>t, char**)<br />

7: {<br />

8: <strong>in</strong>t x = 40;<br />

9: <strong>in</strong>t i = 0;<br />

10: cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!