Branch and loop statements

1. Control statement

It is used to control the execution flow of the program to realize various structural modes of the program. They are composed of specific statement definer. C language has nine control statements.

It can be divided into the following three categories:

  • Conditional judgment statements are also called branch statements: if statements and switch statements;
  • Circular execution statements: do while statement, while statement and for statement;
  • Turn statements: break statements, goto statements, continue statements, and return statements.

2. Branch statement (select statement)

2.1 if statement

2.1.1 single branch statement

#include <stdio.h>
int main()
{
	int age = 0;
	scanf("%d", &age);

	if (age >= 18)
		printf("adult\n");
	return 0; 
}

2.1.2 double branch statement

#include <stdio.h>
int main()
{
	int age = 0;
	scanf("%d", &age);

	if (age >= 18)
		printf("adult\n");
	else
		printf("under age\n");
	return 0; 
}

2.1.3 multi branch statements

#include <stdio.h>
int main()
{
	int age = 0;
	scanf("%d", &age);

	if (age >= 18)
		printf("adult\n");
	else if (age >= 18 && age < 30)
		printf("youth\n");
	else if (age >= 30 && age < 50)
		printf("middle age\n");
	else if (age >= 50 && age < 80)
		printf("Middle aged and elderly");
	else if (age >= 80 && age < 100)
		printf("old age\n");
	else
		printf("God of Longevity\n");
	return 0; 
}

Note: if is followed by a statement by default. If you want to output multiple statements, you need to add {....}

2.1.4 suspended else

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 2;
	if (a == 1)
		if (b == 2)
			printf("heh\n");
	else
			printf("haha\n");
}

Note: else matching: else matches the nearest if. So the output of this program does not execute if else.. Statement, no output.

2.1.5 judge whether a number is odd

#include <stdio.h>
int main()
{
	int n = 0;
	scanf("%d", &n);
	if (n % 2 == 1)
		//if(n%2!=0)
	{
		printf("Odd number\n");
	}
	return 0;
}

2.1.6 odd number between output 1 and 100

//Method 1
#include <stdio.h>
int main()
{
	int i= 1;
	while (i <= 100)
	{
		printf("%d ", i);
		i=i+2;
	}
	return 0;
}
//Method 2
#include <stdio.h>
int main()
{
	int i= 1;
	while (i <= 100)
	{
		//Judge i and print it only if it is an odd number
		if(i%2==1)
		printf("%d ", i);
		i++;
	}
	return 0;
}

2.2 switch statement

The switch statement is also a branch statement, which is often used in the case of multiple branches

#include <stdio.h>
int main()
{
	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;
}
Sometimes our needs change:
Input 1-5 , the output is "weekday";
Input 6-7 , output "weekend"
#include <stdio.h>
int main()
{
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("weekday\n");
		break;
	case 6:
	case 7:
		printf("weekend\n");
		break;
	default:
		printf("Input error\n");
		break;
	}
	return 0;
}
Note: default: write in the position where any case label can appear.
When the value of the switch expression does not match the value of all case tags, the statement after the default clause will be executed.
Therefore, only one default clause can appear in each switch statement.
However, it can appear anywhere in the statement list, and the statement flow executes the default clause like a case tag.

3. Circular statement

3.1 while loop

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

3.1.1 break and continue in while statement

#include <stdio.h>
int main()
{
	int i = 1;
	while (i <= 10)
	{
		if (i == 5)
			break;
		printf("%d ", i);
		i++;
	}
	return 0;
}
//Only 1 2 3 4 was printed 
Summary:
break stay while Role in the cycle:
In fact, in the cycle, as long as you encounter break , stop all the later cycles and directly terminate the cycle.
So: while Medium break Is used for permanent To terminate the loop.
#include <stdio.h>
int main()
{
	int i = 1;
	while (i <= 10)
	{
		if (i == 5)
			continue;
		printf("%d ", i);
		i++;
	}
	return 0;
}
//Dead cycle after printing 1 2 3 4
Summary:
continue stay while The role of the loop is:
continue It is used to terminate this cycle, that is, in this cycle continue The later code will no longer be executed,
Instead, jump directly to while The judgment part of the statement. Determine the inlet of the next cycle.
Example 1:
#include <stdio.h>
int main()
{
	//getchar gets a character and putchar prints a character
	int ch = getchar();
	putchar(ch);
	return 0;
}
#include <stdio.h>
int main()
{
	char password[20] = { 0 };
	printf("Please input a password:>");
	scanf("%s", password);//123456

	printf("Please confirm the password(Y/N):>");
	int ch = getchar();
	if ('y' == ch)
		printf("Confirmation successful\n");
	else
		printf("Confirmation failed\n");
	return 0;
}
//The code reports an error: after scanf reads 123456, put \ n the default into the buffer and getchar reads it

After modification:

#include <stdio.h>
int main()
{
	char password[20] = { 0 };
	printf("Please input a password:>");
	scanf("%s", password);//123456

	//Method 1
	//getchar() processing \ n

	//Method 2
	int tmp = 0;
	while ((tmp = getchar()) != '\n')
	{
		;
	}

	printf("Please confirm the password(Y/N):>");
	int ch = getchar();
	if ('y' == ch)
		printf("Confirmation successful\n");
	else
		printf("Confirmation failed\n");
	return 0;
}

Example 2:

//Print only numeric characters, skipping other characters
#include <stdio.h>
int main()
{
	int ch = '\0';
	while ((ch = getchar() )!= EOF)
	{
		if (ch < '0' || ch>'9')
			continue;
		putchar(ch);
	}
	return 0;
}

3.2 for loop

3.2.1 syntax

for ( expression 1 ; expression 2 ; expression 3 )
Circular statement ;
expression 1
expression 1 by Initialization part , used to initialize the loop variable.
expression 2
expression 2 by Condition judgment part , used to determine when the loop is terminated.
expression 3
expression 3 by Adjustment part For adjustment of cycle conditions.
//Print 1 ~ 10
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		printf("%d ", i);
	}
	return 0;
}

3.2.2} break and continue in the for loop

#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		if (i == 5)
			break;

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

		printf("%d ", i);
	}
	return 0;
}
//Print 1 2 3 4 6 7 8 9 10

Keywords: C

Added by aouriques on Sun, 16 Jan 2022 13:57:11 +0200