Execute Both If And Else Statements In Cc++ Simultaneously

If-else statements are designed as plan-a backing plan-b. If plan-a fails, plan-b comes into the picture. How can we make both the conditionals work? The trick we apply to solve this chicken-and-egg problem in C and C++ is we use the goto function. The goto function links both the conditionals in such a way that if one gets executed, the execution of the following boolean executes as well simultaneously.

Syntax of if-else statements in C/C++ language is

Example

if (Boolean expression)
{
    // The above written if condition will get executed only if
    // the expression inside the Boolean is true
}
else
{
    // The above written if condition will get executed only if
    // the expression inside the Boolean of if the condition is false
}

C Code(if-else)

Example

// Here we are writing down the C programming language code to demonstrate the
// concept of If statement along with its relevant code and output supported
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include <stdio.h>
// The main driver code functionality starts from here
int main()
{
               int j = 20;
 
               if (j < 15) {
// printing the true condition of boolean
                              printf("j variable is smaller than 15");
               }
               else {
// printing the false condition of boolean
                              printf("j variable is greater than 15");
               }
               return 0;
// The main driver code functionality ends from here

Output:

Output

j variable is greater than 15

C++ Code(if-else)

Example

// Here we are writing down the C programming language code to demonstrate the
// concept of If statement along with its relevant code and output supported
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include <iostream>
using namespace std;
// The main driver code functionality starts from here
int main()
{
               int j = 20;
 
               if (j < 15)
// printing the true condition of boolean
                              cout << "j variable is smaller than 15";
               else
// printing the false condition of boolean
                              cout << "j variable is greater than 15";
 
               return 0;
// The main driver code functionality ends from here
}

Output:

Output

j variable is greater than 15

Syntax

Example

if (condition1)
{
       // The above written if condition will get executed only if
      // the expression inside the condition1 is true
 
   if (condition2)
   {
      // The above written if condition will get executed only if
      // the expression inside the condition2 is true
   }
}

Nested if-else statements are special conditions; here, conditionals are either undirected or directed one inside the other. The nested if-else statements can be compared with nested for loops and nested while loops. Implementing nested if-else statements is discussed in C and C++ programming languages.

Nested if-else in C

Example

// Here we are writing down the C programming language code to demonstrate the
// concept of If statement along with its relevant code and output support
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include <stdio.h>
// The main driver code functionality starts from here
int main()
{
               int j = 10;
 
               if (j == 10) {
                              if (j < 15)
                                             printf("j is smaller than 15\n");
 
              
                              if (j < 12)
                              // printing the true condition of boolean
                                             printf("j is smaller than 12 too\n");
                              else
                              // printing the condition of boolean if above is false
                                             printf("j is greater than 15");
               }
 
               return 0;
// The main driver code functionality ends from here
}

Output:

Output

j is smaller than 15
j is smaller than 12 too

Nested if-else in C++

Example

// Here we are writing down the C programming language code to demonstrate the
// concept of If statement along with its relevant code and output support
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include <iostream>
using namespace std;
// The main driver code functionality starts from here
int main()
{
               int j = 10;
 
               if (j == 10) {
                             
                              if (j < 15)
                                             // printing the true condition of boolean
                                             cout << "j is smaller than 15\n";
 
                             
                              if (j < 12)
                              // printing the true condition of boolean
                                             cout << "j is smaller than 12 too\n";
                              else
                              // printing the condition of boolean if above is false
                                             cout << "j is greater than 15";
               }
// The main driver code functionality ends from here
               return 0;
}

Output:

Output

j is smaller than 15
j is smaller than 12 too

Below we are writing down the code to execute both if and else statements together. To achieve this task, we will use the goto function and label them to the next targetted conditional statement. It can either be an else statement or another if statement, which is nested if.

C Code

Example

#include <stdio.h>
int main()
{
if (1)
{
               label_1: printf("Hello ");
               goto label_2;
}
else
{
               goto label_1;
 
               label_2: printf("JTP");
}
return 0;
}

Output:

Output

Hello JTP

C++ Code

Example

#include <bits/stdc++.h>
using namespace std;
int main()
{
if (1)
               label_1: cout <<"Hello ";
               goto label_2;
}
else
{
               goto label_1;
               label_2: cout <<"JTP";
}
return 0;
}

Output:

Output

Hello JTP

Input Required

This code uses input(). Please provide values below: