同时执行C或C++中的if和else语句

同时执行C或C++中的if和else语句

If-else语句设计为plan-a后备plan-b。如果plan-a失败了,plan-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循环进行比较。在C和c++编程语言中讨论了嵌套if-else语句的实现。

**在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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程