C language circulation and simple code example _c language

Source: Internet
Author: User
Tags goto

C Loop

Sometimes, we may need to execute the same piece of code multiple times. In general, statements are executed sequentially: The first statement in a function executes first, then the second statement, and so on.

The programming language provides a variety of control structures for more complex execution paths.

The loop statement allows us to execute a statement or group of statements multiple times, and the following is a flowchart for looping statements in most programming languages:

Loop type

The C language provides the following types of loops. Click on the link to see the details for each type.

Loop Type Description
While loop A repeating statement or group of statements when the given condition is true. It tests the condition before executing the loop body.
For loop Executes a sequence of statements multiple times, simplifying the code that manages the loop variable.
Do...while Cycle Except that it tests the condition at the end of the loop body, the other is similar to the while statement.
Nested loops You can have a while, for, or do ... One or more loops are used within the while loop.

Loop Control statement

The loop control statement changes the order in which your code is executed. Through it you can implement the code jump.

C provides the following loop control statements. Click on the link to see the details of each statement.

Control Statements Description
Break statement Terminates a loop or a switch statement, and the program continues to execute the next statement immediately following the loop or switch.
Continue statement Tell a loop body to stop this iteration of the loop immediately, and start the next iteration of the loop again.
Goto statement Transfers control to the marked statement. However, it is not recommended to use the GOTO statement in your program.

Infinite loop

If the condition is never false, the loop becomes an infinite loop. For loops are used in the traditional sense to implement an infinite loop. Because none of the three expressions that make up a loop is required, you can leave some conditional expression blank to form an infinite loop.

#include <stdio.h>
 
int main ()
{for

  (;;)
  {
   printf ("This loop would run forever.\n");
  }

  return 0;
}

When a conditional expression does not exist, it is assumed to be true. You can also set an initial value and an increment expression, but in general, C programmers tend to use a for (;;) structure to represent an infinite loop.

Note: you can press Ctrl + C to terminate an infinite loop.

The above is the C language cycle of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.