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.

SYNTAX<br />

▼<br />

▼<br />

TIP<br />

TIP<br />

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

The <strong>C++</strong>Builder Code Editor has a handy function to help you f<strong>in</strong>d<br />

match<strong>in</strong>g braces. Position the cursor on the brace for which you want<br />

to f<strong>in</strong>d the correspond<strong>in</strong>g brace. Press either the Alt+[ or the Alt+] key<br />

comb<strong>in</strong>ation, and the cursor will be positioned at the brace you are<br />

look<strong>in</strong>g for. It doesn’t matter whether you start on the open<strong>in</strong>g brace or<br />

the clos<strong>in</strong>g brace. In either case, the match<strong>in</strong>g brace will be located.<br />

If a section of code conta<strong>in</strong>s more than two or three consecutive if<br />

statements test<strong>in</strong>g for different values of the same variable, it might be a<br />

candidate for a switch statement. The switch statement is discussed<br />

later <strong>in</strong> this chapter <strong>in</strong> the section “The switch Statement.”<br />

Earlier I mentioned <strong>C++</strong> shortcuts. There is a shortcut for the if-else comb<strong>in</strong>ation. Look<br />

at the follow<strong>in</strong>g code:<br />

if (direction == EAST) lost = true;<br />

else (lost = false);<br />

These two l<strong>in</strong>es can be condensed <strong>in</strong>to a s<strong>in</strong>gle l<strong>in</strong>e:<br />

direction == EAST ? lost = true : lost = false;<br />

Although this shortcut notation might look a little odd at first, you will quickly learn to<br />

recognize it when you see it. The if statement is heavily used <strong>in</strong> <strong>C++</strong>. It’s pretty straightforward,<br />

so you won’t have any trouble with it. The ma<strong>in</strong> th<strong>in</strong>g is keep<strong>in</strong>g all of the braces<br />

straight.<br />

The if statement, Form 1:<br />

if (cond_expr) {<br />

true_statements;<br />

}<br />

else {<br />

false_statements;<br />

}<br />

If the conditional expression, cond_expr, is true (nonzero), the block of code represented by<br />

true_statements is executed. If the optional else clause is specified, the block of code<br />

represented by false_statements is executed when the conditional expression, cond_expr, is<br />

false.<br />

43<br />

2

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

Saved successfully!

Ooh no, something went wrong!