Loops in C Programming

Loops in C Programming

Loops are used when you need to repeat a part of program. In C, we have loops; through which we can repeat a part of 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

The while loop in C language is used when you want to repeat a part of program for 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 while loop in C Language,

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

The following is the output,

Loops in C Programming

do-while loop

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

Syntax

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

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

The following is the output showing the usage of 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 while and do-while loop.

Syntax

The syntax of for loop shows the usage of loop counter, it’s 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

break Statement

Use break statement, if you want to jump out of a loop. The control passes to the first statement after the loop, since break statement terminates the loop.

Example

The following is an example showing the usage of break statement in C language,

The following is the output showing the usage of break statement in C Language,

C Programming break statement

continue statement

Use continue statement, if you want to pass control to the next iteration of the loop. However, it skips any code in between.

The syntax for continue statement in C language,

The following is an example showing the usage of continue statement,

The following is the output,

C Programming continue statement

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

Decision Making Statements in C
Arrays in C Programming
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create free tutorials for all.

No Comments

Post A Comment