cpp 专题
专题目录
您的位置:cpp > cpp 专题 > C++ if/else语句
C++ if/else语句
作者:--    发布时间:2019-11-20

在c++编程中,if语句用于测试条件。 在c++中有多种类型的if语句,它们分别如下所示 -

  • if语句
  • if-else语句
  • 嵌套if语句
  • if-else-if阶梯

1. c++ if语句

c++ if语句测试条件。 如果条件为真,则执行。

if(condition){    
    //code to be executed    
}

c++ if语句的执行流程图如下所示 -

c++ if语句示例

#include <iostream>  
using namespace std;  

int main () {  
    int num = 10;    
    if (num % 2 == 0)    
    {    
        cout<<"it is even number";    
    }   
    return 0;  
}

执行上面代码,得到以下结果 -

it is even number

2. c++ if-else语句

c++ if-else语句也测试条件。 如果if条件为真,则执行if块中的代码,否则执行else块中的代码。

if(condition){    
    //code if condition is true    
}else{    
    //code if condition is false    
}

if-else执行的流程如下图中所示 -

c++ if-else示例

#include <iostream>  
using namespace std;  
int main () {  
   int num = 11;    
   if (num % 2 == 0)    
   {    
       cout<<"it is even number";    
   }   
   else  
   {    
       cout<<"it is odd number";    
   }  
   return 0;  
}

执行上面代码得到以下结果 -

it is odd number

c++ if-else示例:带有来自用户的输入

#include <iostream>  
using namespace std;  
int main () {  
    int num;  
    cout<<"enter a number: ";  
    cin>>num;  
    if (num % 2 == 0)    
    {    
        cout<<"it is even number"<<endl;    
    }   
    else  
    {    
        cout<<"it is odd number"<<endl;    
    }  
   return 0;  
}

执行上面代码得到以下结果 -

[h3@localhost cpp]$ g++ ifelse.cpp && ./a.out
enter a number: 43
it is odd number
[h3@localhost cpp]$ g++ ifelse.cpp && ./a.out
enter a number: 22
it is even number
[h3@localhost cpp]$

3. c++ if-else-if梯形语句

c++ if-else-if梯形语句用于从多个语句中执行一个条件。

if(condition1){    
    //code to be executed if condition1 is true    
}else if(condition2){    
    //code to be executed if condition2 is true    
}    
else if(condition3){    
    //code to be executed if condition3 is true    
}    
...    
else{    
    //code to be executed if all the conditions are false    
}

if-else-if语句的执行流程如下图所示 -

c++ if-else-if示例

#include <iostream>  
using namespace std;  
int main () {  
       int num;  
       cout<<"enter a number to check grade:";    
       cin>>num;  
       if (num <0 || num >100)    
       {    
           cout<<"wrong number\n";    
       }    
       else if(num >= 0 && num < 50){    
           cout<<"fail\n";    
       }    
       else if (num >= 50 && num < 60)    
       {    
           cout<<"d grade\n";    
       }    
       else if (num >= 60 && num < 70)    
       {    
           cout<<"c grade\n";    
       }    
       else if (num >= 70 && num < 80)    
       {    
           cout<<"b grade\n";    
       }    
       else if (num >= 80 && num < 90)    
       {    
           cout<<"a grade\n";    
       }    
       else if (num >= 90 && num <= 100)    
       {    
           cout<<"a+ grade\n";  
       }
    return 0;
}

执行上面示例代码,得到以下结果 -

[h3@localhost cpp]$ g++ if-else-if.cpp && ./a.out
enter a number to check grade:99
a+ grade
[h3@localhost cpp]$ g++ if-else-if.cpp && ./a.out
enter a number to check grade:59
d grade
[h3@localhost cpp]$ g++ if-else-if.cpp && ./a.out
enter a number to check grade:49
fail
[h3@localhost cpp]$

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册