How to execute both conditions of if else Statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to execute both conditions of if else Statement

I want to execute both conditions of if else statement . for example I want to print 12 as the output of the following program. if(x) cout<<1; else cout<<2;

1st Mar 2017, 11:42 AM
Yash Raghava
Yash Raghava - avatar
6 Answers
+ 3
I heard both conditions may get executed
1st Mar 2017, 2:03 PM
Yash Raghava
Yash Raghava - avatar
+ 3
#include<stdio.h> #include<conio.h> void main() { clrscr(); if (!(printf("1"))) ; else printf("2"); getch(); } //we all know that printf(), scanf(), exit() returns zero hence by adding ! It makes the value false so it prints if statement as well as else statement and the symbol ; terminates the statement :3 /*now apply this trick with all ur frnds n njoy*/ ;) The code is being fully tested and this is how the output looks like with both if and else statement
28th Jan 2020, 7:22 AM
Rimjhim Pandey
Rimjhim Pandey - avatar
+ 1
if(x) is true, so this code will only execute cout<<1, remove the else and your done.
1st Mar 2017, 12:27 PM
Andre van Rensburg
Andre van Rensburg - avatar
0
if.. else... cannot execute both brackets. if and only if 'IF' is false then it goes to ELSE.
1st Mar 2017, 11:58 AM
AshishFF
AshishFF - avatar
0
Remove if-else. Otherwise, only one flow will be executed according to condition.
1st Mar 2017, 12:12 PM
Twelfty
Twelfty - avatar
0
you can use if elseif statement
2nd Mar 2017, 10:33 AM
Hitesh Morasiya
Hitesh Morasiya - avatar