C Loops

In this tutorial you will learn about the C Loops and its application with practical example.

C Loops

In c programming loops are a fundamental concept. Loop statements allow a set of instructions to be executed repeatedly until a certain condition is fulfilled. This way loops are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. Loops are also known as iterating statements or looping statements. For example, if you want to print a message 100 times, then rather than typing the same code 100 times, you can use a loop.

Need Of Loops In C

While programming there comes a situation where we want to execute a block of code several numbers of times. In general, we will need to write the same block of code multiple times in the program. These statements will be executed in a sequential manner the first block of code executes first, followed by the second, and so on. That would take up a lot of effort which is just copy-pasting the same block of code required a number of times. This makes the process very complicated as well as lengthy and time-consuming. That’s where the loops come into play. With loops, we can group a set of instructions that we needed to be executed repeatedly. Then the loop statement does the remaining job.

Components of a loop

Generally, a loop statement has the following four components:

  • Initialization
  • Test Expression(Condition)
  • Increment/Decrement
  • Loop Body

Each of the loop components has a different purpose associated with it. We will discuss each of the above loop components for a better understanding of the working of the loops.

Initialization:- Before we enter into a loop, we must initialize its control variable. In the initialization part, the control variable is initialized with its the first value. The initialization expression is executed only once at the beginning of the loop. Here, we can initialize the variable, or we can use an already initialized variable.

Test Expression:- Test expression is executed with each iteration of the loop. The value of the test expression is used to decide whether the loop body will be executed or not. If it returns a true value then the loop body is executed for the current iteration. If it returns a false value then the loop is terminated. It must return a boolean value either true or false.

Increment/Decrement:- The Increment/Decrement expression is executed at the end of the loop. It increments or decrements the control variable value.

Loop Body:-The loop body contains the block of code to be executed repeatedly (as long as the test expression is true). The value of the test expression decides whether the code inside the loop body will be executed or not. If the value of the test expression evaluates to be true then the loop body is executed, otherwise, the loop is terminated.

Types of Loops In C

In this tutorial, we will learn about the various types of loops in c programming. While all loop types provide the same basic functionality, they differ in their syntax and condition checking time. In C programming we have the following three loop types:

 

c-types-of-loops

C for loop statement

The for loop is an entry-controlled loop that allows executing a block of code repeatedly with a fixed number of times on the basis of the test expression. The for loop is used when we want to execute a block of code known times.

Syntax:-

C while loop Statement

The while loop is an entry-controlled loop. While loop will execute a block of statements as long as a test expression or test condition is true. When the expression evaluates to false, the control will move to the next line just after the end of the loop body. In a while loop, the loop variable must be initialized before the loop begins. And the loop variable should be updated inside the while loop’s body.

Syntax:-

C do while loop Statement

The do-while loop is an exit-controlled loop. Unlike the for loop and while loop, the do-while loop evaluates its test expression after executing the statements inside the loop body. The do-while loop will first execute a block of code and then test the condition for the next iteration. It will execute the next iteration only if the condition is true. This means in the do-while loop the block of code will always execute at least once.

Syntax:-

In this tutorial we have learn about the C Loops and its application with practical example. I hope you will like this tutorial.