[Solved] justify this excution please c++ [closed]


After an if and after an else there needs to be exactly one statement. The statement after if will be executed if the condition is true, the statement after else will be executed if the condition is false.

Now the important thing to understand is the following:

  1. A single semicolon ; is a statement. It’s an empty statement and putting it behind if or behind else fullfils the requirements of a statement.

  2. A block between curly brackets {} is a statement. It groups multiple statements together, but it is one statement and thus also fulfills the requirements.

Any other statement that comes after the first statement after if or else has nothing to do with the if or else and will be executed like any other code would.

So to break down your example:

if(x > 0)
    y = y + 100;
else;
    y = y + 200;

is equivalent to:

if(x > 0)
    y = y + 100;  // first statement after if
else
    ;             // first statement after else

y = y + 200;      // separate statement uneffected by if or else

Removing the else gives us this:

if(x > 0)
    y = y + 100;  // first statement after if

y = y + 200;      // separate statement uneffected by if

and of course the statement y = y + 200; will be executed, because it has nothing to do with the if or the else.

0

solved justify this excution please c++ [closed]