How to use selection and Loop in C language
This article mainly introduces how to use selection and loop in C language, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!
1. Select statement (if)
Let's not talk about switch first, but we will add later. Let's start with a simple if.
If (condition), through the judgment of the condition to determine which branch to execute, multi-branch we can use else if.
Since we are only doing a simple understanding, we look directly at the code.
As shown in the figure, our condition is age > = 18. When we enter 19:00, we determine that the condition is true, so we execute the first branch, that is, we output "you have come of age". Conversely, if the condition is false, we will output "you are not yet an adult".
two。 Cycle
Some things have to be done all the time, such as we have to eat every meal, walk step by step, and the code has to be tapped day after day. In the computer, we call it a loop. Loops are divided into finite loops and infinite loops (infinite loops are usually misoperated, if you're not trying to make fun of them).
Three elements of finite cycle: condition initialization, condition judgment, condition update
So let's go straight to the example.
While cycle
The following iTunes + is i=i+1, and we'll talk about the operator abbreviated below, as you can see, in this while loop, as long as it satisfies I.