
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Execute Both If and Else Statements in C/C++ Simultaneously
In this article, we will see how to execute the if and else section simultaneously in a C or C++, code. This solution is a little bit tricky.
When the if and else are executed one after another then it is like executing statements where if-else are not present. But here we will see if they are present how to execute them one after another.
Let's go through the different ways to simulate execution of both if and else blocks. This may include using functions, macros, or separating logic completely.
Note: These tricks are for educational purposes. In actual logic, if and else are meant to be exclusive decisions ? either one or the other, not both.
Using Separate Functions
This is the cleanest approach. we can place both if and else logic inside separate functions and call them unconditionally.
Example
In this program, we call two separate functions to print messages for "IF" and "ELSE" parts, simulating conditional logic.
#include <iostream> using namespace std; void doIfPart() { cout << "Executing IF part" << endl; } void doElsePart() { cout << "Executing ELSE part" << endl; } int main() { doIfPart(); doElsePart(); return 0; }
Following is the output to the above program:
Executing IF part Executing ELSE part
Using Macros
Here, you can define macros that look like if or else but it always execute.
Example
In this example, we use macros to simulate 'if' and 'else' statements, printing predefined messages regardless of any actual conditions.
#include <iostream> #define if(x) { cout << "Simulated IF: " << x << endl; } #define else(x) { cout << "Simulated ELSE: " << x << endl; } using namespace std; int main() { if("Condition is True"); else("Condition is False"); return 0; }
Following is the output to the above program:
Simulated IF: Condition is True Simulated ELSE: Condition is False
Using Goto
Using Goto acts like an infinite loop, but the if block and else block, are executing simultaneously. After the first check, the condition checking is not really affected on the output.
Note: Here we are using a goto statement to forcefully send the control from if block to else and else to if. But the using of the goto statement is not good. It makes difficult to trace the control flow of program.
Example
The program repeatedly switches between two messages using 'goto', without stopping.
#include <iostream> using namespace std; int main() { int x = 10; if(x > 5) { lebel_1: cout << "This is inside if statement" <<endl; goto lebel_2; } else { lebel_2: cout << "This is inside else statement" <<endl; goto lebel_1; } }
Following is the output to the above program:
This is inside if statement This is inside else statement This is inside if statement This is inside else statement This is inside if statement This is inside else statement This is inside if statement This is inside else statement .... .... ....