2. Branch and loop statements
preface
Tip: C language notes are used as a summary of their own learning and convenient access to forgotten knowledge in the future.
We will also check and make up for deficiencies in the future.
What is a statement?
In C language, a semicolon (;) Separated is a statement.
1. Branch statement
1.1 branch statement (select structure)
Format:
(1) No else part:
if(expression) Statement 1;
(2) There are else parts:
if(expression) Statement 1; else Statement 2;
(3) else nested multiple if:
if(Expression 1) Statement 1; else if(Expression 2) Statement 2; else if(Expression 3) Statement 3; else Statement 4;
If the expression result is true, the statement is executed.
(0 means false, non-0 means true)
Note: generally, else matches the nearest else.
if 1 if 2 else here else Is with if2 matching
If it is
if 1 { if 2 } else here else Is with if2 matching
example
if(condition)
{
return x;
}
returny;
If the condition is true, x is returned; otherwise, y is returned
1.2. Multi branch statement (switch statement)
Format:
switch(Integer expression) { case Expression result 1: Statement 1; break; case Expression result 2: Statement 2; break; case Expression result 3: Statement 3; break; case Expression result 4: Statement 4; break; default: Statement 5; break; }
be careful:
(1). The expression in () in switch () must be integer, not decimal or other types.
(2).break means to jump out of the statements contained in {}, and the statements after break will not be executed.
such as
switch() { case 1: case 2: break; } At this time, if from case 1 If the execution starts, it will be executed at the same time case 2, Then it will be executed break,Jump out of this switch sentence.
(3). default indicates all options except those in the case statement.
(4). There is no order between case and default, and they can also be placed on them,
It is recommended to put it at the bottom.
2. Circular statement
have
- while statement
- for statement
- do while statement
2.1 while statement
Format:
while(expression) { sentence; }
continue function:
For example, in the while statement, the statement after continue will not be executed, but directly jump to the judgment part of the while statement to judge the entry of the next cycle
Note: using continue in while may cause an endless loop
2.2 for statement
Format:
for(Expression 1;Expression 2;Expression 3) { Circular statement; }
Expression 1: initialization part
Expression 2: condition judgment part
Expression 3: adjustment part
for loop suggestions:
- Loop variables cannot be modified in the for loop body to prevent the for loop from losing control
- It is suggested that the value of the loop control variable of the for statement should be written in the interval before closing and after opening, for example, for (I = 0; I < 10;).
(recommended but not required)
for( ; ; )
In this for statement, no expression is added and an endless loop is executed
for(k=0;k=0;k++)
When expression 2 is 0, it is judged as false and ends directly. The loop is 0 times
2.3 do while statement
Format:
do Circular statement; while(expression);
If the while expression is true, the loop will loop, and if it is false, the loop will end.
practical application
1. Half search algorithm (binary search algorithm)
Note: find the middle element and remove half
2. Rolling phase division
explain:
Find the greatest common divisor of m and n
If M > n
m% n = R -- > R is the module of m divided by n
(1)m n r
(1)n r m%n
Repeat (2) until r is 0, then n is the maximum common divisor of m and n
int main() { int m = 0; int n = 0; int r = 0; scanf("%d%d", &m, &n); while (m%n) { r = m%n; m = n; n = r; } printf("%d\n", n); return 0; }
3. Calculate leap years
leap year:
(1) A year that can be divided by 4 and cannot be divided by 100
(2) Year divisible by 400
Demand: find the leap years between 1900 and 2020 and calculate the total number of leap years
#include<stdio.h> int main() { int count = 0; int n = 0; for (n = 1900; n <= 2020; n++) { if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) printf("%d\n", n); count++; } printf("Altogether%d Leap year\n", count); return 0; }
4. Judgment method of prime number
Prime: a natural number that can only be divided by 1 and itself. (1 is not prime)
Requirement: find the prime number in 1-100
#include<stdio.h> int main() { int i = 0; int j = 0; for (i = 1; i <= 100; i++) { for (j =2; j < i; j++) { if (i%j == 0) break; if (j = i) { printf("%d\n", i); } } } return 0; }
5. 99 multiplication table
1 * 1=1
1 * 2=2 2 * 2=4
1 * 3=3 2 * 3=6 3 * 3=9
...
#include<stdio.h> int main() { int i = 0; int n = 0; int sum = 0; while (n<9) { n++; for (i = 1; i <= n; i++) { sum = i*n; printf("%d*%d=%-2d", i, n, sum); printf(" "); } printf("\n"); } return 0; }
Note:%2d indicates the right alignment of two digits
%-2d indicates left alignment of two digits
summary
(1).getchar() indicates input
puchar() indicates output
For example:
int ch=getchar(); putchar(ch); printf("%c\n",ch); return 0;
(2). In scanf ("% d", & x), if 1234 ABCD is input, the output result will be 1234. There will be no ABCD, that is, scanf will only read the value in front of the space.
(3).
(4).
(5).
(6).
(7).
(8).
(9).