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

An example Analysis of C language Loop and Branch statement

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "C language loop and branch sentence example analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C language loop and branch sentence example analysis" bar!

Write at the beginning

0 means false, non-0 means true.

1. Branch statement

Branch statements, also known as conditional selection statements, are mainly divided into if statements and switch statements.

1.1 if statement

If ()... {} else if ()... {} else {}

Code interpretation:

# include / / here is the header file where the "printf" function is introduced. When using the function, remember to add int main () {int a = 1, b = 2, c = 3; if (a > c) printf ("an is bigger than c"); else if (a > b) printf ("an is bigger than b"); else ("an is smaller than b and c");}

Analysis:

In if (a > c), a > c is false, so printf is skipped ("an is greater than c"); else if is then run, and a > b is also false, so the else statement "an is smaller than b and c" is executed downwards, as shown in the figure above.

Note: if the condition is set in the first if, then the subsequent else if and else statements will not be run.

If ()... {}... Else {}

After understanding the first method, the latter two methods are not difficult to understand. There is a lack of if else statements in this statement, so compared with the previous method, this statement skips the judgment and execution phase of else if at run time.

Or we can understand it this way: else if actually exists here, for example, in this way: else if (0) {expression;}

If ()... {}

There must be no need for me to say more about this sentence. I believe smart friends can understand it themselves.

Summary:

Fill in the conditions in the parentheses of if (). If the condition is valid, the expression in the braces (code block) will be executed, and if the condition is not valid, the system will directly skip the expression to execute the judgment of else if. If the condition is true, run the code block after else if, otherwise, skip it, and finally, if it does not meet the conditions, it will directly execute the statement after else.

1.2 switch

Switch statements are usually used in conjunction with loop statements, so let's take a look at how switch statements are used.

Code format:

Switch (a) / / an is a shaping variable {case 1 break; case 1 expression1 break; / it is not necessary to write break; case 1, but it can be any number that a can take. Default:expression;}

Put an integer variable an in switch (), and the program will jump to the corresponding case statement according to the value of an and execute it. When executing break, the program will jump out of the switch statement, and if there is no corresponding case statement, it will execute the default statement. Take a look at the example below to understand:

/ / convert the number to its position within a week int main () {int day = 0; scanf ("% d", & day); / / enter an integer switch (day) {case 1:printf ("Monday") into the variable day through the keyboard; break; case 2:printf ("Tuesday"); break Case 3:printf ("Wednesday"); I omitted the middle break; / /. The code demonstration here is mainly to let people understand the function, and the long speech is not very good case 7:printf ("Sunday"); break; default:printf ("Please enter the correct number");} return 0;}

As mentioned earlier, the function of break is to jump out of the switch statement. Now I'll show you the effect without break:

Since you didn't jump out, let's move on to the next statement, no problem. Break statements don't have to be added after every case statement. Sometimes we can achieve the desired effect by adding them only where necessary.

two。 Loop statement

Circular statements are mainly divided into three types: for statements, while statements and do while statements, these three statements can be converted into each other, generally speaking, for statements will be more convenient, but in the end, it depends on specific problems and personal preferences.

2.1 while () statement

While (condition)

{

Expression

}

Explanation:

Condition is the condition of judgment.

A statement that executes when the expression condition is true.

Run time:

First of all, the while () statement is to judge the true value of the expression in parentheses. If the condition is true (not 0), it will run the following code block. After running, it will once again enter the condition judgment stage. If the condition is still true, it will continue to run until the condition is false (0) and jump out of the loop.

Here is a small question for you, casually review the if sentence you just talked about (no, it doesn't matter, take your time, here is just to impress you)

1. Print odd numbers within 10.

Is there a partner who wrote this: printf ("% d%d%d%d%d", 1,3,5,7,9)

Haha, it turned out to be no problem, but we are going to be programmers

Int main () {int I = 0 while / remember to assign a value to a variable when you declare it, which is called the initialization of the variable (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