Why is there no else + loop statements in C++?

I find logical occurances all over the place for things like if(condition) else while(condition is true)

Yet instead I have to waste time and do

If(condition 1)
Else if(condition 2)
{
While(condition 2)
}

Having to write the same conditipn twice.

I could say the same for else do while, else for, etc.

These forms don't exist in C++ correct? Do they exist in any other language? C#, VB?

Could I make my own library implementing this in C++? With a header file or somesuch?

Also, what is the significance of this strange notation i see people use? Instead of cout << they do std::cout, what is the purpose of the "std" notation?
Last edited on
a condition is interpreted as a boolean.

simply make a bool isVisible= myCondition;

if(isVisible)
{
}
else
{

}

std is a namespace for standard libraries . :: is a scope operator.
re: if(condition1) else if(condition2) while(condition2)

You are adding an unnecessary condition to the problem. Condition 1 and 2 do not intersect! Reduce it to:

    if(condition2) while(condition2)

which is identical to:

    while(condition2)

Your original problem statement is then simply:

    if(condition1) statement1; else while(condition2)

If 'statement1' is empty, you can reduce it further to:

    if(!condition1) while(condition2)

No language can reduce it further than that; you cannot skip logic.


re: "Could I make my own library implementing [syntax X] in C++?"

RLM (Radical Language Modification) is something of a black art, and a very limited one in C and C++ as the languages offer no support for it. For 'simple' things, you can use macros. For example, Boost's "foreach" library.

For more extensive modifications to the language's syntax you would need to add a custom preprocessor to the compilation stage -- which isn't that difficult, actually, but it is a non-portable thing to do.

The greater problem with it is that such a language is no longer C or C++; it is a different language. As a result, it has less utility than straight-up C and C++.

Beyond that, though, is that you are looking at things the wrong way. C and C++ work well because there are idioms associated with how things are done. It is best to use the native idioms to do stuff -- the code produced will be the most correct, maintainable, and efficient compared with any perceived benefit from RLMs.


re: What is this "std::" stuff?

http://www.cplusplus.com/doc/tutorial/namespaces/#namespace
(It is worth your time to read the entire page, and not just the part on namespaces.)

Namespaces is an old idea designed to modularize source code. The C++ Standard Library is encapsulated in the "std" namespace.

As to why people prefer things like std::cout instead of cout, see here:
https://isocpp.org/wiki/faq/coding-standards#using-namespace-std

Hope this helps.
Topic archived. No new replies allowed.