• Forum
  • Lounge
  • How would you do your If and else statem

 
How would you do your If and else statements

Pages: 1234
Apr 13, 2014 at 5:57pm
closed account (EwCjE3v7)
I`m trying to adapt to a new style and can`t seem to get it

I used to use

1
2
3
4
5
if () {
   ....
} else {
   ....
}


But I`m trying to get used to this type

1
2
3
if ()
{
}


but I don`t know how I should do a else

should I do it like this
1.
1
2
3
4
5
6
7
if () 
{
   ...
} else 
{
   ...
}


2.
1
2
3
4
5
6
7
8
if () 
{
   ....
} 
else 
{
   ...
}


or another way. Thank you
Last edited on Apr 13, 2014 at 6:03pm
Apr 13, 2014 at 5:58pm
The latter I prefer. Also you still indent after the braces like the first version.


1
2
3
4
5
6
7
8
if(something)
{
    dosomething();
}
else
{
    donothing();
}
Apr 13, 2014 at 6:02pm
closed account (EwCjE3v7)
yea forgot that, thank you

I will do that one
Apr 13, 2014 at 6:08pm
closed account (N36fSL3A)
If the if statement runs a single instruction I usually do this:
1
2
3
4
if(statement)
    runCode();
else
    runThisInstead();


I agree with giblit though, but I like to put a line between the closing bracket and the else statement:

1
2
3
4
5
6
7
8
9
if(something)
{
    dosomething();
}

else
{
    donothing();
}
Last edited on Apr 13, 2014 at 6:22pm
Apr 13, 2014 at 6:21pm
I prefer K&R style (your original style). I use one liners (without braces) only when testing something and prefer to not have them in real code.

My problem with Astyle is that actions are visually separated from condition (braces are thin and easy to overlook). How I see Astyle code at first glance:
1
2
3
4
5
6
7
if (condition)

    action;

else

    another_action;
Apr 13, 2014 at 6:39pm
closed account (EwCjE3v7)
Yea I`m thinking to maybe sticking to my original style(K&R)
Apr 13, 2014 at 7:30pm
1
2
3
4
5
6
7
if (condition) {
        action;
        action2;
} else {
        action3;
        action4;
}
Apr 13, 2014 at 7:33pm
I might be weird or something but I personally find that version hard to read since the braces don't match up.
Apr 13, 2014 at 9:00pm
Providing braces reduces the risks of typos if you ever decide to make the statement more complicated.

I personally like words matching words:
1
2
3
4
5
6
if ( /* condition */ (
{
}
else
{
}
Apr 13, 2014 at 9:03pm
I usually do it like so:

1
2
3
4
5
6
if(condition){    
    ....
}
else{
    ....
}
Apr 13, 2014 at 9:31pm
I might be weird or something but I personally find that version hard to read since the braces don't match up.


Me too. I never really understood the K & R approach.
Apr 13, 2014 at 9:36pm
Because it uses indentation to indicate the level of nested code instead of brackets.
Apr 13, 2014 at 11:01pm
closed account (N36fSL3A)
NoXzema wrote:
1
2
3
4
5
6
7
if (condition) {
        action;
        action2;
} else {
        action3;
        action4;
}
Are you a Java programmer?
Last edited on Apr 13, 2014 at 11:02pm
Apr 14, 2014 at 12:10am
I use the same style, doesn't mean I'm a Java programmer. Though, I myself take it a bit far for some people's liking and go like this:
1
2
3
4
5
6
7
8
9
void func() {
    if (condition) {
        action();
        action2();
    } else {
        action3();
        action4();
    }
}


I guess its a matter of taste. Like how some people prefer putting spaces in between the parentheses and the condition while others don't like spaces at all, or your '*' placement, or your cv-qualifier placement, etc. Personally, I think the only thing that matters is consistency of style.
Apr 14, 2014 at 4:23am
Multiple conditions can also be used
if ()
{
//code to run
}
else if
{
//code to run
}
Apr 14, 2014 at 4:25am
Alam wrote:
Multiple conditions can also be used

The topic is more about coding style preferences not what you can do with if/else statements. :P
Apr 14, 2014 at 4:52am
I do variants.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if (condition1) action1;

else if(condition2) action2;

else action3;

// OR

if(condition){
      action1;
}else{
      action2;
}

// OR
if(condition){
      action1;
}
else{
      action2;
}

// I usually try to use this one when helping with someone else's code
if(condition)
{
      action1;
}
else
{
      action2;
}
Apr 14, 2014 at 6:42am
Are you a Java programmer?
why would that make him a java programmer
Apr 14, 2014 at 6:42am
Functions are an exception... I actually use I guess what's called "Kernel" style, close to K & F. Functions are declared as such:
1
2
3
4
int bob(void)
{
        return a_global_int_cuz_bad_programmer;
}
Apr 14, 2014 at 7:42am
@Little Bobby Tables
Different programming languages have different styles that are associated with them. As a general rule, C is assumed to have a coding style like this:
1
2
3
4
5
6
7
8
if (something)
{
  dosomething;
}
else
{
  dosomethingelse;
}

While Java traditionally is layed out similar to how my code and @NoXzema's code was layed out. Don't ask me why, I don't actually know.
Pages: 1234