if statement execution | Sololearn: Learn to code for FREE!


if statement execution

why is both printf in the following program getting printed when i value is given above 15? #include <stdio.h> int main() { int i=1000; if(i>15) { printf("\n if block"); } printf("\n not in if block"); return 0; }

1/28/2019 6:54:34 PM


3 Answers

New Answer


You can enclose the 2nd print statement in an else block so it doesn't get printed out. As *Asterisk* pointed out, any block of code ouside the if statement will be executed after the if statement is evaluated regardless of the outcome of the if statement.


The if means: if I is greater than 15 which is true since I is 1000 and 1000 is greater than 15, so the statement in the if will be printed, and the statement outside the if will surely be printed no matter the condition of the if. I hope you get that


Additionally, to what *Asterisk* said, you can wrap the second printf call within an else block, which will only be executed in case value of <i> was NOT greater than 15. Example: if(i > 15) { // the "if" block } else { // the "else" block } Hth, cmiiw