Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of Branch sentences in C language

Shulou Source: shulou.com Published: 2022-05-31 17:46:45 10月04日 Update

This article mainly introduces the relevant knowledge of C language branch sentence example analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this C language branch sentence example analysis article. Let's take a look.

Sentences in C language

C statements can be divided into the following five categories:

1. Expression statement

two。 Function call statement

3. Control statement

4. Compound statement

5. Empty sentence

What I want to share today is: control statements.

So what is a control statement?

To put it simply, it controls the process of program execution, and there are three families in the C language.

Today, let's first introduce to you: the branch family of love choice, and the following will introduce you to the single-minded cycle family and the turning family of love.

The branch family of love choice

There are two members in this family, the omnipotent eldest brother if and the new younger brother switch. The common characteristic of the members of this family is love "choice". Come on, let's walk into this interesting family together.

The omnipotent Big Brother if

The Cognition of Brother if: expression (start with "true" and goodbye to "false")

0 means false, non-0 means true.

Examples are as follows:

# define _ CRT_SECURE_NO_WARNINGS#includeint main () {int a = 0; printf ("attitude towards learning C:\ n"); scanf ("% d", & a); if (0! = a) {printf ("like to learn C\ n") } else {printf ("Don't learn C language\ n");} return 0;}

Print:

Pay attention to getting to know Brother if:

If loves the else closest to him (match).

{} is used to execute multiple statements after the if.

Switch, a younger brother who found another way.

We should pay special attention to the switch statement, whose judgment condition is the integer expression.

Examples are as follows:

# includeint main () {int day = 0; scanf ("% d", & day); switch (day) {case 1: printf ("Monday\ n"); break; case 2: printf ("Tuesday\ n") Break; case 3: printf ("Wednesday\ n"); break; case 4: printf ("Thursday\ n"); break Case 5: printf ("Friday\ n"); break; case 6: printf ("Saturday\ n"); break; case 7: printf ("Sunday\ n") Break; default: printf ("selection error\ n"); break;} return 0;}

Print:

For the need for change

1. Enter 1-5 and output "weekday"

two。 Enter 6-7 and output "weekend"

How can we write:

# include / / switch code demonstrates int main () {int day = 0; scanf ("% d", & day); switch (day) {case 1: case 2: case 3: case 4: case 5: printf ("weekday\ n"); break Case 6: case 7: printf ("weekend\ n"); break;} return 0;}

Summary of switch:

1 means break to jump out of this cycle.

2 all statements are skipped when the value of the switch expression does not match the value of all case tags.

3 default (/ d'f invalid lt/) can appear anywhere in the switch, and only one, so that when the value in the expression does not match the csae, the statement after the default clause will be executed.

4 We usually add a break after each case statement to form a good programming habit.

This is the end of the article on "case analysis of branch sentences in C language". Thank you for your reading! I believe you all have a certain understanding of the knowledge of "C language branch sentence example analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Sentence language family week branch selection example case analysis analysis big brother expression control knowledge omnipotence alternative approach content younger brother member article cycle Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker Redmi NVidia OPPO Reno