开发者

for loops and if statements without {}s?

They are pretty popular but I never have the "balls" to use them.

Basically, I never know where they are going to stop. Do they just execute the next line of code?

If I do:

for(int i = 0; i<10; i++)
    if(i % 2 == 0)
        //this is included in the for loop and the if statement
        function();

        // is this?
        function2();

    // where does it stop?
    function3();

What about including an else statement?

for(int i = 0; i<10; i++)
    if(i % 2 == 0)
        //this is included in the for loop and the if statement
        function();
    else
        // is this run as part of the for loop? even though theres a semi colon before it?
        function2();

If I do this...

if((int)1 == (int)1) function1(); function2(); function3(); function4();

is all of that code run?

what about this?

if((int)1 == (int)1) function();

function2();



function3(开发者_JAVA技巧);

Does it just run until the next ;?

Thanks Tom


An if/else/for/foreach/while/do will only execute the very next statement if it doesn't have {}. That includes:

if((int)1 != (int)1) function1(); function2(); function3(); function4();

the first function will not run because the if is not met, the others are not included in the if

The general rule is always use {}, it makes the code more clear and readable


Depends on the language.

In many languages, basically most of the C derivatives, an if-statement only executes the next statement or block of statements. (In fact, in most of the underlying grammars, a block of statements DOES reduce to a single statement!) This is true in the following languages (that I know of):

  • C
  • C++
  • PHP
  • Java
  • C#

In your first code example, only function() is bound to the if conditional.

In Perl, the designer decided to force blocks to go after if-statements. For single-statement if's, though, there are statement suffixes. So the following are equivalent:

# if-statement preceding block...
if (x == 5)
{
  print "x is 5";
}

# if-statement as a suffix conditional to one statement (line break for readability)
print "x is 5"
  if x == 5;

# Equivalent to the second, because "do BLOCK;" can be used anywhere a
# simple statement can be used
do
{
  print "x is 5";
} if x == 5;

Python works a bit more like your initial example would suggest: indentation is everything.

if (x == 5):
  print "x is 5"
print "This is always printed"


These are the equivalents:

for(int i = 0; i<10; i++) {
    if(i % 2 == 0) {
        //this is included in the for loop and the if loop
        function();
    }
}
// is this?
function2();

// where does it stop?
function3();

....

for(int i = 0; i<10; i++) {
    if(i % 2 == 0) {
        //this is included in the for loop and the if loop
        function();
    }
    else {
        // is this run as part of the for loop? even though theres a semi colon before it?
        function2();
    }
}

...

if((int)1 == (int)1) {
    function1();
}
function2();
function3();
function4();

...

if((int)1 == (int)1) {
    function();
}
function2();
function3();

...

In summary, anything between {} are treated as a single statement - and vice versa.


If you'd like to use an "inline" if state, you can do it like this:

bool greaterthan(int a, int b){
   return a>b ? true:false;
}

which essentially means If a>b, return true, otherwise, return false. This can be usedin many other ways too!


The next logical evaluation "block" is executed, which in general means the next "statement" (terminating with a ";") is executed.

Program flow commands are considered a statement for purposes of evaluation.


All the C based languages (PHP is syntactically C based) work in the following way:

for (foo1();foo2();foo3())
    operation1();
operation2();

Above only operation1() will be executed

if (foo())
    operation1();
operation2();

Above only operation1() will be executed


If you are not using a compound statement (statements enclosed by {}), only the very first line will be included

for(int i=0; i<10; i++)
 if(i==5)                // included in loop
     printf("%d\n",i);   // included since if is included. 
 else
     prinf("Not Five"); // Included if is included

if statments, if-else statements, for loop, while loop etc are considered as one line

Similarly

for(int i=0; i<10; i++)
    function1();    // included
function2();         // not included

Hope you understood.


I would like to point out that this syntax isn't recommended because it has a tenancy to backfire easily. Take the following, for example:

if (true)
    // not working properly. Exclude it for now
    //    foo()

bar();

In such a case, bar() get's executed because it is the next logical statement, even though it is clearly not the intended statement to be run. This has the potential to cause massive headaches, all from a simple comment and poorly structured 'if'


Depends on the language you're using. Just learn the language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