[prog] Re: more C++

Mary mary-linuxchix at puzzling.org
Sun Dec 14 11:16:06 EST 2003


On Sat, Dec 13, 2003, wolf wrote:
> Well this works but I got the answer via trial and error : )
> Could anyone help explain very fundamentally why it works when I 
> comment out the one line? : )
> 
> 
> #include <iostream>
> 
> 
> int main()
> 
> 
> {
> 
> int integer1;
> 
> std::cout << "Enter a whole number\n";
> std::cin > > integer1;
> 
> 
> 
> if(integer1 % 2 == 0)
> 
> std::cout << "Your number is even." <<  std::endl;

If statements work like this:

    if (something)
        do something;

There can only be one "do something". The "do something" command ends at
the first semicolon ;

Since the "do something" ends at the first semi-colon, and every C++
statement needs to end in a semicolon, if you wanted to do several
things, you would need to use braces:

    if (something)
    {
        do something;
        do something else;
    }

Strictly speaking, it works something like this:
    if (something)
        block of code

A block of code is *either*:
    - a single statement ending in a semi-colon (like your code and my
      first example)
    - many statements (and maybe more complex code) enclosed in curly
      braces { }

> else //(integer1 % 2 !=0)
> 
> std::cout << "Your number is odd." << std::endl;
> 
The syntax

else (something)

is not correct.

When you say "doesn't work" by the way, you need to distinguish between
"doesn't compile" -- which I suspect this didn't until you put your //
in to comment the (integer1 % 2 !=0) bit out -- and "does compile but
doesn't do what I want". As a beginner, a lot of your time might be
spent just getting the compile errors out. As you advance, a lot of your
time will be spent with code that compiles perfectly but doesn't do what
you want (this is what programmers mean by "a bug").

Anyway, I suspect this didn't compile, but it's helpful if you tell
people "I want help compiling this" or "I want help making this do what
I want".

If you want to say "if this, do that, if something else, do something
else instead", the syntax is:

if (something)
    block of code
else if (something else)
    block of code

Notice the "else if" -- your code only has "else".

An alternative is "if this, do that; in all other cases, do something
else". The syntax for this is:

if (something)
    block of code
else
    block of code

When you commented out "(integer1 % 2 !=0)" this is what your code
turned into, and so it worked (if I'm reading the code right, it both
compiled *and* did what you wanted).

You can combine the two into

if (something)
    block of code
else if (something)
    block of code
else if (something)
    block of code
else
    block of code

if you have a situation that requires that.

So, in summary, either:

    if(integer1 % 2 == 0)
        std::cout << "Your number is even." <<  std::endl;
    else 
        std::cout << "Your number is odd." << std::endl;

or:

    if(integer1 % 2 == 0)
        std::cout << "Your number is even." <<  std::endl;
    else if (integer1 % 2 !=0)
        std::cout << "Your number is odd." << std::endl;

[note the "else if"!] would have worked, and you happened to stumble by
trial-and-error on the first case.

-Mary


More information about the Programming mailing list