C++ 同时执行if和else语句

C++ 同时执行if和else语句

if-else语句被设计为计划A备用计划B。如果计划A失败,计划B就会被执行。我们如何使两个条件都起作用?我们在C和C++中解决这个鸡生蛋的问题的技巧是使用goto函数。goto函数将两个条件链接在一起,以便如果一个条件执行,后面的条件也同时执行。

C/C++语言中if-else语句的语法是

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代码(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 
// 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

输出:

j variable is greater than 15

C ++ 代码(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 
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
}

输出:

j variable is greater than 15

语法

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
   }
}

嵌套if-else语句是特殊的条件语句;在这里,条件语句可以是互相独立的,或者嵌套在另一个条件语句内部。嵌套if-else语句可以与嵌套的for循环和嵌套的while循环进行比较。实现嵌套if-else语句在C和C++编程语言中进行讨论。

在C中的嵌套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 support
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include 
// 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
}

输出:

j is smaller than 15
j is smaller than 12 too

在C++中的嵌套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 support
// by syntax wherever necessary to explain the concept of executing both
// if and else statements simultaneously
#include 
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;
}

输出:

j is smaller than 15
j is smaller than 12 too

以下我们将编写代码同时执行if和else语句。为了实现这个任务,我们将使用goto函数,并将它们标记为下一个目标条件语句。它可以是一个else语句,也可以是另一个if语句,即嵌套的if。

C 代码

#include 
int main()
{
if (1)
{
               label_1: printf("Hello ");
               goto label_2;
}
else
{
               goto label_1;

               label_2: printf("JTP");
}
return 0;
}

输出:

Hello JTP

C++ 代码

#include 
using namespace std;
int main()
{
if (1)
               label_1: cout <<"Hello ";
               goto label_2;
}
else
{
               goto label_1;
               label_2: cout <<"JTP";
}
return 0;
}

输出:

Hello JTP

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程