Introduction to C language (4)

Preface
  Sometimes, we may need to execute the same block of code multiple times.
  In general, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on.
  Programming languages ​​provide various control structures for more complex execution paths.
  Loops allow us to execute a statement or group of statements multiple times.

1. for loop

1.1 Grammar

for(表达式1;表达式2;表达式3)
	循环语句;

Expression 1 is the initialization part , which is used to initialize the loop variable.
Expression 2 is the condition judgment part , which is used to judge the termination of the loop.
Expression 3 is an adjustment part , which is used to adjust the loop condition.

如果判断部分省略,意味着判断恒真。

Example: Use a for loop to print the numbers 1-10 on the screen

#include<stdio.h>
int main()
{
    
    
	int i;
	for (i = 1; i <= 10; i++)
	{
    
    
		printf("%d ", i);
	}
	return 0;
}

The execution flow chart of the for loop:
       insert image description here
for语句只控制一条语句,控制多条语句要加{}。

1.2 break and continue in for loop

//break 代码实例
#include<stdio.h>
int main()
{
    
    
	int i;
	for (i = 1; i <= 10; i++)
	{
    
    
		if (i == 9)
			break;
		printf("%d ", i);
	}
	return 0;
}

Output result:
insert image description here
Why does it end at 8?

The function of break in the for loop:

In fact, as long as a break is encountered in the loop, all subsequent loops will be stopped and the loop will be terminated directly.
So: the break in for is used to permanently terminate the loop.

//continue 代码示例
#include<stdio.h>
int main()
{
    
    
	int i;
	for (i = 1; i <= 10; i++)
	{
    
    
		if (i == 9)
			continue;
		printf("%d ", i);
	}
	return 0;
}

What will be the output of this code?
insert image description here
The role of continue in the for loop:

continue is used to terminate this cycle, that is, the optimized code after continue in this cycle will not be executed again,
but directly jump to the judgment part of the for statement. Carry out the entry judgment of the next cycle.

1.3 The loop control variable of the for statement

//前闭后开
int i=0;
for(i=1;i<11;i++)
{
    
    }
//两边都是闭区间
int i=0;
for(i=1;i<=10;i++)
{
    
    }

1.不可在for循环体内修改循环变量,防止for循环失去控制;
2.建议for语句的循环控制变量的取值采用”前闭后开区间写法。

1.4 Some for loop variants

//代码1
for(;;)
{
    
    
	printf("hh\n");//无限循环打印hh
}
//for循环中的初始化部分、判断部分、调整部分可以省略。
//初始化和调整部分的省略就是啥都不做
//判断部分省略了,意味着判断是恒为真的
//建议不要随便省略

//代码2
int i=0;
int j=0;
for(i=0;i<10;i++)
{
    
    
	for(j=0;j<10;j++);
	{
    
    
		printf("hh\n");//打印10个hh
	}
}

//代码3
int i=0;
int j=0;
for(;i<10;i++)
{
    
    
	for(;j<10;j++)
	{
    
    
		printf("hh\n");//同代码2
	}
}

//代码4-使用多于一个变量控制循环
int x, y;
	for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
	{
    
    
		printf("hh\n");//打印两个hh
	}

How many hh will the following code print?

#include<stdio.h>
int main()
{
    
    
	int i = 0;
	int j = 0;
	//i=0 1 
	for (i=0; i < 4; i++)
	{
    
    
		for (j=0; j < 4; j++)
		{
    
    
			printf("hehe\n");
		}
	}
	return 0;
}

1.5 Exercises

May I ask how many times to loop?

#include<stdio.h>
int main()
{
    
    
	int i = 0;
	int k = 0;
	for (i = 0, k = 0; k = 0; i++, k++)
	{
    
    
		k++;
		printf("hh");
	}
	return 0;
}

for loops can be nested:
insert image description here

2. while loop

2.1 Grammar

while(表达式)
	循环结构;

The process executed by the while statement:
        insert image description here
        
Example: Use the while loop to print the numbers 1-10 on the screen

#include<stdio.h>
int main()
{
    
    
	int i = 1;//初始化
	while (i <= 10)//判断
	{
    
    
		printf("%d ", i);
		i++;//调整
	}
	return 0;
}

