Loops in C Programming

Loops are used when you need to repeat a part of the program. In C, we have loops; through which we can repeat a part of the program until a condition is satisfied. In this lesson, we will learn how to work with loops in C Programming.

Here are the loops,

  • while loop
  • do-while loop
  • for loop

while loop

The while loop in C language is used when you want to repeat a part of the program a fixed number of times. This is specified as a condition.

For example, marks of students with more than 75 in Maths.

Syntax

The following is the syntax of the while loop in C Language,

The following is an example showing the usage of the while loop in C Language,

The following is the output,

Loops in C Programming

do-while loop

The do-while is the same as while, but the do-while executes the statement first and then the condition is tested.

Syntax

The following is the syntax of using the do-while loop in C Language,

The following is an example showing the usage of the do-while loop in C Language,

The following is the output showing the usage of the do-while loop in C Language,

C Programming do-while loop

for Loop

The for loop is a loop that executes a specified number of times. Under this, initialize, test, and increment loop counter in a single line, unlike the while and do-while loop.

Syntax

The syntax of for loop shows the usage of the loop counter, its initialization, as well as condition,

The following is an example showing the usage of for loop in C language,

The output shows the value of i iterated five times with for loop,

C Programming for loop

In this lesson, we learned how to work with loops in C Language. We saw the implementation of the while loop, do-while loop, for loop, break statement, and continue statement.

Let us now learn about the break and continue statements in C with examples.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

Break Continue statements in C
Call by Value in C
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading