WiredWX Christian Hobby Weather Tools
Would you like to react to this message? Create an account in a few clicks or log in to continue.

WiredWX Christian Hobby Weather ToolsLog in

 


descriptionC help - "if else" EmptyC help - "if else"

more_horiz
I just want to ask, is this right or wrong.

My teacher told me to do like this:

Code:

if()
{
command 1;
command 2;
}
else
{
command 1;
command 2;
}


Can i use this ?

Code:


if() command 1,command 2;
else
command 1,command 2;


I'm not sure, please help.



Let me think

............................................................................................

C help - "if else" V45u80

descriptionC help - "if else" EmptyRe: C help - "if else"

more_horiz
No you can't. The reason is the braces ( {} ) tell the compiler where the if part of the if-else statement start and end, and where the else part starts and ends. You can leave the braces out ONLY if you want to execute a SINGLE line of code in these sections. Also each command ends with a semicolon ( ; ) not a comma ( , ).

However I think it is better to always include them. The reason for this is that if you need to debug by putting in an extra line you won't forget to insert the braces that would otherwise cause an error if left out.

............................................................................................



There are no stupid questions, only those too stupid to ask for help.

descriptionC help - "if else" EmptyRe: C help - "if else"

more_horiz
OK. Thanks... If i need something more, i'll ask Smile...

............................................................................................

C help - "if else" V45u80

descriptionC help - "if else" EmptyRe: C help - "if else"

more_horiz
You can do this though...

Code:

if (condition) { //code here }
else { //more code }


As long as you have the braces then you are good Big Grin

descriptionC help - "if else" EmptyRe: C help - "if else"

more_horiz
Excuse me but that is just plain wrong. The comma operator does indeed allow you to write things like:

Code:


if( condition )
    command1, command2;
else
    command1, command2;


That is perfectly valid C (and C++ for that matter, where you can even overload the ',' operator to do weird stuff). It will do just what you expect it to. That is, the same as:

Code:


if( condition )
{
    command1;
    command2;
}
else
{
    command1;
    command2;
}



Now just because you *can* do it doesn't mean you should. There is no reason to obfuscate your code doing stupid stuff like that without a specific reason. As darkagn also mentions the braces are not required, but you should include them for readability. Especially in a school assignment where their proper use show you have a firm grasp of the syntax, and *especially* when your teacher tells you to do it. Using A when told to use B just to show that you're a "cool hacker" would probably have a negative impact on your grades. Noone likes a smart-ass. Smile...

One place where it *is* permissible is in a for loop where you want to use 2 variables like:

Code:


for(i = 0, j = 10; i < 10; i++, j--)
      {
      ... rest of loop ...
      }


See e.g.
http://c-faq.com/~scs/cclass/int/sx4db.html
http://stackoverflow.com/questions/1613230/uses-of-c-comma-operator

Regards.

descriptionC help - "if else" EmptyRe: C help - "if else"

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum