If-else statements are structured to serve as the primary and secondary options. In case the primary plan fails, the secondary plan is activated. How can we ensure the functionality of both conditions? The technique employed to address this cyclic problem in C and C++ involves leveraging the goto statement. By using the goto statement, we establish a connection between both conditions so that if one is triggered, the subsequent boolean condition is also executed concurrently.
Syntax of if-else statements in C/C++ language is
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)
// 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:
j variable is greater than 15
C++ Code(if-else)
// 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:
j variable is greater than 15
Syntax
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 present unique scenarios where conditions are organized in a hierarchical manner, with one condition encapsulated within another. These nested structures can be likened to nested for loops and nested while loops. The application of nested if-else statements is explored within the realms of C and C++ programming languages.
Nested if-else in C
// 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:
j is smaller than 15
j is smaller than 12 too
Nested if-else in C++
// 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:
j is smaller than 15
j is smaller than 12 too
Below we are outlining the code to execute both the if and else conditions simultaneously. To accomplish this objective, we will employ the goto function and assign labels to direct the flow to the subsequent targeted conditional block. This could lead to either an else condition or a nested if statement.
C Code
#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:
Hello JTP
C++ Code
#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:
Hello JTP