2.2 Break introduction

#include<stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		if (i == 5)
			break;//(跳出)终止循环
		printf("%d ", i);
		i = i + 1;//i++;
	}
	return 0;
}

insert image description here

2.3 continue introduction

#include<stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		i = i + 1;
		if (i == 5)
			continue;//跳过本次循环continue后边的代码,直接去while循环的判断部分		printf("%d ", n);

		printf("%d ", i);
	}
	return 0;
}

insert image description here

We found that break and continue can also appear in the while loop, and their meanings are the same as in the for loop.
But there are still some differences.

2.4 Exercises

Use while loop to print odd numbers within 1-100

int main()
{
    
    
	int i = 1;
	while (i <= 100)
	{
    
    
		if(i % 2 == 1)//判断i是奇数的话,就打印i
			printf("%d ", i);
		++i;
	}
	return 0;
}
//打印数字字符,跳过其他字符
#include <stdio.h>
int main()
{
    
    
	char ch = '\0';
	while ((ch = getchar()) != EOF)
	{
    
    
		if (ch < '0' || ch > '9')
			continue;
		putchar(ch);
	}
	return 0;
}

Replenish:

while ((ch = getchar()) != EOF)
	{
    
    
		putchar(ch);
	}
//getchar()--接收字符
//putchar()--打印字符
//EOF -- end of file 文件结束标志
//在函数读取失败的时候返回了EOF
//scanf函数读取成功,返回的是读取到的数据的个数,读取失败返回EOF
//而getchar 读取成功返回字符的ASCII码值,读取失败返回EOF

3. do...while loop

3.1 Syntax of the do statement

do
	循环语句;
while(表达式);

The flow of do...while statement execution:
         insert image description here

3.2 Features of the do statement

Example: Print the numbers 1-10 on the screen using the do...while loop

#include<stdio.h>
int main()
{
    
    
	int i = 1;//初始化
	do
	{
    
    
		printf("%d ", i);
		i++;//调整
	} while (i<=10);//判断

	return 0;
}

循环至少执行一次,使用场景有限,所以不经常使用。

3.3 break and continue in do...while loop

//break
#include<stdio.h>
int main()
{
    
    
	int i = 1;
	do
	{
    
    
		if (5 == i)
			break;
		printf("%d ", i);//1 2 3 4
		i++;
	} while (i<=10);
	return 0;
}

insert image description here

//continue
#include<stdio.h>
int main()
{
    
    
	int i = 1;
	do
	{
    
    
		if (5 == i)
			continue;
		printf("%d ", i);//1 2 3 4 _
		i++;
	} while (i<=10);
	return 0;
}

insert image description here

4. Practice

1. Calculate the factorial of n
2. Calculate 1!+2!+...+10!

first question:

//for循环
//5!=5*4*3*2*1
//4!=4*3*2*1
//3!=3*2*1
#include<stdio.h>
int main()
{
    
    
	int n;
	int i;
	int temp = 1;
	scanf("%d", &n);//输入
	for (i = 1; i <= n; i++)
	{
    
    
		temp = temp * i;
	}
	printf("%d\n", temp);
	return 0;
}
//while循环
#include<stdio.h>
int main()
{
    
    
	int n;
	int i=1;
	int temp=1;
	scanf("%d", &n);//输入
	while (i <= n)
	{
    
    
		temp = temp * i;
		i++;
	}
	printf("%d\n", temp);
	return 0;
}

Second question:

#include<stdio.h>
int main()
{
    
    
	int i;
	int temp = 1;
	int sum = 0;
	for (i = 1; i <= 10; i++)
	{
    
    
		temp *= i;//求每个数字的阶乘
		sum += temp;//求和,相当于:sum=sum+temp;
	}
	printf("%d\n", sum);
	return 0;
}

conclusion

The loop statement ends here! To fully master it requires constant practice.
  Mountains are formed by accumulating soil, and seas are formed by accumulating water.
  See you in the next article.
insert image description here

Guess you like

Origin blog.csdn.net/iLoyo_/article/details/131475443