branching and looping structures
introduce
C language is a structured programming language. The structure here refers to the sequential structure, selection structure, and loop structure. The C language can implement these three structures. In fact, if we analyze carefully, what we daily Everything you see can be broken down into these three structures or a combination of these three structures.
We can use if
, switch
to implement the branch structure, and use for
, while
, do while
to implement the loop structure.
branch structure
if statement
if(表达式)
语句1
else
语句2
If the expression is established (true), the statement is executed; if the expression is not established (false), the statement is not executed.
In C language, 0 means false and non-0 means true., that is, if the result of the expression is 0, the statement will not be executed; if the result of the expression is not 0, the statement will be executed.
Let’s demonstrate by judging whether you are an adult:
#include<stdio.h>
int main()
{
int age=0;
scanf("%d",&age);
if(age<18)
printf("已成年\n");
else
printf("未成年\n");
return 0;
}
Of course, this is just the most basic structure. There are three more complex ones:
A branch contains multiple statements
There is only one statement in the above, whether it is if
or else
after, what if there are more? then you need to {}
enclose the statement with
#include<stdio.h>
int main()
{
int score=0;
scanf("%d",&score);
if(score>=80)
{
printf("您已达良好:>\n");
printf("考得好,下次继续:>\n");
}
else
{
printf("您未达良好:<\n");
peintf("下次加油:>\n");
}
return 0;
}
Nested if
Multiple judgments can be achieved using nested ifs, the structure is as follows:
if(表达式1)
语句1;
else if()
语句2;
else
语句3;
Leave empty else
Let’s take a random example here:
it can be seen that nothing is printed. This is because although the expression after the first if is true, the expression after the second if is false.
Another important point is:When else
left empty, else
the closest if
match is always used.
Although you know the alignment rules, the logic of the code will be clearer with appropriate curly brackets. Therefore, you should pay attention to the use of brackets when writing code in the future to make the code more readable.
switch statement
General structure:
switch(expression)
{
case value1:statement
case value2:statement
default:statement
}
If expression does not have a corresponding value, default will be executed.
The order of case and default in the switch statement
Is there any required order between clauses and sub-clauses in switch
a statement ? Can it only be put at the end? In fact, there is no order requirement for the statements in the switch statement , as long as your order meets the actual needs. But we usually deal with the clause last.case
default
default
case
default
default
Note:
• The expression after switch must be an integer expression
• The value after case must be an integer constant expression
Why does the error in the picture appear when entering 4? This is because it case4
does not end after entering, but continues to enter case5
..., so switch
you should also pay attention when using statements:
• There must be a space between the case and the following number.
• After the code in each case statement is executed, break needs to be added to jump out of the switch statement.
Loop structure
while loop
while(表达式)
语句;
while
The specific execution process of the statement:
The first step is to execute the judgment expression. If the value of the expression is 0, the loop ends directly; if the value of the expression is not 0, the loop statement is executed. After the statement is executed, it will continue to judge whether to proceed to the next time. judge.
Let’s take an example to learn more about it. Input a positive integer and print each digit of this integer in reverse order
: input: 1234, output: 4 3 2 1
Question analysis:
- To get the lowest digit of n, you can use the operation n%10, and the remainder is the lowest digit, for example: 1234%10 gets 4
- If you want to remove the lowest digit of n and find the second to last digit, you can use the n=n/10 operation to remove the lowest digit. For example: n=1234/10 to get 123. Compared with 1234, 123 has the lowest digit removed. digit, 123%10 will get the second to last digit 3.
- By looping through steps 1 and 2, all bits can be reached before n becomes 0.
#include<stdio.h>
int main()
{
int num=0;
scanf("%d",&num);
while(n)
{
printf("%d ",n%10);
n/=10;
}
return 0;
}
break and continue in while loop
Let's look at the following piece of code:
after printing 1, 2, 3, and 4, when i equals 5, the loop terminates at the break point, no longer prints, and no longer loops.
So break
its function is to permanently terminate the loop. As long as break
it is executed, break
the outer first-level loop will be terminated. After that, if we are in the loop and want to terminate the loop under certain conditions, we can use it break
to achieve the effect we want.
Then we are thinking about a question, what if we break
replace continue
it?
At this point we can analyze it, continue
which can help us skip the code behind a certain loop continue
and go directly to the judgment part of the loop to judge the next loop. If the loop adjustment is at the continue
back, it may cause
an infinite loop. .
for loop
General form:
for(表达式1;表达式2;表达式3)
语句;
Expression 1 is used to initialize the loop variable.
Expression 2 is used to judge the loop end condition.
Expression 3 is used to adjust the loop variable.
for
The loop execution process:
first execute expression 1 to initialize the loop variable, and then execute the judgment of expression 2. Part, if the result of expression 2 == 0, the loop ends; if the result of expression 2 ! = 0, the loop statement is executed. After the loop statement is executed, expression 3 is executed, the loop variable is adjusted, and then Judgment is performed where expression 2 is executed. Whether the result of expression 2 is 0 determines whether the loop continues.
During the entire loop process, the initialization part of expression 1 is only executed once, and the rest is expression 2, loop statement, and expression 3 in the loop.
Let’s do an exercise: Calculate the sum of numbers that are multiples of 3 between 1 and 100
#include <stdio.h>
int main()
{
int i = 0;
int sum = 0;
for(i=1; i<=100; i++)
{
if(i % 3 == 0)
sum += i;
}
printf("%d\n", sum);
return 0;
}
break and continue in for loop
In fact, just like the ones while
in the loop , the ones in the loop are also used to terminate the loop. No matter how many times the loop needs to be looped, as long as it is executed , the loop will be completely terminated. What about that ? Let's go directly to the code. We see that the debugging result is less than 5, so the function of the for loop is to skip the code after this loop and go directly to the adjustment part of the loop. In the future, when a certain condition occurs and this cycle does not need to perform certain subsequent operations, it can be used to achieve this.break
for
break
break
break
continue
continue
continue
do-while loop
General form:
do
语句;
while(表达式);
do-while
Loop execution process: first execute the "statement" on the diagram in the loop, and then execute the "judgment expression" after executing the statement. If the result of the judgment expression is !=0, then the loop will continue and the loop statement will be executed;
judgment do while
The result of the expression == 0, the loop ends.
Therefore, in the do while statement, the loop body is executed at least once, which is do while
a special aspect of the loop.
Let's continue to practice with a question: input a positive integer, how many digits does this integer have?
eg: Input: 24534 Output: 5
#include <stdio.h>
int main()
{
int num = 0;
scanf("%d", &num);
int cnt = 0;
do
{
cnt++;
num = num / 10;
} while (num);
printf("%d\n", cnt);
return 0;
}
It is not necessary to use statements here do while
, but this code is more suitable for using do while
loops, because even if num is 0, it is still a 1-digit number, and the number of digits needs to be counted.
break and continue in do-while loop
do-while
break
The sum in the loop for
is while
very similar. What about that continue
? From the above code
, you can see that the program is in an infinite loop. This is because continue
the following code is skipped, so that it i
is always 5, so the function in the loop is to skip the last code in this loop do-while
and go directly to the judgment part of the loop.continue
continue