当前位置:网站首页>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;
}
边栏推荐
- MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
- Devops' future: six trends in 2022 and beyond
- 堆排序【手写小根堆】
- 记录:Navicat Premium初次无法连接数据库MySQL之解决
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- Knowledge system of digital IT practitioners | software development methods -- agile
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- Chromatic judgement bipartite graph
- 2-year experience summary, tell you how to do a good job in project management
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
猜你喜欢

FairyGUI条子家族(滚动条,滑动条,进度条)

面试必备:聊聊分布式锁的多种实现!
![[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix

Fgui project packaging and Publishing & importing unity & the way to display the UI
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
![[算法] 劍指offer2 golang 面試題2:二進制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 劍指offer2 golang 面試題2:二進制加法

地球围绕太阳转

微信小程序开发心得

How to ensure data consistency between MySQL and redis?

MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
随机推荐
图书管理系统小练习
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
Music playback (toggle & playerprefs)
RTKLIB: demo5 b34f. 1 vs b33
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
isEmpty 和 isBlank 的用法区别
Mixed use of fairygui button dynamics
Compile GDAL source code with nmake (win10, vs2022)
阿里云微服务(四) Service Mesh综述以及实例Istio
Record: Navicat premium can't connect to MySQL for the first time
[GNSS] robust estimation (robust estimation) principle and program implementation
The port is occupied because the service is not shut down normally
[untitled]
What are the advantages of using SQL in Excel VBA
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
抽象类和接口
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Usage differences between isempty and isblank
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing