当前位置:网站首页>Branch and loop statements
Branch and loop statements
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
Branch statement
if - else
int main()
{
if(condition)// expression
{
// sentence
}
int n = 3;
if (n = 5)// = Is assignment operator Equivalent to if(5) It's not zero, it's zero true Executive judgment
printf("hehe\n");// prints hehe
if (3 == n)// In order to prevent you from appearing, hit one less = You can put constants in front
// At this time, if you miss one = You're going to report a mistake
printf("hehe\n");
int age = 10;
if (18 < age <= 28)// This means to judge first age Is it greater than 18, no , return 0,0<=28
//true return 1 So it will print adult
// If we want to write such logic in mathematics, we should use && Logical operators
printf(" adult \n");
if (age > 18 && age <=28)
printf(" adult \n");
return 0;
}
int main()
{
//if else Without curly braces, you can only control the following line
// Add curly braces when controlling multiline statements
// also else Only with the nearest if matching
// Add curly braces It can make your logic clearer and avoid
// Indent unaligned logical confusion, etc
// So please develop excellent code style , Add curly braces
//int age = 10;
//if (age < 18)
// printf(" A minor \n");
// printf(" Don't drink \n");
//else
// printf(" adult \n");
//if (age < 18)
// printf(" A minor \n");
// if (age > 3)
// printf("age=10");
//else
// printf("age !=10");
int age = 10;
if (age < 18)
{
printf(" A minor \n");
printf(" Don't drink \n");
}
else
{
printf(" adult \n");
}
if (age < 18)
{
printf(" A minor \n");
}
if (age > 3)
{
printf("age=10");
}
else
{
printf("age !=10");
}
return 0;
}
switch
int main()
{
//switch case sentence
//switch(c) c Can not be float type
//case - Only integer constant expressions It can be characters, because characters store characters ASCII code
// Enter a case After the execution, use break end switch
// Otherwise, he will perform other tasks in sequence case until switch end
// When the entered value does not case When the match , use default To receive all other values
// When multiple inputs are one result, please see the code writing below
int day = 0;
scanf("%d", &day);
switch (day)
{
/*case 1: printf(" Monday \n"); break; case 2: printf(" Tuesday \n"); break; case 3: printf(" Wednesday \n"); break; case 4: printf(" Thursday \n"); break; case 5: printf(" Friday \n"); break; case 6: printf(" Saturday \n"); break; case 7: printf(" Sunday \n"); break; default: printf(" Wrong choice \n"); break;*/
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
default:
printf(" Wrong choice \n");
break;
}
return 0;
}
Please keep good programming habits , In the last case perhaps default Added in both break
Loop structure
while
int main()
{
// Print 1-100 The odd number
// Both ideas are ok
int i = 1;
while (i < 100)
{
//if (i % 2 == 1)
//{
// printf("%d ", i);
//}
//i++;
printf("%d ", i);
i += 2;
}
return 0;
}
int main()
{
// Here are some knowledge points
// In fact, when we enter something on the keyboard ( such as asdadf\n) Enter to finish typing --
// It will be saved to an input buffer first (asdadf) --
// This is when we use scanf getchar Taking data is taking data from the cache
//scanf You will encounter spaces , End getting data when entering getchar Take only one data, no matter what
//EOF - end of file End flag at the end of the file
// For example
// Suppose the password is a string
char password[20] = {
0 };
printf(" Please input a password >");
scanf("%s", password);//asde enter asaew asdq
//getchar();// take \n Then the cache is empty , To let you choose Y/N
// When there are many characters in your cache , So you need to empty the cache
int ch = 0;
while ( (ch = getchar()) != '\n')//getchar The return type of is int
{
;
}
printf(" Please confirm the password (Y/N):>");// Direct output NO Don't let you choose
int ret = getchar();
if (ret == 'Y')
{
printf("Yes\n");
}
else
{
printf("NO\n");
}
//int ch = 0;
//while ( (ch = getchar()) != EOF)
//{
// putchar(ch);
//}
return 0;
}
for
int main()
{
// Let's see first while
int i = 0;// initialization
while ( i < 10)// Judge
{
printf("%d ", i);
i++;// adjustment
}
return 0;
}
int main()
{
//for Loop is the most commonly used loop
int i = 0;
for (i = 0; i < 10; i++)//for Loop handle initialization , Judge , Adjustments are put together
// So it's easier to understand
{
printf("%d ", i);
}
// Our usual programming is Way + Write code
// The solution is programming thinking Writing code is to use computer language to realize thinking
// So the cultivation of thinking is very important
return 0;
}
int main()
{
// About writing for Circular advice
//1. Not in for Modify the loop variable in the loop body , prevent for The cycle is out of control
//2. Suggest for The value of cyclic control variable is written in the form of front closed and back open interval ( Except for special circumstances )
int i = 0;
//for(i = 0; i <= 9 ;i++) [0,9]
for (i = 0; i < 10; i++) // [0,10) It's easier to understand what you're doing
{
printf("%d ", i);
// i = 12;
}
return 0;
}
int main()
{
//for If the judgment part of the cycle is omitted, the condition is constant
for (;;)
{
printf("hehe\n");
}
return 0;
}
// seek 1*2*3...*n The factorial
//1!+2!+3!...+n!
int main()
{
int i = 0;
int n = 0;
int ret = 1;
// When asked 20 Left and right factorial times , There will be overflow ,
// This is beyond int Range of type values , Overflow is not discussed here
// For the implementation of large numbers, we need to rewrite a multiplication function for processing
int sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
sum += ret; // Sum of factorials
}
printf("%d\n", ret);
printf("%d\n", sum);
return 0;
}
do while
int main()
{
//do while That is, no matter what, directly cycle once and then judge
// To come up is to do !
int i = 0; // initialization
do
{
printf("%d ", i);
i++;// adjustment
}
while ( i < 10);// Judge
return 0;
}
int main()
{
// Here we are talking about each cycle break and continue situation
int i = 0;
//while (i < 10)
//{
// if (i == 5)
// //break;// Print 0-4
// continue;// Print 0-4 Post dead cycle
// printf("%d ", i);
// i++;
//}
//for (i = 0; i < 10; i++)
//{
// if (i == 5)
// //break;// Print 0-4
// continue;// Print 0-4 6-9
// printf("%d ", i);
//}
do
{
if (i == 5)
//break;// Print 0-4
continue;// Print 0-4 Post dead cycle
printf("%d ", i);
i++;
} while (i < 10);
// In fact, this is because while,do while The adjustment of is continue Back ,
//continue The function of is to jump out of this cycle , Do not execute the following statements and directly enter the next cycle
// therefore i Always equal to 5
return 0;
}
Add
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
//for Each time the loop is executed, the loop body will be adjusted and then judged
// Initialization is performed once
printf("%d ", i);
}
return 0;
}
边栏推荐
- Rt-ppp test using rtknavi
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- 初识C语言(上)
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- 编辑距离(多源BFS)
- Record: the solution of MySQL denial of access when CMD starts for the first time
- Itext 7 生成PDF总结
- 继承和多态(上)
猜你喜欢
[untitled]
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Record: the solution of MySQL denial of access when CMD starts for the first time
[算法] 劍指offer2 golang 面試題2:二進制加法
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
121道分布式面试题和答案
Role movement in the first person perspective
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
XV Function definition and call
随机推荐
使用rtknavi进行RT-PPP测试
架构师怎样绘制系统架构蓝图?
2022国赛Re1 baby_tree
KF UD分解之伪代码实现进阶篇【2】
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
Excel导入,导出功能实现
2年经验总结,告诉你如何做好项目管理
记录:初次cmd启动MySQL拒接访问之解决
继承和多态(下)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
闇の連鎖(LCA+树上差分)
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
Several high-frequency JVM interview questions
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Interview Essentials: talk about the various implementations of distributed locks!
异常:IOException:Stream Closed
编辑距离(多源BFS)
错误: 找不到符号