Advertisement

Just curious...

Started by August 25, 2002 08:48 PM
2 comments, last by Zeraan 22 years ago
I''m not a newbie to C++, but a thought hit me about the if, else if, and else. Is this
  
if(someCondition)
{
  //code here...

}
else if(someAnotherCondition)
{
  //code here...

}
else if(yetAnotherCondition)
{
  //code here...

}
else
{
  //code here...

}
  
the same as this
  
if(someCondition)
{
  //code here...

}
else
{
  if(someAnotherCondition)
  {
    //code here...

  }
  else
  {
    if(yetAnotherCondition)
    {
      //code here...

    }
    else
    {
      //code here...

    }
  }
}
  
I''m just curious, are they the same in theory?
Yes. The blocks just surround statements and make them one (in theory) to the compiler. If you only have one statement in a block though, there''s no use for it. Look in your code and take out the blocks, it''s the same time. So yea, they''ll work the same.

---
My Site-My Tetris Clone w/Source
Come join us on IRC in #directxdev @ irc.afternet.org
Advertisement
I''m thinking no. The second someone places some other operational statement into one of your else if''s before the next if it will screw up the theory. Therefore no.

In the first example there can be no statements between the else if and the first open braket. In the second example there can be statements between an else if and the next if statement.

However, if everyone places their code inside the nested else if''s then the theory works.

But, someone will screw it up eventually.
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Exactly as written, they are functional equivalents.

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement