If-statements: Scope


1
2
3
4
5
6
7
if(true)
    cout<< "what's the difference";

if(!false)
{
    cout<< "between this and the previous if statement?"<< endl;
}


One has a block scope, the other does not, but is there anything else about them that I should know?

Also, loops:

1
2
//same thing
for(unsigned int x = 0; x < unknown_amount; x++) cout<< "lopping whatever"<< endl;


I generally stick to using the brackets, but it would make writing code a bit faster...
Last edited on
The one without brackets only executes one statement. The one with brackets executes every statement within the block. That's the only difference.

Keep using brackets. It's easy to screw up if you don't use brackets.
It's so easy just to cast your eye down the code and pinpoint problems with the layout nicely bracketed.
eh... I suppose...

@sirolou:

That's implied. The statements with brackets are what's called block scope. You'll learn about those.

Also, no. I can have it execute more than 1:

1
2
if(true)
    do_this(), do_that(), (cout<< "Done!"<< endl);
Last edited on
Topic archived. No new replies allowed.