首页  >  文章  >  后端开发  >  使用流程图和程序来描述C语言中的决策概念

使用流程图和程序来描述C语言中的决策概念

WBOY
WBOY转载
2023-09-15 11:05:041328浏览

以下是决策语句 -

  • 简单 - if 语句
  • if - else 语句
  • 嵌套 - if else 语句
  • else – ifladder
  • switch 语句

简单 – if 语句

“if”关键字是用于在逻辑条件为真时执行一组语句。

语法

if (condition){
   Statement (s)
}

使用流程图和程序来描述C语言中的决策概念

示例

以下示例检查数字是否大于 50。

#include<stdio.h>
main (){
   int a;
   printf (&ldquo;enter any number:</p><p>&rdquo;);
   scanf (&ldquo;%d&rdquo;, &a);
   if (a>50)
      printf (&ldquo;%d is greater than 50&rdquo;, a);
}

输出

1) enter any number: 60
60 is greater than 50 .
2) enter any number 20
no output

if else语句

if else语句接受True或False条件。

语法

if (condition){
   True block statement(s)
}
else{
   False block statement(s)
}

流程图

使用流程图和程序来描述C语言中的决策概念

示例

以下是检查奇偶数的程序 −

#include<stdio.h>
main (){
   int n;
   printf (&ldquo;enter any number:</p><p>&rdquo;);
   scanf (&ldquo;%d&rdquo;, &n);
   if (n%2 ==0)
      printf (&ldquo;%d is even number&rdquo;, n);
   else
      printf( &ldquo;%d is odd number&rdquo;, n);
}

输出

1) enter any number: 10
10 is even number

嵌套的 if - else 语句

这里的“if”被放置在另一个 if(或)else 中 -

语法

if (condition1){
   if (condition2)
      stmt1;
   else
      stmt2;
   }
   else{
      if (condition3)
         stmt3;
      else
         stmt4;
   }

流程图

使用流程图和程序来描述C语言中的决策概念

示例

以下示例是打印给定数字中最大的3个数字。

#include<stdio.h>
main (){
   int a,b,c;
   printf (&ldquo;enter 3 numbers&rdquo;);
   scanf (&ldquo;%d%d%d&rdquo;, &a, &b, &c);
   if (a>b){
      if (a>c)
         printf (&ldquo;%d is largest&rdquo;, a);
      else
         printf (&ldquo;%d is largest&rdquo;, c);
   } else {
      if (b>c)
         printf (&ldquo;%d is largest&rdquo;, b);
      else
         printf (&ldquo;%d is largest&rdquo;, c);
   }
}

输出

enter 3 numbers = 10 20 30
30 is largest

Else – if ladder

它是一个多路决策条件。

Syntax

if (condition1)
   stmt1;
else if (condition2)
   stmt2;
   - - - - -
   - - - - -
else if (condition n)
   stmt n;
else
   stmt x;

流程图

使用流程图和程序来描述C语言中的决策概念

示例

以下示例求二次方程的根 -

#include <math.h>
main (){
   int a,b,c,d;
   float r1, r2
   printf ("enter the values a b c");
   scanf (&ldquo;%d%d%d&rdquo;, &a, &b, &c);
   d= b*b &ndash; 4*a*c ;
   if (d>0){
      r1 = (-b+sqrt(d)) / (2*a);
      r2 = (-b-sqrt(d)) / (2*a);
      printf (&ldquo;root1 ,root2 =%f%f&rdquo;, r1, r2);
   }
   else if (d== 0){
      r1 = -b / (2*a);
      r2 = -b/ (2*a);
   printf (&ldquo;root1, root2 = %f%f&rdquo;, r1, r2);
   }
   else
      printf ("roots are imaginary&rdquo;);
}

输出

1) enter the values of a b c : 1 4 3
Root 1 = -1
Root 2 = -3

Switch 语句

它有助于从多个决策中选择一个。

语法

switch (expression){
   case value1 : stmt1;
      break;
   case value2 : stmt2;
      break;
   - - - - - -
   default : stmt &ndash; x;
}

语法

使用流程图和程序来描述C语言中的决策概念

示例

#include<stdio.h>
main (){
   int n;
   printf (&ldquo;enter a number&rdquo;);
   scanf (&ldquo;%d&rdquo;, &n);
   switch (n){
      case 0 : printf (&ldquo;zero&rdquo;)
         break;
      case 1 : printf (&lsquo;one&rdquo;);
         break;
      default : printf (&lsquo;wrong choice&rdquo;);
   }
}

输出

enter a number
1
One

以上是使用流程图和程序来描述C语言中的决策概念的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除