当前位置:网站首页>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 !!!
边栏推荐
- Visual studio 2017 cannot open the include file: 'qopenglfunctions_3_3_core': no such file or directory
- Antd - tree structure: default deployment node attribute failure - Basic promotion
- DL: deep learning model optimization model training skills summary timely automatic adjustment of learning rate implementation code
- Time flies that year
- 一文读懂,MES管理系统模块功能
- Taguchi experimental method
- MES管理系统功能模块之质量管理
- Crmeb SMS for program configuration of knowledge payment system
- 如何拒绝期末复习无用功?猿辅导:找准适合自己的复习方法很重要
- Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend
猜你喜欢

快手伸手“供给侧”,找到直播电商的“源头活水”?

Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?

How latex enters a matrix

Time does not spare

Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture

字节面试惨遭滑铁卢:一面就被吊打,幸得华为内推,三面拿到offer

外包干了三年,废的一踏糊涂...

测试用例设计方法之等价类划分方法

Go 中的 UDP 服务器和客户端

我,33岁,字节跳动测试开发,揭开北京“测试岗”的真实收入
随机推荐
2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h
外包干了三年,废的一踏糊涂...
如何做好测试用例设计
HC32M0+ GPIO
Lower expectations
c语言期末不挂科(上)
The SQL statement concat cannot find the result
Outsourcing work for three years, waste a step confused
[cloud native] kernel security in container scenario
开发者,为什么说容器技术的成熟预示着云原生时代的到来?
Text classification using huggingface
In 2022, the latest and most detailed idea associated database method and visual operation of database in idea (including graphic process)
Bytek suffered a disastrous defeat in the interview: he was hanged on one side, but fortunately Huawei pushed him in, and he got an offer on three sides
What are the top ten securities companies? In addition, is it safe to open a mobile account?
练习副“产品”:自制七彩提示字符串展示工具(for循环、if条件判断)
[three.js] Web3D first experience
2020-12-03
存储引擎分析
优秀的测试/开发程序员与普通的程序员对比......
MySQL installation steps (detailed)