Loops


Loops in C#

Looping is described as repeatedly repeating the same procedure until a certain condition is met. In the C programming language. There are three types of loops in the C programming language, and we will cover all elements of C loops in this section of the course.

In most cases, statements are executed in order: the first statement in a function is executed first, followed by the second, and so on.

Different control structures are available in programming languages, allowing for more complex execution pathways.

The general form of a loop statement in most programming languages is shown below.

Image Not found

To address looping needs, the C programming language includes the following types of loops.

Sr.No. Loop Type & Description
1 while loop
While a given condition is true, it repeats a statement or a set of assertions. Before performing the loop body, it checks everything in the condition.
2 for loop
The code that controls the loop variable is abbreviated and executes a sequence of instructions several times.
3 do...while loop
It's similar to a while statement, with the exception that it checks the condition at the conclusion of the loop body.
4 nested loops
Inside any other while, for, or do..while loop, you can utilise one or more loops.

Loop Control Statements

Control statements in loops alter the execution sequence. All automated objects generated in a scope are deleted when execution exits that scope.

The following control statements are supported by C.

s.no Teacher_Name
1 break statement
The loop or switch statement is terminated, and execution is transferred to the statement immediately after the loop or switch.
2 continue statement
This causes the loop to skip the rest of its body and retest its state before repeating.
3 goto statement
Control is passed to the labelled statement.

The Infinite Loop

If a condition never becomes false, a loop becomes endless. For this, the for loop is typically employed. You may construct an eternal loop by leaving the conditional expression empty since none of the three expressions that make up the 'for' loop are necessary.

#include <stdio.h> int main () { for( ; ; ) { printf("This loop will run forever.\n"); } return 0; }

It is presumed that the conditional statement is true when it is not present. Although you may have an initialization and increment expression in C, the for(;;) construct is more often used to represent an endless loop.

Types of C Loops

  • There are three types of loops:
  • In C, do while while is used to create a do-while loop.
  • The do-while loop runs until a specified condition is met. It's also known as a post-test loop. It is used when the loop must be executed at least once (mostly menu driven programs).
  • The syntax of a do-while loop in C is as follows:
  • while(condition); do /code to be performed

while loop in C

In the case when we don't know the number of iterations ahead of time, we can utilise the while loop in C. The while loop executes the block of statements until the condition provided in the while loop is fulfilled. A pre-tested loop is another name for it.

in C for loop

The for loop is utilised when we need to execute a section of code repeatedly until the provided condition is met. A per-tested loop is another name for the for loop. If the number of iterations is known ahead of time, the for loop is preferable.

The syntax of the for loop in C is as follows:

for(initialization;condition;incr/decr){ //code to be executed }