决策声明


决策对于计算机编程至关重要。在很多情况下,您会得到两个或多个选项,并且您必须根据给定的条件选择一个选项。例如,我们想根据学生的安全分数打印有关该学生的评论。以下是情况 -

Assume given marks are x for a student:

If given marks are more than 95, then
Student is brilliant

If given marks are less than 30, then
Student is poor

If given marks are less than 95 and more than 30, then
Student is average

现在的问题是如何编写编程代码来处理这种情况。几乎所有编程语言都提供基于以下流程图工作的条件语句 -

C 中的决策语句

让我们借助if 条件语句编写一个 C 程序,将上述给定情况转换为编程代码 -

#include <stdio.h>

int main() {
   int x = 45;
   
   if( x > 95) {
	
      printf( "Student is brilliant\n");
   }
   if( x < 30) {
	
      printf( "Student is poor\n");
   }
   if( x < 95 && x > 30 ) {
	
      printf( "Student is average\n");
   }
}

执行上述程序时,会产生以下结果 -

Student is average

上面的程序使用了if条件语句。这里,第一个if语句检查给定的条件,即变量x是否大于95,如果发现条件为真,则输入条件体来执行给定的语句。这里我们只有一个printf()语句来打印有关该学生的评论。

同样,第二个if 语句也有效。最后,执行第三个if 语句,这里我们有以下两个条件 -

  • 第一个条件是x > 95

  • 第二个条件是x < 30

计算机评估给定的条件,然后借助二元运算符&&组合总体结果。如果最终结果为真,则执行条件语句,否则不执行任何语句。

本教程将为您介绍各种形式的if 语句的基本概念,并介绍C 编程语言中可用的switch语句。不同的编程语言提供不同类型的决策语句,但基本概念与本教程中解释的相同。

if...else 语句

if语句后面可以跟一个可选的else语句,该语句在布尔表达式为 false 时执行C 编程语言中if...else语句的语法是 -

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

上述语法可以用流程图的形式表示,如下所示 -

C if...else 语句

当我们必须从两个选项中做出决定时,if...else 语句非常有用例如,如果一个学生的分数超过 95,那么该学生很聪明,否则无法对这种情况进行编码,如下 -

#include <stdio.h>

int main() {
   int x = 45;
   
   if( x > 95) {
	
      printf( "Student is brilliant\n");
   } else {
      printf( "Student is not brilliant\n");
   }
}

执行上述程序时,会产生以下结果 -

Student is not brilliant

if...elseif...else 语句

if语句后面可以跟一个可选的else if...else语句,这对于测试各种条件非常有用。

在使用if、else if、else语句时,需要记住以下几点 -

  • if可以有零个或一个else 并且它必须位于else if之后。

  • 一个if可以有零到多个else...if,并且它们必须出现在else之前。

  • 一旦else...if成功,其余的else...ifelse 都不会被测试。

C 编程语言中if...else if...else语句的语法为 -

if(boolean_expression 1) {

   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {

   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {

   /* Executes when the boolean expression 3 is true */
} else {
   
   /* Executes when the none of the above condition is true */
}

现在,借助if...elseif...else语句,第一个程序可以编码如下 -

#include <stdio.h>

int main() {
   int x = 45;
   
   if( x > 95) {
      printf( "Student is brilliant\n");
   } 
   else if( x < 30) {
      printf( "Student is poor\n");
   } 
   else if( x < 95 && x > 30 ) {
      printf( "Student is average\n");
   }
}

执行上述程序时,会产生以下结果 -

Student is average

Switch 语句

switch语句是if 语句的替代方法,它允许测试变量是否值列表相等。每个值称为一个case,并且针对每个 switch case 检查正在打开的变量。它具有以下语法 -

switch(expression){
   case ONE :
      statement(s);
      break;
   case TWO:
      statement(s);
      break;
   ......
   
   default :
      statement(s);
}

switch语句中使用的表达式必须给出一个整数值,该值将与给定的不SymPy况进行比较是否相等。只要表达式值与 case 值匹配,就会执行该 case 的主体,最后,将使用break语句终止 switch 。如果未提供中断语句,则计算机将继续执行下面针对匹配情况的其他可用语句。如果没有一个 case 匹配,则执行默认的 case 体。

上述语法可以用流程图的形式表示,如下所示 -

C 中的 Switch 语句

现在,让我们考虑另一个例子,我们想要为给定的数字写出等效的英语单词。然后,它可以编码如下 -

#include <stdio.h>

int main() {
   int x = 2;
   
   switch( x ){
      case 1 :
         printf( "One\n");
         break;
      case 2 :
         printf( "Two\n");
         break;
      case 3 :
         printf( "Three\n");
         break;
      case 4 :
         printf( "Four\n");
         break;
      default :
         printf( "None of the above...\n");
   }
}

执行上述程序时,会产生以下结果 -

Two

Java 中的决策

以下是用 Java 编写的等效程序,它也支持ifif...elseif...elseif...elseswitch语句。

您可以尝试执行以下程序来查看输出,该输出必须与上面的 C 示例生成的结果相同。

public class DemoJava {
   public static void main(String []args) {
      int x = 45;
   
      if( x > 95) {
         System.out.println( "Student is brilliant");
      } 
      else if( x < 30) {
         System.out.println( "Student is poor");
      } 
      else if( x < 95 && x > 30 ) {
         System.out.println( "Student is average");
      }
   }
}

执行上述程序时,会产生以下结果 -

Student is average

Python 中的决策

以下是用 Python 编写的等效程序。Python 提供ifif...elseif...elif...elseswitch语句。在这里,您必须注意,Python 不使用大括号作为条件主体,而是简单地使用语句的缩进来标识块的主体。

您可以尝试执行以下程序来查看输出 -

x = 45

if x > 95:
   print "Student is brilliant"
elif x < 30:
   print "Student is poor"
elif x < 95 and x > 30:
   print "Student is average"

print "The end"

执行上述程序时,会产生以下结果 -

Student is average
The end