Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute both if and else part of an if --- else control statement? [duplicate]

Tags:

c

Possible Duplicate:
Simultaneous execution of both if and else blocks

Is it possible to put some condition, so that both if and else part in an if ...else control statement can be executed without any warning or error ??

like image 648
Parikshita Avatar asked Oct 01 '10 09:10

Parikshita


People also ask

Can we execute both IF and ELSE?

Hence we can conclude that only one of the block of if-else statement will execute according to the condition of Boolean expression. But we can change our code so that both the statements inside the if block and the else block gets executed, for the same condition.

Is else if and if-else the same in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

Does else if execute after if?

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to false , and the current elseif expression evaluated to true .

Can you have multiple else if statements?

You can have as many else if statements as necessary. In the case of many else if statements, the switch statement might be preferred for readability. As an example of multiple else if statements, we can create a grading app that will output a letter grade based on a score out of 100.


1 Answers

Do not use! ;-)

Yes, by forking.

if ( fork() ) {
    printf("if\n");
}
else {
    printf("else\n");
}

There are no real use cases to prefer the above code, unless it is for parallel execution.

like image 197
Alan Haggai Alavi Avatar answered Nov 15 '22 09:11

Alan Haggai Alavi