决定声明

决策对计算机编程至关重要.在有许多情况下,您将获得两个或更多选项,并且您必须根据给定条件选择一个选项.例如,我们希望根据他的安全标记打印关于学生的评论.以下是情况 :

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

让我们在的帮助下写一个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语句检查给定条件,即变量x是否大于95,如果它发现条件为真,则输入条件体以执行给定语句.这里我们只有一个 printf()语句来打印关于学生的评论.

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

  • 第一个条件是 x> 95

  • 第二个条件是 x< 30

计算机评估两种给定条件,然后在二元运算符的帮助下结合整体结果&安培;&安培; 的.如果最终结果为true,则将执行条件语句,否则不会执行语句.

本教程将为您提供各种形式的 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 statement

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 statement

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

在使用 if,else if,else 语句时,有几点需要记住 : 去;

  • 如果可以有零个或一个其他的,它必须在其他之后.

  • 一个如果可以有0到多个其他...如果是并且它们必须在之前否则.

  • 一旦其他......如果成功,剩下的其他都没有......如果是其他的将被测试.

if ... else的语法if ... else C编程语言中的语句是 :

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

转换语句

A switch 语句是 if语句的替代语句,它允许根据值列表测试变量的相等性.每个值称为 case ,并且针对每个开关案例检查接通的变量.它具有以下语法 :

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

开关语句中使用的表达式必须给出整数值,将对不同情况下的平等进行比较.只要表达式值与案例值匹配,将执行该案例的主体,最后,将使用 break 语句终止该开关.如果没有提供break语句,则计算机继续执行下面可用的其他语句到匹配的大小写.如果没有一个匹配,则执行默认的case主体.

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

在C语言中切换语句

现在,让我们考虑另一个我们想写同等英语的例子给定数字的单词.然后,它可以编码如下 :

#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编写的程序也支持 if if ...... else if ... elseif ... else 切换语句.

您可以尝试执行以下程序以查看输出,该输出必须与上述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提供 if if ...... else if ... elif ... else switch 声明.在这里,你必须注意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