首页 > 后端开发 > C++ > 正文

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

WBOY
发布: 2023-09-15 11:05:04
转载
1523人浏览过

以下是决策语句 -

  • 简单 - 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条件。

立即学习C语言免费学习笔记(深入)”;

语法

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 中 -

豆包AI编程
豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483
查看详情 豆包AI编程

语法

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中文网其它相关文章!

相关标签:
C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号