Branch and Loop Statements (2)

3.2 for loop

3.2.1 Syntax of the for statement

for statement syntax structure

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

#include <stdio.h>

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


The execution flow of the for statement is as follows:
for statement execution flow


Now let's compare the for loop and the while loop:

#include <stdio.h>

int main()
{
    
    
	int i = 1;//1.初始化

	while (i <= 10)//2.判断
	{
    
    
		printf("%d ", i);
		i++;//3.调整
	}

	return 0;
}

It can be found that the three necessary conditions of the loop still exist in the while loop, but due to the style problem, the three parts are likely to deviate far away, so the search and modification are not concentrated and convenient enough. Therefore, the style of the for loop is better, and the frequency of the for loop is also the highest.

3.2.2 break and continue in for loop

break:

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	
	for (i = 1; i <= 10; i++)
	{
    
    		
		if (5 == i)
		{
    
    
			break;
		}

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

//1 2 3 4

continue:

#include <stdio.h>

int main()
{
    
    
	int i = 0;

	for (i = 1; i <= 10; i++)
	{
    
    
		if (5 == i)
		{
    
    
			continue;
		}

		printf("%d ", i);
	}

	return 0;
}

//1 2 3 4 6 7 8 9 10

3.2.3 The loop control variable of the for statement

suggestion:

  1. Do not modify the loop variable in the for loop body to prevent the for loop from getting out of control.
  2. It is recommended that the value of the loop control variable of the for statement be written in the way of "open interval after closing first".
#include <stdio.h>

int main()
{
    
    
	int arr[10] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	//              0  1  2  3  4  5  6  7  8  9
	int i = 0;//[0,10)

	for (i = 0; i < 10; i++)
	{
    
    
		printf("%d ", arr[i]);
	}

	return 0;
}

3.2.4 Some for loop variants

#include <stdio.h>

int main()
{
    
    
	//初始化和调整部分的省略就是啥都不做
	//判断部分省略了,意味着判断是恒为真的
	//建议不要随便省略
	for (;;)
	{
    
    
		printf("hehe\n");
	}

	return 0;
}

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	int j = 0;

	for (i = 0; i < 4; i++)
	{
    
    
		for (j = 0; j < 4; j++)
		{
    
    
			printf("hehe\n");
		}
	}

	return 0;
}

//打印16个hehe

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	int j = 0;

	for (; i < 4; i++)
	{
    
    

		for (; j < 4; j++)
		{
    
    
			printf("hehe\n");
		}

	}

	return 0;
}

//打印4个hehe

//使用多余一个变量控制循环

#include <stdio.h>

int main()
{
    
    
    int x = 0;
    int y = 0;
    
    for (x=0, y=0; (x<2) && (y<5); ++x, y++)
    {
    
    
        printf("hehe\n");
    }
   
    return 0;
}

//打印2个hehe

3.2.5 A written test question

//请问循环要循环多少次?

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	int k = 0;
	
	for (i = 0, k = 0; k = 0; i++, k++)
		k++;
	
	return 0;
}

//循环0次

3.3 do while loop

3.3.1 Syntax of the do statement

do statement syntax structure
So what is the execution flow of the do statement?
do statement execution flow

3.3.2 Features of the do statement

The loop is executed at least once, and the usage scenarios are limited, so it is not often used.

//使用do while语句打印1~10

#include <stdio.h>

int main()
{
    
    
	int i = 1;
	
	do
	{
    
    
		printf("%d ", i);
		i++;
	} while (i <= 10);

	return 0;
}

3.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);
		i++;
	} while (i <= 10);

	return 0;
}

//1 2 3 4

continue:

#include <stdio.h>

int main()
{
    
    
	int i = 1;
	
	do
	{
    
    		
		if (5 == i)
		{
    
    
			continue;
		}

		printf("%d ", i);
		i++;
	} while (i <= 10);

	return 0;
}

//1 2 3 4 死循环

3.4 Exercises

3.4.1 Computing the factorial of n

//5! = 1*2*3*4*5
//n! = 1~n 累积相乘

//不考虑溢出
#include <stdio.h>

int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	int ret = 1;

	for (i = 1; i <= n; i++)
	{
    
    
		ret = ret * i;
	}

	printf("%d\n", ret);

	return 0;
}

3.4.2 Calculate 1!+2!+3!+...+10!

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	int n = 0;
	int sum = 0;

	for (n = 1; n <= 10; n++)
	{
    
    
		int ret = 1;

		for (i = 1; i <= n; i++)
		{
    
    
			ret = ret * i;
		}

		sum = sum + ret;
	}

	printf("%d\n", sum);

	return 0;
}

//以下方法更好

#include <stdio.h>

