Initial C language (third class period)

Table of contents

1. Sentence structure

     1. Sequential structure  

     2. Choose structure

     3. Loop structure (this section first introduces while)

Two, function

     1. Definition:

     2. Simple function example

     3. Precautions:



1. Sentence structure

1. Sequential structure  

    The sequence structure is not explained

2. Choose structure

(1)if

1)if...else

if (condition)

{

      Executed statement 1

}

else

{

      Executed statement 2

}

Analysis: Writing method 1: Execute statement 1 if the condition is satisfied; execute statement 2 if the condition is not satisfied.

          Writing method 2: "Condition" is true (not 0) execute statement 1; "condition" is false (0) execute statement 2.

example:

describe

Judging whether an integer is divisible by 5 is a very simple problem. The lazy KiKi still doesn't want to do it by himself, so he finds you to help him write the code, so you can help him.

Enter a description:

The input consists of an integer M (1≤M≤100,000).

Output description:

The output consists of one line that outputs YES if M is divisible by 5, and NO otherwise (results are case sensitive).

Complete topic code:

#include <stdio.h>

int main() {
    int M=0;
    scanf("%d\n",&M);
    if(M%5==0)
        printf("YES");
    else
        printf("NO");
    return 0;
}

2)if....else if....else

if (condition 1)

{

     execute statement 1

}

else if (condition 2)

{

     execute statement 2

}

.......

else if (condition n)

{

    execute statement n

}

else

{

     Execute statement 3

}

Explanation: This is when multiple conditions need to be judged. If condition 1 is met, statement 1 is executed, if condition 2 is not met, then condition 2 is judged... until the else statement is executed.

example:

describe

Known a function y=f(x), when x < 0, y = 1; when x = 0, y = 0; when x > 0, y = -1.   

Enter a description:

One line, enter an integer x. (-10000<x<10000)

Output description:

One line, output the value of y.

  Analysis: There are three judgment conditions for the value of Y here, more than two, so choose multiple conditions to judge

Full code:

#include <stdio.h>
 
int main()
 {
    int x=0;
    int y[3]={1,0,-1};
    scanf("%d",&x);
    if(x<0)
    {
        printf("%d",y[0]);
    }
else if(x==0)
{
    printf("%d",y[1]);
}
else{
    printf("%d",y[2]);
}
    return 0;
}

(2)switch

template: switch(expression)

{

   case constant 1: statement 1;

   case constant 2: statement 2;

   .......

   default : Statement N;

}

At present, there is still a lot of knowledge that has not been learned, and swtich will not be expanded for the time being.

3. Loop structure (this section first introduces while)

while:

  template: while(condition)

{

     loop body

}

Analysis: If the condition is satisfied , enter the while loop ( inside {} ), execute the loop body once ; jump out of the loop body after execution (generally, you can add a statement to change the condition in the loop body) to judge the condition, and execute it again until it is no longer Break out of the loop if the condition is met .

Simply give an example:

describe

There are many people in front of the elevator in Xiaolele School's teaching building, and there are n people waiting for the elevator in front of him. The elevator can take 12 people at a time, and the time required for each up and down is 4 minutes (2 minutes for going up and 2 minutes for going down). Please help Xiaolele calculate how many minutes it will take to take the elevator to the upstairs. (assuming the elevator is initially on the 1st floor)

Enter a description:

The input contains an integer n (0 ≤ n ≤ 109)

Output description:

Output an integer, which is the time it takes for Xiao Lele to reach the upstairs

Full code:

#include <stdio.h>

int main()
{
	int n = 0;
	scanf("%d",&n);
	int t = 0;
	while(n>=0)//一趟电梯四分钟		
	{
		if (n >= 12)
		{
			n -= 12;
			t += 4;
		}
		else {
			t += 2;
			break;
		}
     }
     printf("%d",t);
	return 0;
}

for

A rough introduction:

for(expression1:expression2:expression3)

{

    execute statement

}

like:

for(int i=0:i<10:i++)

{

   printf("I am ikun");

}

do...while

Two, function

1. Definition:

  When it comes to functions, everyone should be familiar with them. In fact, they are similar to the functions in mathematics. So let's start with an example from mathematics

Example: given a function y=x+1 ; given a value of x, a value of y will be obtained; it is similar in c language.

Function: It consists of formal parameters, actual parameters, and function body . The function body is used to achieve a certain function.

2. Simple function example

Let's go directly to the code and find the sum of two numbers.

#include<stdio.h>
int Add(int x,int y)
{
	int z = x + y;
	return z;
}
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d%d", &a,&b);
	c = Add(a,b);
	printf("%d",c);
	return 0;
}

Parsing: formal parameters (x, y), actual parameters (a, b) function name (Add)

Here is to pass the value of the actual parameter to the formal parameter (the formal parameter is just a copy of the data of the actual parameter), and then enter the function (Add) to realize the addition operation (function body function) and return the added value


3. Precautions:

       If you want to write the function Add after the main function (anywhere after the function is defined first), the function will fail to run, and you need to define the function before the main function, as follows

typo;

 Correct code:

#include<stdio.h>
int Add(int x,int y);//在main函数前面先定义
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d%d", &a,&b);
	c = Add(a,b);
	printf("%d",c);
	return 0;
}
int Add(int x, int y)
{
	int z = x + y;
	return z;
}

It is enough to have a rough understanding of how different statements are written and what functions are. Beginners currently need to have a framework.

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131676449