C language for loop and while loop


1. Single-layer for loop

  • Example: C language implementation to find the sum from 1 to 10 (implemented using for loop)
#include <stdio.h>

int main()
{
   
    
    
	int i, sum = 0;
	
	for (i = 1; i <= 10; i++)
		sum = sum + i;
	
	printf("sum = %d\n", sum);
	
	return 0;
}

The order of execution of a single-layer for loop:
Insert image description here

The first loop executes statement ① first. After executing statement ①, execute statement ②. If statement ② is true, execute statement ④. After executing statement ④, execute statement ③. After executing statement ③, it marks the end of the first loop. ; The second loop executes statement ② first. If statement ② is true, statement ④ is executed. After executing statement ④, statement ③ is executed. The execution of statement ③ marks the end of the second loop execution. The execution order of subsequent statements in the loop is the same as that of the second loop. Same cycle. If statement ② is not true during the loop, the entire loop ends. Statement ⑤ will not be executed until the end of the entire loop, because the for statement and the if statement can control the same range, and can only control the first statement following it. The implication is that statement ⑤ is not in the for loop. internal. It should be noted that statement ① is only executed in the first loop and not executed in other loops. In other words, statement ① will only be executed once during the entire loop. The function of statement ① is actually to initialize the variable, so it only participates in the first loop. It is also worth noting that when you first learn, it is easy to add a semicolon after for (①; ②; ③). I have already mentioned this in "Common problems in if statements in C language >》The empty statement problem in this article describes the principle in detail, and it is still applicable in the for statement. If you are interested, you can take a look.

  • practise
  • C language implementation to find the sum of odd numbers from 1 to 10
#include <stdio.h>

int main()
{
   
    
    
	int i, sum = 0;
	
	for (i = 1; i <= 10; i = i +2)
		sum = sum + i;
	
	printf("sum = %d\n", sum);
	
	return 0;
}
  • C language implementation to find 1+1/2+1/3+…+1/99+1/100

Error code when starting to learn:

#include <stdio.h>

int main()
{
   
    
    	
	int i;
	float sum = 0; // 考虑到和是小数,所以定义sum为float类型
	
	for (i = 1; i <= 100; i++)
		sum = sum + 1/i;
	
	printf("sum = %f\n", sum); // float类型的变量用%f输出

	return 0;
}

Error reason: The data type of1/i was not taken into account. In C language, the result obtained by dividing an integer type by an integer type only has integer digits. For example, 16/3 = 5. In the above program, i is an integer variable, so 1/i is also an integer variable. Therefore, when i is greater than or equal to 2, The value of 1/i is always 0.

Therefore, it is necessary to find a way to change the value of 1/i into a floating point number to achieve the desired function. C language stipulates that as long as one of the dividend and the divisor is a floating point number, the quotient is a floating point number. Therefore, there are two solutions. The first is to write 1/i as 1.0/i, and the second is to write the integer variable iForced type conversion to float type, the correct code is as follows:

Guess you like

Origin blog.csdn.net/weixin_65334260/article/details/125276062