int main()
{
    
    
	int n = 0;
	int ret = 1;
	int sum = 0;
	//1!
	//2! = 1*2
	//3! = 1*2*3
	//4! = 1*2*3*4
    //每次在前面一次的基础上多乘了一个数,所以不需要每次都把ret改成1重来一遍
	for (n = 1; n <= 10; n++)
	{
    
    
		ret *= n;
		sum += ret;
	}

	printf("%d\n", sum);

	return 0;
}

3.4.3 Find a specific number n in an ordered array

#include <stdio.h>

int main()
{
    
    
	int arr[] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//升序
	int k = 7;
	int i = 0;
	
	for (i = 0; i < 10; i++)
	{
    
    
		if (k == arr[i])
		{
    
    
			printf("找到了,下标是%d\n", i);
			break;
		}
	}

	if (10 == i)
	{
    
    
		printf("找不到了\n");
	}

	return 0;
}

//以下方法更好

#include <stdio.h>

int main()
{
    
    
	int arr[] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//升序
	//printf("%d\n", sizeof(arr));//计算数组的总大小,单位是字节
	//printf("%d\n", sizeof(arr[0]));//4
	//printf("%d\n", sizeof(arr) / sizeof(arr[0]));
	int k = 7;
	int sz = sizeof(arr) / sizeof(arr[0]);
	
	//1
	int left = 0;
	int right = sz - 1;
	int flag = 0;//flag的作用是标志是否找到了
	//2
	while (left <= right)
	{
    
    
		int mid = (left + right) / 2;

		if (k == arr[mid])
		{
    
    
			printf("找到了,下标是:%d\n", mid);
			flag = 1;
			break;
		}
		else if (arr[mid] < k)
		{
    
    
			left = mid + 1;
		}
		else
		{
    
    
			right = mid - 1;
		}
	}

	if (0 == flag)
	{
    
    
		printf("没找到\n");
	}

	return 0;
}

binary search

3.4.4 Multiple characters move from both ends and converge toward the middle

//welcome to bit!!!!!
//###################
//w#################!
//we###############!!
//wel#############!!!
//...
//welcome to bit!!!!!


//welcome to bit!!!!!
//###################

#include <stdio.h>
#include <string.h>
#include <windows.h>//Sleep需要一个windows.h的头文件
#include <stdlib.h>//system需要一个stdlib.h的头文件

int main()
{
    
    
	char arr1[] = "welcome to bit!!!!!";
	char arr2[] = "###################";
	int left = 0;
	int right = strlen(arr1) - 1;

	while (left <= right)
	{
    
    
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);//单位是毫秒
		system("cls");//system函数可以执行系统命令,cls是清理屏幕
		left++;
		right--;
	}
	
	printf("%s\n", arr2);
	
	return 0;
}

3.4.5 Simulate user login scenario

Write code to simulate user login scenarios, and can only log in three times. (It is only allowed to enter the password three times. If the password is correct, it will prompt that the login is successful; if it is entered incorrectly three times, the program will exit.)

//假设密码是:"123456"

#include <stdio.h>
#include <string.h>

int main()
{
    
    
	int i = 0;
	char password[20] = {
    
     0 };

	for (i = 0; i < 3; i++)
	{
    
    
		printf("请输入密码:>");
		scanf("%s", password);
		//判断密码是否正确
		//两个字符串比较相等是不能使用==的,应该使用strcmp库函数	
		//strcmp返回0表示2个字符串相等
		//strcmp返回>0的数字,表示第一个字符串大于第二个字符串
		//strcmp返回<0的数字,表示第一个字符串小于第二个字符串
		//字符串比较大小是比较对应位置上的ASCII码值
		//
		//abcd\0
		//abq\0
		//
		//abq大于abcd

		if (0 == strcmp(password, "123456"))
		{
    
    
			printf("登录成功\n");
			break;
		}
		else
		{
    
    
			printf("密码错误\n");
		}

	}

	if (3 == i)
	{
    
    
		printf("退出程序\n");
	}

	return 0;
}

//也可以这样写

#include <stdio.h>
 
int main()
{
    
    
	int i = 0;
	char password[20] = {
    
     0 };
	int flag = 0;

	for (i = 0; i < 3; i++)
	{
    
    
		printf("请输入密码:>");
		scanf("%s", password);
		
		if (0 == strcmp(password, "123456"))
		{
    
    
			printf("登录成功\n");
			flag = 1;
			break;
		}
		else
		{
    
    
			printf("密码错误\n");
		}

	}

	if (0 == flag)
	{
    
    
		printf("退出程序\n");
	}

	return 0;
}

Attached:

Branch and Loop Statements (1)
Branch and Loop Statements (3)

Guess you like

Origin blog.csdn.net/weixin_73077334/article/details/130359028