当前位置:网站首页>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 !!!
边栏推荐
- 如何在IDEA中自定義模板、快速生成完整的代碼?
- PHP wechat merchant transfer to change initiating merchant transfer API
- Go 中的 UDP 服务器和客户端
- Newton method (optimization of two variable functions)
- 快手伸手“供给侧”,找到直播电商的“源头活水”?
- Nested call and chained access of functions in handwritten C language
- [cloud native] kernel security in container scenario
- What if you can't write your composition well? Ape counseling: parents should pay attention to these points
- Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
- Antd - tree structure: default deployment node attribute failure - Basic promotion
猜你喜欢
How to switch to root in xshell
如何统一项目中包管理器的使用?
The listing of Symantec electronic sprint technology innovation board: it plans to raise 623million yuan, with a total of 64 patent applications
xshell中怎么切换到root用户
一文读懂,MES管理系统模块功能
Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture
numpy的索引和图片的索引一样吗?
Precautions for postoperative fundus hemorrhage / / must see every day
Transaction summary on June 25, 2022
如何拒绝期末复习无用功?猿辅导:找准适合自己的复习方法很重要
随机推荐
Solution to webkitformboundaryk in post request
Developers, why does the maturity of container technology herald the arrival of cloud native era?
Newton method (optimization of two variable functions)
Outsourcing work for three years, waste a step confused
玉米地里的小鸟
Time does not spare
Programmers with a monthly salary of less than 30K must recite the interview stereotype. I will eat it first!
81. search rotation sort array II
The SQL statement concat cannot find the result
Equivalence class partition method for test case design method
眼底出血术后需注意事项//每天必看
[proteus simulation] 8-bit port detection 8 independent keys
Good test / development programmers vs. average programmers
Arlo felt lost
[Simulation Proteus] détection de port 8 bits 8 touches indépendantes
数字垃圾是什么?跟随世界第一位AI艺术家,探索元碳艺术
A Yu's Rainbow Bridge
If the amount exceeds 6 digits after the decimal point, only 6 digits will be reserved, and if it is less than 6 digits, it will remain the same - Basic accumulation
Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
Ml: introduction to confidence interval (the difference and relationship between precision / accuracy / accuracy), use method, and detailed introduction to case application