C语言 - if语句



指令的条件执行是计算机程序的基本要求。C语言中的if语句是主要的条件语句。C语言允许使用可选的else关键字来指定如果if条件为假则要执行的语句。

C语言 - if语句

if语句是C语言编程中一种基本的决策控制语句。根据if语句中布尔条件的真假,代码块中的一条或多条语句将被执行。

if语句的语法

if语句的语法如下:

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}

if语句的工作原理?

C语言使用一对花括号来构成代码块。如果布尔表达式计算结果为真,则将执行if语句内的代码块。

如果布尔表达式的计算结果为假,则将执行if语句结束后的第一组代码(右花括号之后)。

C语言将任何非零和非空值视为真。如果值为零或空,则将其视为假值。

if语句的流程图

if语句的行为可以用以下流程图表示:

C if statement

流程图解释

当程序控制遇到if语句时,将评估条件。

如果条件为真,则执行if块内的语句。

如果条件为假,则程序流程将绕过条件块。

执行if块后的语句以继续程序流程。

C语言中if语句的示例

此示例演示了if语句最简单的用例。它确定并告诉用户变量的值是否小于20。

#include <stdio.h>
 
int main (){

   /* local variable declaration */
   int a;
   
   // run the program for different values of "a" 
   // Assign 12 first and 40 afterwards
   
   a = 12;  //change to 40 and run again
   printf("Value of a is : %d\n", a);
 
   // check the boolean condition using if statement
	
   if(a < 20){
      //if the condition is true, then print the following
      printf("a is less than 20\n" );
   }
   return 0;
}

输出

运行上述程序并检查其输出:

Value of a is : 12
a is less than 20

现在赋值一个大于20的数字。if条件不会被执行。

Value of a is: 40

带有逻辑运算的if语句

您可以使用&&||运算符在if语句的括号中放置复合布尔表达式。

示例

在下面的示例中,比较了三个变量“a”、“b”和“c”。当“a”大于“b”和“c”时,将执行if块。

#include <stdio.h>
 
int main () {

   /* local variable declaration */
   int a, b, c;
   
   /*use different values for a, b and c as
   10, 5, 7
   10, 20, 15
   */
   
   // change to 10,20,15 respectively next time
   a = 10; b = 5; c = 7;
   
   if (a>=b && a>=c){
      printf ("a is greater than b and c \n");
   }
   printf("a: %d b:%d c:%d", a, b, c);
 
   return 0;
}

输出

运行代码并检查其输出:

//when values for a, b and c are 10 5 7
a is greater than b and c 
a: 10 b:5 c:7

//when values for a, b and c are 10 20 15
a: 10 b:20 c:15

请注意,条件块后面的语句在块执行后执行。如果条件为假,程序将直接跳转到块后面的语句。

多个if语句

如果您有多个条件要检查,则可以多次使用if语句。

示例

在此示例中,通过对账单金额应用折扣来计算应付净额。

如果金额在1000到5000之间,则适用的折扣为5%;如果金额超过5000,则适用的折扣为10%。对于低于1000的购买,不适用任何折扣。

#include <stdio.h>
 
int main () {

   // local variable declaration
   int amount;
   float discount, net;
   
   /*Run the program for different values 
   of amount – 500, 2250 and 5200. Blocks in 
   respective conditions will be executed*/
   
   // change to 2250 and 5200 and run again
   amount = 500;  
   
   if (amount < 1000){
      discount=0;
   }
   if (amount >= 1000 && amount<5000){
      discount=5;
   }
   if (amount >= 5000){
      discount=10;
   }
   net = amount - amount*discount/100;
   printf("Amount: %d Discount: %f Net payable: %f", amount, discount, net);
   
   return 0;
}

输出

//when the bill amount is 500
Amount: 500 Discount: 0.000000 Net payable: 500.000000

//when the bill amount is 2250
Amount: 2250 Discount: 5.000000 Net payable: 2137.500000

//when the bill amount is 5200
Amount: 5200 Discount: 10.000000 Net payable: 4680.000000

使用if语句检查多个条件

您也可以在单个if语句中使用逻辑运算符来检查多个条件。

示例

在此程序中,只有当“phy”和“maths”分数的平均值大于等于50时,学生才被判定为及格。此外,学生两门科目的分数都必须超过35分。否则,学生将被判定为不及格

#include <stdio.h>

int main (){
   /* local variable declaration */
   int phy, maths;
   float avg;
   
   /*use different values of phy and maths 
   to check conditional execution*/
   
   //change to 40, 40 and 80, 40
   phy = 50; maths = 50; 
   avg = (float)(phy + maths)/2;
   printf("Phy: %d Maths: %d Avg: %f\n", phy, maths, avg);
    
   if (avg >= 50 && (maths >= 35 && phy >= 35)){
      printf("Result: Pass");
   }

   if (avg<50) {
      printf("Result: Fail\n");
   }
   return 0;
}

输出

运行代码并检查其输出:

//when marks in Phy and Maths - 50 50
Phy: 50 Maths: 50 Avg: 50.000000
Result: Pass

//when marks in Phy and Maths - 40 40
Phy: 40 Maths: 40 Avg: 40.000000
Result: Fail

//when marks in Phy and Maths - 80 40
Phy: 80 Maths: 40 Avg: 60.000000
Result: Pass
c_decision_making.htm
广告
© . All rights reserved.