Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use selection and Loop in C language

Shulou Source: shulou.com Published: 2022-06-01 08:24:49 11月16日 Update

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.

Tags: Loop condition choice statement branch that is computer code expression language content adult limited article life output clear same day after day value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Shulou Technology MySQL Shulou Information