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 :$

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

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.