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

 
How would you do your If and else statements

Pages: 1234
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
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();
}
closed account (EwCjE3v7)
yea forgot that, thank you

I will do that one
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
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;
closed account (EwCjE3v7)
Yea I`m thinking to maybe sticking to my original style(K&R)
1
2
3
4
5
6
7
if (condition) {
        action;
        action2;
} else {
        action3;
        action4;
}
I might be weird or something but I personally find that version hard to read since the braces don't match up.
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
{
}
I usually do it like so:

1
2
3
4
5
6
if(condition){    
    ....
}
else{
    ....
}
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.
Because it uses indentation to indicate the level of nested code instead of brackets.
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
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.
Multiple conditions can also be used
if ()
{
//code to run
}
else if
{
//code to run
}
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
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;
}
Are you a Java programmer?
why would that make him a java programmer
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;
}
@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