当前位置:网站首页>[C language] shallow selection, circular statements, functions and arrays
[C language] shallow selection, circular statements, functions and arrays
2022-07-19 09:56:00 【Lixinze__】
Catalog
Selection structure ( Conditional control statements )
choice 、 Loop statement
C Language is 【 structured 】 The programming language of , Contains three structures
- Sequential structure
- Selection structure
- Loop structure
Everything we experience in life can be expressed in three structures .
Sequential structure
The sequential structure is the structure executed from front to back in the order of writing . This kind of structure is the simplest , It is also the most basic structure . Generally, written programs basically use sequential structure .
Selection structure ( Conditional control statements )
- if sentence
- if···else sentence
- Conditional expression (C The only ternary operator in language )
- switch sentence
Take a small example , This article is just a little understanding , The specific content will be explained in detail in the following article .
int main()
{
printf(" Went to University \n");
printf(" Should you study hard \n");
scanf("%d", &input);
if input ==1
{
printf(" Find a good job \n");
}
else
{
printf(" Can't find a good job \n");
}
return 0;
}
Loop structure
- for sentence
- while sentence
- do···while sentence
Next use while Statement introduces a simple example , Details later articles will explain in detail .
int main()
{
int money = 0;
printf(" start-up \n");
while(money < 20000)
{
printf(" To work hard \n");
money++;
}
if(money >= 20000)
{
printf(" happy life \n");
}
else
{
printf(" Make persistent efforts \n");
}
return 0;
}function
function A unit of independent program code that performs a specific task . Function can save us the task of writing code repeatedly , If the program needs to perform a task multiple times , Then we just need to write a suitable function , Just call it whenever you need it . It also makes the program we write more modular , It is convenient for later revision and improvement .
Take a simple example to roughly understand the function .
If we want the sum of two integers , We'll do that .
int main()
{
int n1 = 0;
int n2 = 0;
// Input
scanf("%d %d", &n1, &n2);
// Sum up
int sum = n1 + n2;
// Print
printf("%d\n", sum);
return 0;
}Next, we write a function in advance to calculate the addition of two integers , Then call it when you need to calculate .
int Add(int x, int y)//Add Is the function name , In parentheses are function parameters ,int Is the return type
{
int z = 0;
z = x + y;
return z;
}
// Function body in curly brackets
int main()
{
int n1 = 0;
int n2 = 0;
// Input
scanf("%d %d", &n1, &n2);
// Sum up
int sum = Add(n1, n2);
// Print
printf("%d", sum);
return 0;
}Array
To define an array
Array Is a collection of elements of the same type .
We need to write a program , It is inevitable to deal with a large amount of data , Arrays can process these data efficiently and conveniently .
int arr[10] = {0,1,2,3,4,5,6,7,8,9};The array subscript
C Language policy , Each element of an array has a subscript , The subscript is from 0 At the beginning .
Arrays can be accessed through subscripts .
int arr[10] = {0};
// If the array has 10 Elements , So the subscript range is 0~9 
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9};
printf("%d\n", arr[8]);
// The printout is 9
return 0;
}Of course, it is also possible to print the elements in the array one by one .
#include<stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9 };
int i = 0;
while (i < 9)
{
printf("%d", arr[i]);
i++;
}
return 0;
}We can also create different types of arrays .
int main()
{
int arr[];
char ch[];
double[];
return 0;
}int main()
{
char arr[4] = {'b','i','t'};
// This is incomplete initialization , The remaining parts are initialized as 0
return 0;
}//C99 Before standard , The size of arrays is specified by constants or constant expressions
int arr1[10] = {0};
int arr2[6+4] = {0};
//C99 After standard , Variable length arrays are supported , At this time, the array size is allowed to be a variable
// But the array specified in this way cannot be initialized
int m = 100;
int arr3[m];
// The compiler I use VS Yes C99 The syntax support in is not very good , Variable length arrays are not supported .边栏推荐
- vim诡异的未知函数0
- Componentized advanced -- slot
- Code to celebrate the Dragon Boat Festival -- Zong your heart
- Redis cache avalanche
- Part I - Fundamentals of C language_ 3. Operators and expressions
- 第九章 STL 之 deque
- The inflection point of eth may be just around the corner, which is how to
- 【C语言】指针练习题2——笔试真题及解析
- Part I - Fundamentals of C language_ 4. Procedure flow structure
- [C language] data type and meaning
猜你喜欢
[email protected]|负载CQDs的FeMIL101材料|mof试剂"/>钴铁双金属有机骨架Cox/MIL-100(Fe)|光敏剂@[email protected]|负载CQDs的FeMIL101材料|mof试剂

Rhcsa the next day 7.15

es索引、类型(mapping)、文档、ik分词器
[email protected]|[email protected]<"/>卟啉包裹型金属有机框架材料[email protected]|[email protected]<

Chapter 4 - consistency of first-order multi-agent systems - > consistency of continuous time systems with time delays

18、shell脚本编程(1)
[email protected]@ZIF67纳米材料"/>氮杂环分子改性UiO-66-NH2|聚乙烯亚胺改性UiO-66-NH2|[email protected]@ZIF67纳米材料

Relationship between standardization, normalization and regularization

5分钟就能轻松入门的机器学习基础知识

rhcsa 第二天 7.15
随机推荐
PTA 1037 在霍格沃茨找零钱
Have you learned the database design pattern of multi tenant SaaS?
浏览器的故事
Dedecms dream weaving article list Title repeated display solution
ES Restful操作
2022.7.16-----leetcode.剑指offer.041
redis缓存雪崩
氮杂环分子改性UiO-66-NH2|聚乙烯亚胺改性UiO-66-NH2|[email protected]@ZIF67纳米材料
第4章-一阶多智体系统一致性 -> 连续时间含时延系统一致性
【ACWing】947. text editor
mof定制材料|双金属CuNi-MOF纳米材料|核—壳结构[email protected]纳米复合材料|ZIF-8/聚偏氟乙烯复合纳米纤维膜PVDF
第一部分—C语言基础篇_2. 数据类型
Chapter XI queue of STL
mof定制材料|超薄MOF纳米带|磁性Fe3O4 @Cd-MOF纳米复合材料|ZIF-8/石墨烯复合纳米颗粒
工程效能CI/CD之流水线引擎的建设实践
Interview questions - design test cases for:: memcpy function
PTA 1037 change at Hogwarts
Series operation of vector container (detailed explanation)
Implement word segmentation for text and draw word cloud
Mysqldump full recovery to another new instance, and then perform flush privileges analysis