0

Hello. I know this is lame questinon but what is multi-if syntax? Like this:

if (a == 0)
       doSometihng();
if (a == 1)
       doSomething();
else
       doElse();

Or like this:

if (a == 0)
       doSometihng();
else if (a == 1)
       doSomething();
else if (a == 2)
       doSomething();
else
       doElse();

I'm useing Microfot Visual C++ 08
Thank you :$

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Fbody
0

It's the second one. Your first snippet is two if statements, one with and one without an else clause.

1

Hello. I know this is lame questinon but what is multi-if syntax? Like this:

if (a == 0)
       doSometihng();
if (a == 1)
       doSomething();
else
       doElse();

Or like this:

if (a == 0)
       doSometihng();
else if (a == 1)
       doSomething();
else if (a == 2)
       doSomething();
else
       doElse();

I'm useing Microfot Visual C++ 08
Thank you :$

They're both syntactically correct, but they accomplish different things. The appropriate one for the situation depends on what you are trying to accomplish.

As Narue mentioned, the first version is 2 separate if statements with the else being associated with the second if. You would use this version if you may need both if statements to potentially be true, (in your example though, this will never happen).

The second version is appropriate if you only need one of the conditions to come true. As soon as one of the conditions is found true, the remaining conditions are ignored. This is sometimes referred to as an "if-else-if ladder".

Edited by Fbody: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.