Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are branch and loop statements in C language?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what are branches and loops in C language". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn what branches and loops are in C language.

1. What is a sentence?

C statements can be divided into the following five categories:

Expression statement

Function call statement

Control statement

Compound statement

Empty sentence

Control statements are used to control the execution flow of the program in order to realize various structural ways of the program. They are composed of specific statement definers. There are nine kinds of control statements in C language.

Can be divided into the following three categories:

1. Conditional judgment statements are also called branch statements: if statements, switch statements

two。 Circular execution statements: do while statements, while statements, for statements

3. Turn statement: break statement, goto statement, continue statement, return statement.

2. Branch statement (select structure) 2.1, if statement

Grammatical structure:

/ / single-branch if (expression) statement; if (expression) statement 1; else statement 2; / / Multi-branch if (expression 1) statement 1; else if (expression 2) statement 2; else statement 3

Else match: else matches its nearest if.

2.2.2.The switch statement

Switch is a multi-branch statement

Switch (integer expression) {case shaping constant expression: statement;} 2.2.1, break in switch statement

Switch statements need to be paired with break to achieve real branching

# include int main () {int day = 0; 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;} return 0;}

When needed:

1. Enter 1-5 and output "weekday"

two。 Enter 6-7 and output "weekend"

So our code should be implemented like this:

# include / / switch code demonstrates int main () {int day = 0; 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;}

The actual effect of the break statement is to divide the list of statements into different branches.

Good habit of programming

Add a break statement after the last case statement. (the reason for writing this is to avoid forgetting to add the break statement after the previous last case statement.)

2.2.2default clause

The default statement is executed when all case conditions are not met

3.1.Loop statement 3.1and while Loop

While grammatical structure

While (expression) loop statement

The process that the while statement executes:

Print a number from 0 to 10 on the screen.

# includeint main () {int I = 0; while (I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report