473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

if else

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
Jul 23 '05 #1
12 3916

"Roman Töngi" <ro**********@bluewin.ch> schrieb im Newsbeitrag
news:42**********@news.bluewin.ch...
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
Jul 23 '05 #2
What I mean: an if else statement as constituent part of if does not
exist in c++. Only nesting is possible to simulate this.
Jul 23 '05 #3
* 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
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.

which is different from the first.
Not really.

The VB "elsif" keyword is just syntactic sugaring.

Have I grasped this correctly?


Almost.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
> 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
Jul 23 '05 #5
"Roman Töngi" <ro**********@bluewin.ch> wrote in message
news:42**********@news.bluewin.ch
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.
--
John Carson

Jul 23 '05 #6
"Roman Töngi" wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7
The difference is that else refers to the last if in c++, whereas in
VB it refers to the initial if.
Jul 23 '05 #8
Great, your URL-resource describes exactly that what I am looking for.

Thanks
Roman
Jul 23 '05 #9

"Roman Tvngi" <ro**********@bluewin.ch> wrote in message
news:42**********@news.bluewin.ch...
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
Jul 23 '05 #10
Howard wrote:
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...?


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.

Jul 23 '05 #11

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d4*************@news.t-online.com...
Howard wrote:
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...?


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


Jul 23 '05 #12
Rolf Magnus wrote:

Howard wrote:
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...?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
5
by: Brie_Manakul | last post by:
Is there a way in javascript to do an if else that shows a script in an iframe? Let me know if that doesn't make sense. We have a portal and in a portlet I need to grab these javascript links to...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
8
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
17
by: JRough | last post by:
I'm trying to get error proof code. I have this code which seems to work but now I look at it I think it should be elseif not else and I wonder why it works. It is in the block:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.