Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C loop


May 11, 2021 C


Table of contents


C Loop

Sometimes, you 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.

Programming languages provide a variety of control structures that allow for more complex execution paths.

Loop statements allow us to execute a statement or group of statements multiple times, as is the general form of circular statements in most programming languages:

C loop

The type of loop

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

Cycle type describe
While cycle Repeat the statement or statement group when a given condition is true.It tests before performing the circulating body.
FOR cycle Perform a statement sequence multiple times, simplify the code of managing the loop variable.
Do ... while loop In addition to it is in the test conditions of the circulatory body, other than the While statement is similar.
Nested cycle You can use one or more cycles in the While, For or Do..while loop.

Loop control statements

The loop control statement changes the normal sequence of executions. When execution leaves a range, all automatic objects created in that scope are destroyed.

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

Control statement describe
BREAK statement termination loop or switch The statement will continue to perform the next statement following Loop or Switch.
Continue statement Start the test conditions immediately in the remainder of the subject.
GOTO statement Transfer control to the tagged statement.But it is not recommended to use a goto statement in the program.

Unlimited loops

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

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

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

   return 0;
}

When a conditional expression does not exist, it is assumed to be true. Y ou can also set an initial value and an incremental expression, but in general, C programmers prefer to use for (; ; ) Structure to represent an infinite loop.

Note: You can terminate an infinite loop by pressing the Ctrl-C key.