当前位置:网站首页>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
原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101730495549.html