[prog] Re: more C++

Dominik Schramm dominik.schramm at gmxpro.net
Sat Dec 13 23:54:17 EST 2003


On 12/13/2003 10:27 PM, wolf wrote:

 > I've tried taking the { and } out but I thought if there was an if
 > statement you needed the braces.

The if-else statement looks like this
(everything in "" has to be typed literally!):

"if" "(" condition ")" statement1 "else" statement2

statement1 (and statement2) has either the form:

  simple_statement ";"
  e.g. "x = 1;"

or

  "{" simple_statement ";" simple_statement2 ";" ... "}"
  e.g. "{ x = 1; y = 2; }"

You wrote (condensed and simplified):

  "if (condition) { simple_statement1; else simple_statement2; }"

This is a syntax error.
It should be (always using braces):
"if (condition) { statement1; } else { statement2; }"

Some consider it good style to always use braces in if-else statements.

Note that between the words and characters in "",
you can put as much whitespace and multiline comments as you want
(also none!):
This is legal (but looks ugly in my opinion):
if(         a == b
   )/*just a small comment*/ {do_something();}

 > Is it a little error I'm making that
 > I just don't seem to see? Or am I way off track?

No, you're very close. :-)

regards,
dominik





More information about the Programming mailing list