Branch and loop statements

a key
1. Branch statement
if
switch
2. Circular statement
while
for
do while
3.go to statement
C language is a structured programming language
1. Sequential structure
2. Select structure
3. Circulation structure
What is a statement?
In C language, there is a semicolon ";" Separated is a statement.
1, Branch statement (select structure)

1.if statement
Grammatical structure
① if (expression)
sentence;

#include<stdio.h>
int main()
{
	int age;
	scanf("%d", &age);
	if (age < 18)
	{
		printf("under age\n");
	}
	return 0;
}

② if (expression)
Statement 1;
else
Statement 2;

#include<stdio.h>
int main()
{
	int age;
	scanf("%d", &age);
	if (age < 18)
	{
		printf("under age\n");
	}
	else
	{
		printf("Adult");
	}
	return 0;
}

③ if (expression 1) / / multi branch
Statement 1;
else if (expression 2)
Statement 2;
else
Statement 3;

#include<stdio.h>
int main()
{
	int age;
	scanf("%d", &age);
	if (age < 18)
	{
		printf("under age\n");
	}
	else if (age > 18 && age < 50)
	{
		printf("middle age");
	}
	else
	{
		printf("old age");
	}
	return 0;
}

Note: the expression in the if statement is used to judge whether the condition is true. If it is true, it will not be executed if it is false.
How to judge true and false in language learning?
0 means false, and non-0 means true

#include<stdio.h>
int main()
{
	int age = 10;
	if (age = 5)
	{
		printf("hehe\n");
	}

	return 0;
}

The output result of this code is hehe. age=5 is an assignment statement, so age=5 is true. Execute the output statement to judge that age is not equal to 2. Use "= ="

be careful

The above code does not output anything, and the code writing is not standard. else should be combined with the second if.

Conclusion: else is combined with his recent if
correction

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

The output is haha

practice
1. Judge whether a number is odd

#include<stdio.h>
int main()
{
	int num=0;
	scanf("%d", &num);
	if (num % 2 ==1)
	{
		printf("This number is odd");
	}
	return 0;
}

2. Output odd numbers between 1-100

#include<stdio.h>
int main()
{
	int num=1;
	for (num = 1; num < 100; num++)
	{
		if (num % 2 == 1)
		{
			printf("%d ",num);
		}
	}
	return 0;
}
#include<stdio.h>
int main()
{
	int num=1;
	while(num<100)
	{
		printf("%d ", num);
		num = num + 2;
	}
	return 0;
}

2.switch statement
switch statements are often used in the case of multiple branches.
switch (integer expression) / / here must be an integer expression, and the statement item must also be an integer

Statement item;

For example:

#include<stdio.h>
int main()
{
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
	case 1:
		printf("Monday");
		break;
	case 2:
		printf("Tuesday");
		break;
	case 3:
		printf("Wednesday");
		break;
	case 4:
		printf("Thursday");
		break;
	case 5:
		printf("Friday");
		break;
	case 6:
		printf("Saturday");
		break;
	case 7:
		printf("Sunday");
		break;
	}
	return 0;
}

Note: break should be added at the end of each case statement, otherwise it will continue to execute the following statements (break is used to jump out of the loop)
What if the expressed value does not match the value of all case tags?
At this time, the default statement is used to deal with the situation that cannot be handled by the case statement.

#include<stdio.h>
int main()
{
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
	case 1:
		printf("Monday");
		break;
	case 2:
		printf("Tuesday");
		break;
	case 3:
		printf("Wednesday");
		break;
	case 4:
		printf("Thursday");
		break;
	case 5:
		printf("Friday");
		break;
	case 6:
		printf("Saturday");
		break;
	case 7:
		printf("Sunday");
		break;
	default:		
			printf("Input error\n");
			break;	}
	return 0;
}

II Circular statement
1.while statement
while (expression)
Circular statement;

Example: print numbers 1-10

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

break and continue in while statement
break directly ends the loop and continue to skip this loop and execute the next loop

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

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


2.for loop
Syntax:
For (expression 1; expression 2; expression 3)
Circular statement;
Expression 1 is the initialization part, which is used to initialize the of loop variables. Expression 2 expression 2 is a condition judgment part for judging
When does the loop end. Expression 3 is the adjustment part, which is used to adjust the loop conditions.

Example: print numbers 1-10

#include<stdio.h>
int main()
{
	int i;
	for (i = 1; i <= 10; i++)
	//If i is equal to 1 and less than or equal to 10, execute the printf statement, and then i + + makes the next judgment
	{
		printf("%d ", i);
	}
	return 0;
}


Comparison between for loop and while loop

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


The conditions of the while loop are scattered and difficult to view and modify. The condition set of the for loop is easy to view and modify and is used more frequently.
3.do while() loop
Syntax:
do
Circular statement;
while (expression);
Execution process:

Characteristics of do statement:
The loop is executed at least once. The scenarios used are limited, so it is not often used.
Example: output 1-10

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

practice:
1. Calculate the factorial of 10.

#include<stdio.h>
int main()
{
	int i = 1;
	int sum = 1;
	for (i = 1; i <= 10; i++)
	{
		sum = i * sum;
	}
	printf("%d ", sum);
	return 0;
}

2. Calculate the sum of 1-10 factorials

#include<stdio.h>
int main()
{
	int i = 1;
	int sum1 = 1;
	int sum2 = 0;
	for (i = 1; i <=10; i++)
	{
		for (int j = 1; j <=i; j++)
		{
			//sum1 = 1;
			sum1 = j * sum1;
		}
		sum2 = sum2 + sum1;
		sum1 = 1;  //After one cycle, reset sum1 to 1 and calculate the factorial of the next number
	}
	printf("%d ", sum2);
	return 0;
}

Added by Jim_Bo on Mon, 31 Jan 2022 23:59:13 +0200