目录

计算机编程 - 决策(Decisions)

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

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

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

  • 第一个条件是x 》 95

  • 第二个条件是x 《 30

计算机评估给定的条件,然后,整体结果与二元运算符&&的帮助相结合。 如果最终结果为true,则将执行条件语句,否则不执行任何语句。

本教程将为您提供有关各种形式的if statements的基本概念,以及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's ,它必须在一个else if

  • 一个if可以有0到多个else…if's ,他们必须在else之前。

  • 一旦else…if成功,剩下的else…if'selse's将被测试。

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 statements的替代if statements ,它允许根据值列表测试变量的相等性。 每个值称为一个case ,并检查每个开关案例接通的变量。 它具有以下语法 -

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

switch语句中使用的expression必须给出一个整数值,该值将与给定的不同情况进行相等性比较。 只要表达式值与case值匹配,该case的主体就会被执行,最后,switch将使用break语句终止。 如果没有提供break语句,则计算机继续执行下面提供的其他语句到匹配的大小写。 如果所有情况都不匹配,则执行默认的案例正文。

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

在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编写的等效程序,它支持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
↑回到顶部↑
WIKI教程 @2018