当前位置:网站首页>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;
}
边栏推荐
- Employment of cashier [differential constraint]
- 编辑距离(多源BFS)
- [algorithm] sword finger offer2 golang interview question 5: maximum product of word length
- Fgui project packaging and Publishing & importing unity & the way to display the UI
- 服务未正常关闭导致端口被占用
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
- Record: newinstance() obsolete replacement method
- 基本Dos命令
- C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
- Music playback (toggle & playerprefs)
猜你喜欢
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
RTKLIB: demo5 b34f.1 vs b33
一文搞定 UDP 和 TCP 高频面试题!
Fairygui bar subfamily (scroll bar, slider, progress bar)
阿里云微服务(四) Service Mesh综述以及实例Istio
What are the advantages of using SQL in Excel VBA
[algorithm] sword finger offer2 golang interview question 2: binary addition
NovAtel 板卡OEM617D配置步骤记录
[算法] 剑指offer2 golang 面试题2:二进制加法
Fgui project packaging and Publishing & importing unity & the way to display the UI
随机推荐
【RTKLIB 2.4.3 b34 】版本更新简介一
音乐播放(Toggle && PlayerPrefs)
Rt-ppp test using rtknavi
[Chongqing Guangdong education] Shandong University College Physics reference materials
Implementation of Excel import and export functions
记录:Navicat Premium初次无法连接数据库MySQL之解决
抽象类和接口
MySQL backup -- common errors in xtrabackup backup
Several high-frequency JVM interview questions
XV Function definition and call
Mixed use of fairygui button dynamics
All in one 1405: sum and product of prime numbers
The earth revolves around the sun
String类
初识C语言(下)
121 distributed interview questions and answers
Fairygui bar subfamily (scroll bar, slider, progress bar)
堆排序【手写小根堆】
分支语句和循环语句
Code example of MATLAB reading GNSS observation value o file