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

A case study of Branch sentences in C language

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

Share

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

Today, the editor will share with you the relevant knowledge points about the example analysis of branch sentences in C language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

What is a statement?

In the C language, there is a semicolon; separated by a statement.

Printf ("hehe"); 1 + 2; Branch statement (select structure)

If you study hard, get a good offer and get to the top of your life.

If you don't study, graduation is equal to unemployment. Go home and lie flat.

That's the choice!

If statement

If structure of multiple statements

Suspended else

When you write this code:

Correction:

A comparison of if Writing forms

Classroom exercises

Judge whether a number is odd or not

Output odd numbers between 0100

Switch statement

The switch statement is also a branch statement. It is often used in multi-branch situations.

Statement item

Break in switch statement

In the switch statement, we can't implement the branch directly, we can only achieve the real branch with break.

Code example: (suggest you implement it yourself?)

# include int main () {int day = 0; scanf ("% d", & day); switch (day) / / must contain integer {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: break;} return 0;}

If the requirements change:

Input 1-5 and output "weekday"

Enter 6-7 and output "weekend"

So our code should be implemented like this:

Summary: make different branch loops according to different needs.

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.)

Default clause

What if the expressed value does not match the values of all case tags?

In fact, it's nothing, the structure is that all the sentences are skipped.

The program does not terminate and does not report an error, because this situation is not considered appropriate in C #.

But what if you don't want to ignore the values of expressions that don't match all tags?

You can add a default clause to the list of statements and put the following tag

Default:

Write in any location where the case tag can appear.

When the value of the switch expression does not match the values of all case tags, the statement following the default clause is executed.

Therefore, only one default clause can appear in each switch statement.

But it can appear anywhere in the list of statements, and the statement flow runs through the default clause as if it were through a case tag.

Good habit of programming

It's a good habit to put a default clause in each switch statement, and you can even add a break after it.

These are all the contents of the article "case Analysis of C language Branch sentences". Thank you for your reading. I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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