if statement execution | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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; }

28th Jan 2019, 6:54 PM
LYDIA JENNIFER P
LYDIA JENNIFER P - avatar
3 Answers
+ 4
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.
28th Jan 2019, 7:52 PM
Lambda_Driver
Lambda_Driver - avatar
+ 3
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
28th Jan 2019, 7:28 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
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
28th Jan 2019, 7:57 PM
Ipang