Write a C/C++ program that execute both if-else block statements simultaneously.
Syntax of if-else statement in C/C++ language is:
if (Boolean expression)
{
// Statement will execute only
// if Boolean expression is true
}
else
{
// Statement will execute only if
// the Boolean expression is false
}
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 able to make our program so that both the statements inside if and else will execute simultaneously.
The trick is to use goto statement which provides an unconditional jump from the ‘goto’ to a labelled statement in the same function.
Below is C/C++ program to execute both statements simultaneously:-
#include <stdio.h> int main() { if (1) //Replace 1 with 0 and see the magic { label_1: printf("Hello "); // Jump to the else statement after // executing the above statement goto label_2; } else { // Jump to 'if block statement' if // the Boolean condition becomes false goto label_1; label_2: printf("Geeks"); } return 0; } |
Output: Hello Geeks
Therefore both the statements of if and else block executed simultaneously. Another interesting fact can be seen that Output will always remain same and will not depend upon the whether the Boolean condition is true or false.
NOTE – Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. As a programmer we should avoid the use of goto statement in C/C++.
This blog is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++17 new feature : If Else and Switch Statements with initializers
- Decision Making in C / C++ (if , if..else, Nested if, if-else-if )
- How to execute zombie and orphan process in a single program?
- fork() to execute processes from bottom to up using wait()
- Difference between continue and break statements in C++
- Else without IF and L-Value Required Error in C
- switch vs if else
- C/C++ if else if ladder with Examples
- C/C++ if else statement with Examples
- Code valid in both C and C++ but produce different output
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Similarities and Differences between Ruby and C++
- Generate an array of given size with equal count and sum of odd and even numbers

