当前位置:网站首页>C language selection, loop overview
C language selection, loop overview
2022-06-30 01:04:00 【Hua Weiyun】
Select statement
Common logical operators 
if
#include <stdio.h>int main(){ int a; scanf("%d", &a); if (a > 1) printf("a Greater than 1"); return 0;}if…else…
Implementation of conditions if What's in the sentence , Otherwise execution else Later
#include <stdio.h>int main(){ int a; scanf("%d", &a); if (a >= 60) printf(" pass "); else printf(" fail, "); return 0;}if…else if…else
It is not difficult to guess by comparing the above ,
If … And if … otherwise …
#include <stdio.h>int main(){ int a; scanf_s("%d", &a); if (a >= 60 && a <= 70) printf(" pass "); else if (a > 70 && a <= 80) printf(" good "); else if (a > 80) printf(" good "); else printf(" fail, "); return 0;}expression 1? expression 2: expression 3
It means when expression 1 When it's true , Execute expression 2, Otherwise, execute the expression 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 sentence
swtich( expression ) { // () Only integers are supported for data types in case Constant 1 : Statement to execute ; break; case Constant 2 : Statement to execute ; break; case Constant 3 : Statement to execute ; break; default: Statement to execute ; break;}Be careful :default, When the above conditions are not met , perform default sentence
Example
- Judgement of leap year
- Determine whether the character is a capital letter , If lower case letters are converted to upper case , Otherwise direct output ( See the conditional expression code above )
// Leap year #include<stdio.h>int main() { int y; scanf_s("%d", &y); if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { printf("%d It's a leap year ", y); } else { printf("%d It's not a leap year ", y); } return 0;}Loop statement
for
Imagine an operation , We define an array size as 10, And then you're going to put data into this array ,
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; Is the initial condition of the cycle
i<10; Is the cyclic condition , When i<10 The cycle continues
i++;i Self increment per cycle 1
i++,++i
i++, Perform the operation first , Since the increase again
++i, First of all, increase by yourself , Perform the operation again
while and do……while
while loop :
while The key point of the loop is The loop may not execute once . When conditions are false when , Will skip the body of the loop , Direct execution followed by while The next statement of the loop .
#include <stdio.h> // Output 0-9int main (){ int a = 0; while( a < 10 ) { printf("a Value : %d\n", a); a++; } return 0;} And while The difference in the cycle is :
do…while Loop at least once
#include <stdio.h>int main (){ int a = 0; do { printf(" Although I don't meet the conditions , But do it again "); }while(a>0); return 0;}Example :
Suppose the user's password is a three digit integer , Make the user enter the password , If the input is successful , Prompt welcome, If input fails , Prompt ”bad password“, If the input fails three times , Then lock , Tips :“user locked”
#include <stdio.h>int main(){ int key = 123; int times = 0;// frequency bool passed = 0; do { times++; int input = 0; printf(" Please input a password :"); 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;}function
I'm not going to say much , Function declaration Correct the wrong questions , Multiple choice questions !
Declaration of functions ( C The function prototype ), Tell the compiler the type of the function , And you need to find the definition of this function elsewhere .
Definition of function , The concrete realization of function , It specifies the specific functions of the function .
Function call , Causes the function to be executed .
The function must Declare before use . Function definition before calling , Don't say , Function definition after call or in other files , You must declare before calling . The called function declaration can be outside the calling function , You can also place variable declarations anywhere in the calling function .
Let's define a function that returns the maximum value
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }Function declaration
int max(int num1, int num2);Function call
#include <stdio.h> /* Function declaration */int max(int num1, int num2); int main (){ /* Definition of local variables */ int a = 100; int b = 200; int ret; /* Call the function to get the maximum value */ ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0;} /* The function returns the larger of the two numbers */int max(int num1, int num2) { /* Local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }Last
And the structure , The pointer , I'm not going to talk about , Given the difficulty , If you still need to strengthen your study , Welcome to the blogger column , Best wishes : Everyone, learn from the scum ,c Language must pass !!!
边栏推荐
- Too voluminous ~ eight part essay, the strongest king of interview!
- 出门在外保护好自己
- Developers, why does the maturity of container technology herald the arrival of cloud native era?
- R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题
- 利用tsne将不同句子关于相似度可视化出来
- Text classification using huggingface
- 作文总写不好怎么办?猿辅导:家长要注意这几点
- A Si's mood swings
- The SQL statement concat cannot find the result
- numpy的索引和图片的索引一样吗?
猜你喜欢

如何在IDEA中自定義模板、快速生成完整的代碼?

Seata 与三大平台携手编程之夏,百万奖金等你来拿

一文读懂,MES管理系统模块功能

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

如何查看一个文件夹下所有文件的大小?

Analysis of natural frequency and buckling load of cylinder by finite element method

岁月不饶人
![[mrctf2020]ezpop-1 | PHP serialization](/img/f8/6164b4123e0d1f3b90980ebb7b4097.png)
[mrctf2020]ezpop-1 | PHP serialization

Live broadcast configuration of crmeb knowledge payment system program configuration (method 2)

A Yu's Rainbow Bridge
随机推荐
Outsourcing work for three years, waste a step confused
A Yu's Rainbow Bridge
IDEA中的常用设置
Unity编辑器随机生成物体,更换场景之后物体丢失问题解决
Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?
Simple pages
如何查看一个文件夹下所有文件的大小?
Outsourcing for 3 years is a waste
[cloud native] kernel security in container scenario
[three.js] Web3D first experience
传统微服务框架如何无缝过渡到服务网格 ASM
Go 中的 UDP 服务器和客户端
Practical application of information security
In 2022, the latest and most detailed idea associated database method and visual operation of database in idea (including graphic process)
What if you can't write your composition well? Ape counseling: parents should pay attention to these points
Time flies that year
The unity editor randomly generates objects. After changing the scene, the problem of object loss is solved
干外包3年,真废了...
Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
测试用例设计方法之等价类划分方法