How would you do your If and else statements

Pages: 1234
Apr 17, 2014 at 10:22pm
Personally I don't think anyone on here could explain there whole style because its just so many rules to adapt to.
its not too difficult to explain a style...
Apr 17, 2014 at 10:31pm
closed account (3hM2Nwbp)
I usually just type it out however, and then format my entire project 100% perfectly and accurately within a few seconds by using my IDE's automatic functionality.

This works for all style formats, even custom ones (assuming people use a modern, functional IDE).
Apr 18, 2014 at 12:40am
1
2
3
4
5
6
7
switch(...)
{
    default:
    {
    }
    break;
}

That's how I do my if-else, cause it's way more efficient... (try if/else and switch a million times and see which one's better)
Apr 18, 2014 at 1:53am
closed account (3hM2Nwbp)
@IWishIKnew -- I wouldn't be so quick to make that claim. Often times, compilers may opt to generate a switch-like structure in the resulting object code from an if/elseif chain. Likewise the same may be said about a compiler generating a simple branch if a switch has few paths. I would suspect that switches are a little bit more tricky to get right with branch-prediction optimizations like simple branches are.

Optimizations are also often times compiler-specific. One compiler might spit out drastically different binary than another.

* Just to be clear, I make no claims at being an optimization guru. I have next to no experience in any assembler level object code other than x86 (that is to say, no experience from technology in the past 10 years).
Last edited on Apr 18, 2014 at 1:55am
Apr 18, 2014 at 7:05am
I can't rely on my IDE, if I don't format it as I type, I risk mis-matching a brace and that's a problem I don't have time for.

I use auto-formatting when importing code from someone else (another developer, not an external library).

For switches I like this:
1
2
3
4
5
6
switch(...)
{
default:
    code here
    break;
}
Apr 18, 2014 at 7:08am
also, switches only work the same as if chains if and only if each operator is ==. if you want to compare using < > <= then you would need to use if blocks. also, you can only use chars, ints, bools, and floats (i think. never tested it)
Apr 18, 2014 at 10:19am
closed account (EwCjE3v7)
@Little Bobby Tables
No you cannot use floats, only integral values. Well you can use float but it will be turned into a int by taking away the decimal part
Apr 18, 2014 at 11:05am
hmm i thought as much. never really had a need for it though so i assumed yes
Apr 18, 2014 at 11:28am
1
2
3
4
5
6
7
8
float test = 0.4f;
switch(test > 0.39f && test < 0.41f)
{
    case 1:
        break;
    default:
        break;
}


(That's the nearest you can get)
Last edited on Apr 18, 2014 at 11:29am
Apr 18, 2014 at 10:14pm
What is preferable this:
for (int i = 0; i < 5; i++)
Or this:
1
2
int i;
for (i = 0; i < 5; i++)

I always initialize i within the for statement.
Apr 18, 2014 at 10:24pm
it depends. in the first one the i doesnt exist past the for statement. in the second it has a a longer scope. if you are going to be using it multiple times then the bottom, otherwise the first
Apr 18, 2014 at 10:27pm
Don't forget, with the new standard some prefer this version of the first method:
 
for (auto i = 0; i < 5; i++)
Apr 18, 2014 at 10:29pm
i doesnt exist past the for statement

That is only in some compilers. I was just wondering because my tutor told me that he usually prefers the second one.
Apr 18, 2014 at 10:34pm
That is only in some compilers.

What?! If compilers let you access i outside of the scope of for (int i = 0; i < 5; i++){} then never touch those compilers as it should go out of scope and not be accessible.

For example:

1
2
3
4
5
6
for(int i = 0; i < 5; i++)
{
      std::cout << i << std::endl;
}

std::cout << i << std::endl; // should return a compile error for being out of scope 


g++ -g -pg -std=c++11 -Wall -c "ex2_33.cpp"  (in directory: /home/equinox/project/tests/src)
ex2_33.cpp: In function ‘int main()’:
ex2_33.cpp:22:23: error: ‘i’ was not declared in this scope
  std::cout << "\n" << i << std::endl;
                       ^
Compilation failed.



Last edited on Apr 18, 2014 at 10:40pm
Apr 18, 2014 at 10:39pm
Drrockso wrote:
That is only in some compliant compilers.

Fixed.


Drrockso wrote:
I was just wondering because my tutor told me that he usually prefers the second one.

I would view said tutor's advice with suspicion.

I generally use another form of loop when I must access the counter afterwards. If I have to access the value after the loop to find out what it was, the condition for continuing the loop obviously doesn't just depend on the counter's value.
Apr 19, 2014 at 12:19am
Keep in mind: the tutors ae students themselves. You should call into question their credibility if they say somthing questionable. That doesn't mean that the tutor is wrong for prefering the latter example, but the latter example is somwhat..... un-conventional? In any case, I wouldn't declare the for-loop's variable outside of it unless I was doing somthing really special with it.
Apr 19, 2014 at 12:54am
Scope should be kept as limited as possible.

Edit: Before C99, you couldn't declare a variable from inside the for loop.
Last edited on Apr 19, 2014 at 12:55am
Apr 19, 2014 at 12:56am
Before C99, I believe all variables had to be declared before any statements. Thank goodness that absurd restriction got removed.
Apr 19, 2014 at 1:19am
warning: ISO C90 forbids mixed declarations and code [-Wpedantic]

... which is what you're talking about. ( guess I'm not using that :) )
Last edited on Apr 19, 2014 at 1:22am
Topic archived. No new replies allowed.
Pages: 1234