当前位置:网站首页>c语言选择,循环概述
c语言选择,循环概述
2022-06-30 01:02:00 【华为云】
选择语句
常见的逻辑运算符
if
#include <stdio.h>int main(){ int a; scanf("%d", &a); if (a > 1) printf("a大于1"); return 0;}if…else…
条件成立执行if语句中的内容,否则执行else后面的内容
#include <stdio.h>int main(){ int a; scanf("%d", &a); if (a >= 60) printf("及格"); else printf("不及格"); return 0;}if…else if…else
对比上面的也不难猜出,
如果…又如果…否则…
#include <stdio.h>int main(){ int a; scanf_s("%d", &a); if (a >= 60 && a <= 70) printf("及格"); else if (a > 70 && a <= 80) printf("良好"); else if (a > 80) printf("优秀"); else printf("不及格"); return 0;}表达式1?表达式2:表达式3
意思就是当表达式1为真的时候,执行表达式2,否则执行表达式3
#include<stdio.h>int main() { char ch; scanf("%c",&ch); ch = (ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch; printf("%c", ch); return 0;}switch语句
swtich(表达式) { // ()中的数据类型仅仅支持整数 case 常量1 : 要执行的语句; break; case 常量2 : 要执行的语句; break; case 常量3 : 要执行的语句; break; default: 要执行的语句; break;}注意:default,当上述条件都不满足的时候,执行default语句
例题
- 判断闰年
- 判断字符是否是大写字母,如果是小写字母转换为大写,否则直接输出(见上面条件表达式代码)
//闰年#include<stdio.h>int main() { int y; scanf_s("%d", &y); if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { printf("%d是闰年", y); } else { printf("%d不是闰年", y); } return 0;}循环语句
for
试想一个操作,我们定义一个数组大小为10,然后要给这个数组中存入数据,
int a[10];a[0]=1;a[1]=2;......a[9]=10;for(int i=0;i<10;i++){ a[i]=i+1;}int i=0;为循环初始条件
i<10;为循环条件,当i<10时循环继续进行
i++;i每次循环自增1
i++,++i
i++,先执行操作,再自增
++i,先自增,再执行操作
while和do……while
while循环:
while 循环的关键点是循环可能一次都不会执行。当条件为 false 时,会跳过循环主体,直接执行紧接着 while 循环的下一条语句。
#include <stdio.h> //输出0-9int main (){ int a = 0; while( a < 10 ) { printf("a 的值: %d\n", a); a++; } return 0;}与while循环不同的地方是:
do…while循环至少执行一次
#include <stdio.h>int main (){ int a = 0; do { printf("我虽然不满足条件,但是也执行一次"); }while(a>0); return 0;}例题:
假设用户的密码是一个三位整数,令用户输入密码,如果输入成功,则提示welcome,如果输入失败,则提示”bad password“,如果三次都没有输入成功,则锁定,提示:“user locked”
#include <stdio.h>int main(){ int key = 123; int times = 0;//次数 bool passed = 0; do { times++; int input = 0; printf("请输入密码:"); scanf_s("%d", &input); if (key == input) { passed = 1; break; } else { printf("bad password\n"); } } while (times < 3); if (passed) { printf("Welcome"); } else { printf("user locked"); } return 0;}函数
我不打算讲多少,函数声明改错题,选择题考的多!
函数的声明( C函数原型),告诉编译器函数的类型,且需要在其他地方找到该函数的定义。
函数的定义,函数的具体实现,确切的指定了函数的具体功能。
函数的调用,导致该函数被执行。
函数必须先声明后使用。函数定义在调用之前,可不声明,函数定义在调用之后或在其他文件中,那么调用之前必须先声明。被调用函数声明可在调用函数之外,也可在调用函数内任何可以放置变量声明的位置。
下面我们来定义一个返回最大值的函数
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }函数声明
int max(int num1, int num2);函数调用
#include <stdio.h> /* 函数声明 */int max(int num1, int num2); int main (){ /* 局部变量定义 */ int a = 100; int b = 200; int ret; /* 调用函数来获取最大值 */ ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0;} /* 函数返回两个数中较大的那个数 */int max(int num1, int num2) { /* 局部变量声明 */ int result; if (num1 > num2) result = num1; else result = num2; return result; }最后
还有结构体,指针,我不打算讲了,鉴于确实有难度,如果还需要加强学习,欢迎到博主专栏查看,最后祝愿:各位学渣,c语言必过!!!
边栏推荐
- Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture
- shell 规范日期格式
- 【云原生】容器场景下的内核安全
- Analysis of natural frequency and buckling load of cylinder by finite element method
- What if you can't write your composition well? Ape counseling: parents should pay attention to these points
- How to create a module in the idea and how to delete a module in the idea?
- Practical application of information security
- 如何统一项目中包管理器的使用?
- Yunna | advantages of fixed assets system management, what are the characteristics of fixed assets management system
- 阿四的情绪波动
猜你喜欢

Clean, talk, bring children, and get rid of the label of "artificial mental retardation" for the sweeper

Newton method (optimization of two variable functions)

解决choice金融终端Excel/Wps插件修复visual basic异常

面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?

Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?

优秀的测试/开发程序员与普通的程序员对比......

数字垃圾是什么?跟随世界第一位AI艺术家,探索元碳艺术

作文总写不好怎么办?猿辅导:家长要注意这几点

How to view the size of all files in a folder?

IDEA中的常用设置
随机推荐
Cloud, IPv6 and all-optical network
After the element uses align items center and overflow auto, some contents are not fully displayed
How latex enters a matrix
[MySQL basic] general syntax 2
CSV文件格式——方便好用个头最小的数据传递方式
[mrctf2020]ezpop-1 | PHP serialization
如何统一项目中包管理器的使用?
数据中台咋就从“小甜甜”变成了“牛夫人”?
【深度学习编译】算子编译 IR 转换
阿洛觉得自己迷茫
Cantilever beam calculation [matlab code]
xshell中怎么切换到root用户
Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)
[Simulation Proteus] détection de port 8 bits 8 touches indépendantes
Experience of C language course design: open source sharing of "push box" course design works
阿于的彩虹桥
Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
【three.js】WEB3D初次体验
HC32M0+ GPIO
Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt