if else

R

Roman Töngi

In c++ there does not exist an if-statement as for example in Visual Basic,
does it?

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

In c++ only nested if-else-constructs are possible, right?
So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

which is different from the first. Have I grasped this correctly?

Thanks
 
G

Gernot Frisch

Roman Töngi said:
In c++ there does not exist an if-statement as for example in Visual
Basic, does it?

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

In c++ only nested if-else-constructs are possible, right?
So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

which is different from the first. Have I grasped this correctly?

The VB above is:
if(cond)
statement;
else if(cond2)
statement2;
else
statement3;

what your C++ sample wants to do is:

if(cond)
statement;
else
{
if(cond2)
statement2;
else
statement3;

}

HTH,
Gernot
 
R

Roman Töngi

What I mean: an if else statement as constituent part of if does not
exist in c++. Only nesting is possible to simulate this.
 
A

Alf P. Steinbach

* Roman Töngi:
In c++ there does not exist an if-statement as for example in Visual Basic,
does it?

It exists.

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

What you mean is probably VB

if COND then
statement
elsif COND
statement
else
statement
endif

In c++ only nested if-else-constructs are possible, right?
Right.


So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

Yes, except you shouldn't indent this the way you've done.

See section 8 "Use if-else to select from n actions" of chapter 1.4
of my attempted correct C++ tutorial at
which is different from the first.

Not really.

The VB "elsif" keyword is just syntactic sugaring.

Have I grasped this correctly?

Almost.
 
N

Niels Dybdahl

In c++ there does not exist an if-statement as for example in Visual
Basic,
does it?

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

In c++ only nested if-else-constructs are possible, right?
So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

which is different from the first. Have I grasped this correctly?

I do not think that the last example is functionally different from the
first. How should it differ ?

Niels Dybdahl
 
J

John Carson

Roman Töngi said:
In c++ there does not exist an if-statement as for example in Visual
Basic, does it?

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

In c++ only nested if-else-constructs are possible, right?
So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

which is different from the first.

How is it different from the first?

It is not at all clear what you are asking. Visual Basic has an ElseIf
keyword, whereas there is no elseif in C++. Thus in Visual Basic you can
have

If cond1 Then
statement1
ElseIf cond2 Then
statement2
Else
statement3
End If

In practical terms, this has the same effect as

if(cond1)
statement1
else if(cond2)
statement2
else
statement3

in C++.

It is true that each else-if statement in C++ gets you one nesting level
deeper (a kind of "tail nesting"). This may not be so obvious in Visual
Basic, but the logic is the same.
 
K

Karl Heinz Buchegger

Roman Töngi said:
In c++ there does not exist an if-statement as for example in Visual Basic,
does it?

//VB-analogy:
if (cond.)
statement;
else if (cond.)
statement;
else
statement;

In c++ only nested if-else-constructs are possible, right?
So, the above example would have the following logic in c++:

if (cond.)
statement;
else
if (cond.)
statement;
else
statement;

which is different from the first. Have I grasped this correctly?

No.
Both versions are exactly equivalent.
 
R

Roman Töngi

The difference is that else refers to the last if in c++, whereas in
VB it refers to the initial if.
 
R

Roman Töngi

Great, your URL-resource describes exactly that what I am looking for.

Thanks
Roman
 
H

Howard

Roman Tvngi said:
The difference is that else refers to the last if in c++, whereas in
VB it refers to the initial if.

There is no difference.

Show me a case where the code would take a different action given the same
conditions...?

-Howard
 
R

Rolf Magnus

Howard said:
There is no difference.

Show me a case where the code would take a different action given the same
conditions...?

If the above statement is true (and I can't say that for sure since I don't
know VB):

if (a)
;
if (b)
;
else
std::cout << "Ha!\n";

The C++ version will output the text if a is true and b is false, while an
equivalent piece of VB code would print it if a is false. The value of b
doesn't matter in this case.
 
H

Howard

Rolf Magnus said:
If the above statement is true (and I can't say that for sure since I
don't
know VB):

if (a)
;
if (b)
;
else
std::cout << "Ha!\n";

The C++ version will output the text if a is true and b is false, while an
equivalent piece of VB code would print it if a is false. The value of b
doesn't matter in this case.

The original question had "else if" for the second conditional, which is
different from your example, which just uses "if".

In VB, whether using "else if" or its shortcut version "elsif", it is still
equivalent to the C++ code.

-Howard
 
K

Karl Heinz Buchegger

Rolf said:
If the above statement is true (and I can't say that for sure since I don't
know VB):

if (a)
;
if (b)
;
else
std::cout << "Ha!\n";

The C++ version will output the text if a is true and b is false, while an
equivalent piece of VB code would print it if a is false. The value of b
doesn't matter in this case.

Which is not true.
The dangling else problem in VB is dealt with by having an explicite
endif statement.

So the equivalent code in VB would be

if a then
if b then
else
rem output "Ha!"
endif
endif

which does exactly the very same thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top