C language_ Branch and loop statements

Posted by M4ngo on Tue, 01 Feb 2022 04:20:57 +0100

  • C language is a structured programming language: 1. Sequential structure; 2. Select the structure (branch statement or loop statement); 3. Loop structure (loop statement)
  • In C language, what is separated by a semicolon is a statement
  • Branch statements: if, switch
    Circular statements: while, for, do while

1. if statement

int main()
{
	int age=20;
	if(age<18)
		printf("under age\n");
	else
		printf("adult\n");
	return 0;
}
int main()
{
	int age=100;
	if(age<18)
		printf("under age\n");
	else if(age>=18 && age<28)
		printf("youth\n");
	else if(age>=28 && age<50)
		printf("Prime of life\n");
	else if(age>=50 && age<90)
		printf("old age\n");
	else
		printf("Old age\n"); //In if Else if there can be no else
	return 0;
}
  • Else matches the nearest unmatched if. Pay attention to the problem of hanging else
  • The function ends when it executes to return
  • In order to avoid writing if(num==5) as if(num=5) (amplitude operation, its content must be executed), you can write it in reverse, that is, as if(5==num)

2. switch statement

int main()//Function: enter the day of the week
{
	int day=0;
	scanf("%d",&day);
	switch(day)
	{
		case 1:
			printf("Monday\n");
			break;
		case 2:
			printf("Tuesday\n");
			break;
		case 3:
			printf("Wednesday\n");
			break;
		case 4:
			printf("Thursday\n");
			break;
		case 5:
			printf("Friday\n");
			break;
		case 6:
			printf("Saturday\n");
			break;
		case 7:
			printf("Sunday\n");
			break;
	}
	return 0;
}
  • Execution rules of switch statements: judge the number of statements in parentheses after switch (must be integer), select an appropriate one from the case expression (must be integer), and break out after execution.
int main()//Function: input 1-5 to output working days, and input 6 and 7 to output rest days 
{
	int day=0;
	scanf("%d",&day);
	switch(day)
	{
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
				printf("weekdays\n");
				break;
		case 6:
		case 7:
				printf("Rest Day\n");
				break;
		default:
				printf("Input error\n");
				break;
	}
	return 0
}
  • Break in case is not necessary, but it is better to add break in the last case
  • Default is optional; case and default can be put at will, but it's best to follow the recommended
  • continue cannot appear in a switch statement
  • break is to end the current switch statement

3. while statement

Break is to stop, that is, to stop executing the loop statement of this layer after break, so as to jump out of the loop of this layer

  • Continue is to continue, that is, skip the loop statement after continue and execute the statement from the judgment of the loop
int main()//Input a character, then output a character until EOF is input
{
	int ch=0;
	while((ch=getchar())!=EOF)//EOF - end of file
	{
		putchar(ch);
	}
	return 0;
}
int main()//Enter and confirm the password
{
	int ret=0;
	int ch=0;
	char password[20]={0};
	printf("Please input a password:>");
	scanf("%s",password);//Enter the password and store it in the password array
	//Press enter after entering the password, and there will be a "\ n" left in the input buffer, so read the data in the buffer again to empty the buffer; A getchar can only read one character; If the password contains spaces, only the characters before the spaces are valid, and spaces, that is, the characters after them, will remain in the input buffer. Therefore, we need to carry out a cycle to empty the input buffer
	while((ch=getchar())!='\n')
	{
		;
	}
	printf("Please confirm(Y/N):>");
	ret=getchar();
	if(ret=='Y')
	{
		printf("Confirmation successful\n");
	}
	else
	{
		printf("Waiver of confirmation\n");
	}
	return 0;
}
int main()//Print only numbers from character 0 to character 9
{
	int ch=0;
	while((ch=getchar())!=EOF)
	{
		if(ch<'0'||ch>'9')
			continue;
		putchar(ch);
	}
	return 0;
}
int main()//To find a specific number n in an ordered array, use the binary search method (it must be an ordered array)
{
	int arr[]={1,2,3,4,5,6,7,8,9,10};
	int k=7;//If find 7
	int sz=sizeof(arr)/sizeof(arr[0]);//Calculate the number of elements
	int left=0;//Left subscript
	int right=sz-1;
	while(left<=right)
	{
		int mid=(left+right)/2;
		if(arr[mid]>k)
		{
			right=mid-1;
		}
		else if(arr[mid]<k)
		{
			left=mid+1;
		}
		else
		{
			printf("Yes, the subscript is:%d\n",mid);
			break
		}
	}
	if(left>right)
		printf("can't find\n");
	return 0;
}
int main()//Demonstrate that multiple characters move from both ends to converge in the middle
{
	char arr1[]="welcome to home!";//Finally, there is a "\ 0" element
	char arr2[]="################";
	int left=0;
	int right=sizeof(arr1)/sizeof(arr1[0])-2;//Since there is another "\ 0" element at the end, subtract 2 instead of 1; Or use int right=strlen(arr1)-1, because strlen is the length of the string and stops when it encounters "\ 0"
	while(left<=right)
	{
		arr2[left]=arr1[left];
		arr2[right]=arr1[right];
		printf("%s\n",arr2);
		sleep(1000);//Rest for 1 second
		system("cls");//System is a function that executes system commands (the header file is stdlib.h), cls - clear the screen
		left++;
		right--;
	}
	printf("%s\n",arr2);
	return 0;
}

4. for statement

break and continue function similarly in a while statement

  • Do not change the loop variable in the for loop to avoid losing control of the for loop
  • It is suggested that the value of the loop control variable of the for statement should be written in "close before open", that is, for (I = 0; I < 10; I + +), rather than for (I = 0; I < = 9; I + +). Because it's more intuitive. It's 10 cycles, but it's not absolute
  • The initialization, judgment and adjustment of the for loop can be omitted (preferably not omitted), but if the judgment part of the for loop is omitted, the judgment condition will always be true
int main()//Two loop variables can be used to control the loop
{
	int x,y;
	for(x=0,y=0;x<2&&y<5;++x,y++)
	{
		printf("hehe\n");
	}
	return 0;
}
int main()//Calculate 1+ 2!+…+ n!
{
	int n=0;
	int ret=1;
	int sum=0;
	for(n=1;n<=3;n++)
	{
		ret=ret*n;
		sum=sum+ret;
	}
	printf("sum=%d\n",sum);
	return 0;
}
  • for(i=0,k=0;k=0;i++,k + +) this for loop does not cycle once. Because the judgment formula gives 0 to K, the value of the formula is 0 (false), so it is not carried out
int main()//Find a specific number n in an ordered array, using the sequential search method
{
	int arr[]={1,2,3,4,5,6,7,8,9,10};
	int k=7;
	int i=0;
	int sz=sizeof(arr)/sizeof(arr[0]);
	for(i=0;i<sz;i++)
	{
		if(k==arr[i])
		{
			printf("Yes, the subscript is:%d\n",i);
			break;
		}
	}
	if(i==sz)
		printf("can't find\n");
	return 0;
}

5. do... while statement

int main()//Print 1-10
{
	int i=1;
	do
	{
		printf("%d ",i);
		i++;
	}
	while(i<=10);
	return 0;
}
  • "= =" cannot be used to compare whether two strings are equal. A library function should be used - strcmp, eg: strcmp(password, "123456") = = 0// If the two strings are equal, 0 is returned

Topics: C