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

Example Analysis of Loop 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 the "sample analysis of loops in C language", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of loops in C language".

(1) while statement

The execution process of 1.1while

For example, we achieve:

Print a number from 1 to 10 on the screen.

Introduction to break and continue1.2.1 break in 1.2while statement

Summary: the role of break in the while loop:

In fact, as long as you encounter break in the loop, you will stop all the later cycles and terminate the cycle directly. So: the break in while is used to permanently terminate the loop.

The answer is revealed.

1.2.2 introduction to continue

Summary: the role of continue in the while loop is:

Continue is used to terminate this loop, that is, the code after continue in this loop is no longer executed, but jumps directly to the judgment part of the while statement. Determine the entrance of the next cycle.

The answer is revealed.

Yes, it is trapped in an endless cycle! (this is a GIF)

Skipped the following iTunes + so that I is always 5, unable to continue to jump out of the loop

(II) for Loop 2.1 syntax

Expression 1 is the initialization part that is used to initialize the loop variable.

Expression 2 is the conditional judgment part, which is used to determine the termination of the loop.

Expression 3 is the adjustment part, which is used for the adjustment of loop conditions.

2.1.1 practical problems:

Use the for loop to print 1-10 numbers on the screen.

2.2 execution process

Comparison between 2.3while and for

It can be found that there are still three necessary conditions of the cycle in the while loop, but due to the style problem, the three parts are likely to deviate far away, so the search and modification is not centralized and convenient.

As a result, the style of the for loop is better. For is also recycled most frequently.

2.4break and continue

We found that break and continue can also appear in the for loop, and their meaning is the same as in the while loop. But there are still some differences:

Let's take a look at the result of this code.

Wuhu! The cycle is finished successfully!

2.5 Tips

Loop variables cannot be modified in the for loop to prevent the for loop from losing control.

It is suggested that the value of the loop control variable of the for statement should be written as "before closing and then opening interval".

A pen examination question

It doesn't cycle once.

Because the return value of the middle k = 0 (assignment operation) is 0 means that the judgment condition is false and does not enter the loop.

(3) the syntax of the do...while () loop 3.1do statement:

3.2 execution process

The characteristics of 3.3do sentence

The loop is executed at least once, and the use of scenarios is limited, so it is not often used.

The above is all the content of the article "sample Analysis of loops in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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