Javatpoint Logo
Javatpoint Logo

Execute both if and else Statements in C/C++ 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

C Code(if-else)

Output:

j variable is greater than 15

C++ Code(if-else)

Output:

j variable is greater than 15

Syntax

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

Output:

j is smaller than 15
j is smaller than 12 too

Nested if-else in C++

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

Output:

Hello JTP

C++ Code

Output:

Hello JTP






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA