当前位置:网站首页>C language ---6 first knowledge of selection statement, loop statement, function and array
C language ---6 first knowledge of selection statement, loop statement, function and array
2022-06-10 21:29:00 【Try!】
1、 Select statement
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int input = 0;// Definition input Variable
printf(" Congratulations xxx College admission ");
printf(" Should you choose to study hard (1/0)?>:");
scanf("%d",&input);
if (input == 1)
{
printf(" You will become better ");
}
else
{
printf(" After that, I will be more passive ");
}
return 0;
}
The operation results are as follows :
Congratulations xxx Do you want to study hard for college admission (1/0)?>:1
You will become better
2、 Loop statement
Let's say it's after 30000 lines of code , Ability can be improved , Take it to the next level , How to describe this thing ?
#include<stdio.h>
int main()
{
int line = 0;
while (line < 30000)
{
printf(" Yes %d Line code \n",line);
line++;
}
printf(" Take it to the next level !");
return 0;
}
Execution results :
...
Yes 29998 Line code
Yes 29999 Line code
Take it to the next level !
3、 function
In mathematics , image f(x)=2x+5 This is called a function ,c The same is true for functions in languages .
f(x,y)=x+y------->Add(x,y)=x+y, So how to use C Language to achieve the sum of two numbers ?
The two methods , One is to write code directly , One is to use functions
Method 1 :
int main()
{
int num1 = 0;
int num2 = 0;
scanf("%d %d",&num1, &num2);
int sum = num1 + num2;
printf("sum=%d",sum);
return 0;
}
Method 2 :
int Add(int x, int y)
{
int z;
z = x + y;
return z;
}
int main()
{
int num1 = 0;
int num2 = 0;
scanf("%d %d", &num1, &num2);
int sum =Add(num1,num2);
printf("sum=%d", sum);
return 0;
}
among ,Add What is done is the process of summing , Entrust the sum to Add, Give Way num1 And num2 Sum up , And send the value to sum,Add As a delegate , First in Add Put in the integer parameter x And y, Used to receive incoming num1 as well as num2, Giving a z, Used for x And y And , Figure out z after , take z The value of the return , because z Is an integer , so Add Also integer .
The operation results are as follows :
45 56
sum=101
4、 Array
An array is a collection of elements of the same type .
To define an array :int arr[10]={1,2,3,4,5,6,7,8,9,10};// Define an integer array , discharge 10 Elements and char ch[5]={'a','b','c'}// Incomplete initialization , The remaining defaults to 0
Arrays are accessed using subscripts , The array opens up a space , The elements stored inside , The name of the array is arr, Subscript from 0 Start , Write arr[0] The first element is accessed .
And while Loop and combine , Print out every element .
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9,10};
int i = 0;
while (i < 10)
{
printf("%d",arr[i]);
i++;
}
return 0;
}
Running results :
12345678910
边栏推荐
- Read the source code of micropyton - add the C extension class module (3)
- Error code 1129, state HY000, host 'xxx' is blocked because of many connection errors
- 面试必备——synchronized底层原理的基础知识
- 你的公司会选择开发数据中台吗?
- Portable FDW framework for Pb
- 2 pcs share a set of keyboard and mouse
- 标配双安全气囊,价格屠夫长安Lumin 4.89万起售
- Vertical bar of latex tips absolute value \left\right|
- 20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
- Redis缓存穿透
猜你喜欢
随机推荐
Video monitoring system storage control, bandwidth calculation method
CCF class a conference or journal - regression related papers
Leetcode advanced path - delete duplicates in the sorting array
Nodejs: official document 3 Dgram stream
MySQL Basics
H265 Nalu type judgment and SPS data analysis
Will your company choose to develop data center?
Read the source code of micropyton - add the C extension class module (4)
Brute force method / task assignment
LeetCode 进阶之路 - 136.只出现一次的数字
软件测试工程师是做什么的?
微积分复习1
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
Leetcode advanced road - 125 Validate palindrome string
H.264中NALU、RBSP、SODB的关系
「运维有小邓」自助帐户解锁工具
六级考试-商务英语-考前最后一背
H265 Nalu类型判断及 sps 数据解析
Redis缓存穿透
Quick start to VISSIM simulation









