while loop statement in C language

In addition to choices in daily life, there are many things to do in a cycle, such as life at three points and one line, work day after day, repeating the same thing every day, this is a cycle, then in C language What does a loop statement look like? Let's start today's sharing.

while loop

basic grammar format

while(表达式)
{
    语句;
}

Still use a code to explain the execution process of the while loop:

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

It can be seen from the code that the expression i<=10 is judged to be true when i==1, so the statement in the loop body is executed, and 1-10 is finally printed, then we can know that when the expression is true , the while loop will be executed, and when the expression is false, the loop will not be executed. So if the expression is a non-zero number, will the loop continue to execute?

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	while (1)
	{
		printf("a");
	}
	return 0;
}

From the running results, we can see that when the expression is 1, the while loop will always execute the loop body, which becomes an infinite loop.

break in while loop;

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int i = 1;
	while (i<=10)
	{
		printf("%d\n", i);
		if (i==5)
		{
			break;
		}
		i++;
	}
	return 0;
}

Through the previous code, we know that this code is used to print 1-10, so what happens if break is added when i==5?

 It can be seen that after the break is added, when the execution reaches i==5, the subsequent loop is not executed, so it can be understood that the break statement means to terminate the loop.

continue statement in while loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int i = 1;
	while (i<=10)
	{
		if (i == 5)
		{
			continue;
		}
		printf("%d\n", i);
		
		i++;
	}
	return 0;
}

 We know the function of break above, so what is the difference between continue and break? From the running results, we can see that when the output reaches 4, the code enters an infinite loop instead of terminating like break. Why? Let me explain, continue means to continue. When the code is executed to continue, it will skip this cycle and continue to the next time. From the code, when i==5, the cycle is skipped, then i++ will not Execution, the value of i will not change, then i==5 returns to the expression judgment 5<10, enters the loop, then at this time i==5 will trigger continue again, and then i++ cannot be executed, resulting in i The value is always 5, skipping all the time, it becomes an endless loop.

Then we know the basic information of the while loop, let's realize the following two questions.

First: The user enters a number and finds the sum of the digits of the number.

Then we can know from the title that a number may have many digits, so it is not enough to calculate only once, and a circular calculation is required, and to find the sum of the digits, it is necessary to add each digit of the number separately, knowing that After these, let's implement it in detail.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int numb;//定义一个int类型的整数
	int sum = 0;//定义一个int类型的变量用于接受位数之和
	printf("Please input\n");//让用户输入
	scanf("%d", &numb);//接受用户输入的整数
	while (numb!=0) 
	{
		sum += numb % 10;//将整数的位数分离并加在sum中
		numb /= 10;//将已经计算过的位数去除

	}
	printf("该数的位数之和为%d\n", sum);//输出sum的值
	return 0;
}

Before writing this kind of code, we need to take an example and calculate it. Then I will take 123 as an example. When the input numb is 123, it is necessary to calculate 1+2+3, and to separate each digit, then first divide the ones digit The number is separated, then the remainder of 123/10 is the single digit 3, then we get the single digit 3, add 3 to the sum, then the 3 has been added, and the 3 must be removed, then the 123/10 If the quotient is 12, remove 3 and repeat the result. When only 1 is left, 1/10 is 0, so the loop ends. If the number of digits is more than 3, even if the number is n digits, the highest digit /10 must also be 0, then when the highest digit is calculated, the addition has been completed, so the highest digit/10 is 0 is the loop condition, if the separated value/10 is not 0, it means that there are at least two or more The values ​​are not separated, so the loop condition is numb==0.

 Second, this code is very fun and interesting, to output the following picture

 So how to write this code, we can see that the * is output at the beginning, and then a character is used instead of the * for each output, and finally an I Love You is output. Then we can define *best and string as an array of character types, and replace the leftmost value of array 1 with the leftmost value of array 2 every time it is output, and the same is true for the rightmost. Then the value in the array is called by the subscript, the subscript starts from 0, and the subscript needs to be increased by 1 every time it is replaced. Knowing this, we can implement the code.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	char arr1[] = "I  Love  You";
	char arr2[] = "************";
	int left = 0;
	//int right = sizeof(arr1) / sizeof(arr1[0]) - 2;//[a,b,c,\0]
	int right = strlen(arr2)-1;//右下标
	printf("%s\n", arr2);
	printf("\n");
	while (left<right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		left++;
		right--;
		printf("%s\n", arr2);
		printf("\n");
	}
	return 0;
}

Then every time the value is replaced, the left scale will be increased by 1, and the right scale will be decreased by 1. There will always be one time when the two are equal, that is the last character replacement, so the loop condition is left<right.

The above is all the content of today, so do you in front of the screen have a certain understanding of the while loop?

Guess you like

Origin blog.csdn.net/qq_53086187/article/details/123970000