当前位置:网站首页>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 !!!
边栏推荐
- VIM editor common instructions
- Lower expectations
- How to design test cases
- HC32M0+ GPIO
- 测试用例设计方法之等价类划分方法
- Practical application of information security
- 英伟达Jetson Nano的初步了解
- A Si's mood swings
- Analysis of IM instant messaging development technology on modern web
- Too voluminous ~ eight part essay, the strongest king of interview!
猜你喜欢

在线文本数字识别列表求和工具

月薪没到30K的程序员必须要背的面试八股,我先啃为敬!

Crmeb SMS for program configuration of knowledge payment system

2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h

Solve the problem of repairing Visual Basic exceptions with excel/wps plug-in of choice financial terminal

如何做好测试用例设计

How to seamlessly transition from traditional microservice framework to service grid ASM

2020-12-03

Common settings in idea

Equivalence class partition method for test case design method
随机推荐
The SQL statement concat cannot find the result
Crmeb SMS for program configuration of knowledge payment system
Cantilever beam calculation [matlab code]
Arlo felt lost
post请求出现WebKitFormBoundaryk的解决办法
Clean, talk, bring children, and get rid of the label of "artificial mental retardation" for the sweeper
Quick Pow: 如何快速求幂
Seata 與三大平臺攜手編程之夏,百萬獎金等你來拿
SFDP super form development platform v6.0.4 was officially released
[MRCTF2020]Ezpop-1|php序列化
Too voluminous ~ eight part essay, the strongest king of interview!
【Proteus仿真】8比特端口檢測8獨立按鍵
Lower expectations
How to refuse the useless final review? Ape tutoring: it is important to find a suitable review method
利用huggingface进行文本分类
Common settings in idea
Stimulsoft Reports报告工具,Stimulsoft创建和构建报告
Newton method (optimization of two variable functions)
VIM editor common instructions
[proteus simulation] 8-bit port detection 8 independent keys