0

I'm learning C language usnig Turbo C++ compiler and just in time I encountered the two statements:

  • IF (nested with many IFs)
  • IF-else(not nested but continuing else,else and so on)

I was wondering if my idea is correct or not that IF (nested with many IFs) and IF-else(not nested) are the same? Suggestions are well appreciated.

mtszkw
  • 2,717
  • 2
  • 17
  • 31
  • 3
    Might want to show a code example of each so we know what you are talking about – Glenn Teitelbaum Sep 08 '15 at 19:53
  • 2
    Show the actual code you have a question about. Some are equivalent, some are not. – Barmar Sep 08 '15 at 19:53
  • 8
    If _Turbo C++ 3.0_ refers to the software released in 1992 I would really encourage you to get a more modern compiler/development environment. There are several free ones around, for example [Eclipse](http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/marsr). – jpw Sep 08 '15 at 19:53
  • Use a recent [GCC](http://gcc.gnu.org/), consider install some Linux distributions (free). – Basile Starynkevitch Sep 08 '15 at 19:57
  • 1
    http://stackoverflow.com/a/3435105/34824 – abelenky Sep 08 '15 at 20:24

4 Answers4

3

That's only basic logic behind that:

Nested if conditions: IF first condition's value is true, go into the second condition.

if(a > 0)
{
  printf("A is greater than 0\n");
  if(a > 2) printf("A is greater than 0 and 2\n");
}

if-else condition: IF first condition's value is false, go to the next:

if(a > 0) printf("A is greater than zero\n");
else if(a < 0) printf("A is lesser than zero\n");
else printf("A is zero\n");

There is one more instruction that you should know, switch:

switch(a)
{
  case 0: printf("A is zero\n"); break;
  case 1: printf("A is one\n"); break;
  case 5: printf("A is five\n"); break;
  default: printf("A is not 0, 1 or 5\n"); break;
}
mtszkw
  • 2,717
  • 2
  • 17
  • 31
  • "Nested if conditions" as you stated above,If the user doesn't enters number "2" after the execution of the program and we don't create else-if to change the way when condition becomes false.The program will break in the middle or what? – John Counter Sep 08 '15 at 21:03
  • @JohnCounter, the program will not do any specific action for your number then. In this case, there is another instruction `switch`, I edited my post, take a look. – mtszkw Sep 09 '15 at 08:07
2

Nested if is not equivalent to if-else. It can be equivalent to single if with a combined condition, for instance:

if (a == 1) {
    if (b == 2) {
        ...
    }
}

is equivalent to:

if (a == 1 && b == 2) {
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

I guess you rather mean if this:

if(expression){
    //code
}
else{
    if(expression){
        //code
    }
}

is equivalent to this:

if(expression){
    //code
}
else if(expression){
    //code
}

and yes it's absolutely the same. Second one is just better looking way of doing this.

DawidPi
  • 2,285
  • 2
  • 19
  • 41
1

The else if blocks are in fact nested else’s since C and C++ don’t have any special support for “elseif” or “elif” concept (not speaking about the preprocessor directives now). It gets obvious with strict use of blocks and indentation:

if(something) { doSomething(); }
else {
    if(anotherThing) { doAnotherThing(); }
    else {
        if(yetAnotherThing) { doYetAnotherThing(); }
        else
            { doSomethingElse(); }
    }
}

The same code written with the usual else if notation:

if(something) { doSomething(); }
else if(anotherThing) { doAnotherThing(); }
else if(yetAnotherThing) { doYetAnotherThing(); }
else { doSomethingElse(); }

And as Mateusz Kwaśniak has mentioned, you should prefer switch over else if when possible. However, it’s not available for string comparison, for example.

Melebius
  • 6,183
  • 4
  • 39
  • 52