Category Archives: C Loops

C Nested Loop

C Nested Loop

In c programming, loop within a loop is called a nested loop. In this tutorial, we will learn about nested loops in c programming with examples.

What Is Nested Loop

When there is a loop statement inside another loop statement then it is known as a nested loop statement. It is possible to have a loop statement as a part of another loop statement in c programming. There can be any number of loops inside a loop. It is not necessary that the loop must be nested inside the same type of loop. There can be any type of loop nested inside the body of another loop, such as a while loop or for a loop. For example, we can have an inner for loop inside the outer while loop.

Syntax:-

How nested loop works

For every pass of the outer loop triggers the inner loop. This repeats until the outer loop completes all iterations. For each iteration of the outer loop inner loop execute all its iteration. It is possible to have a break statement within either the inner or outer loop to interrupt this process. A total number of iterations is equal to the number of iterations of the outer loop multiplied by the number of iterations in an inner loop.

Nested for Loop

When there is for loop inside another for loop statement then it is known as a nested for loop statement. It is possible to have a for loop statement as a part of another for loop statement. Below is the general syntax of nested for loop statements in c programming:

Syntax:-

Below is a simple example to demonstrate the use of nested for loop statement in c programming. In this example, we are using a for loop inside a for a loop.

Example:-

Output:-

c-nested-for-loop-example

Nested while Loop

When there is a while loop inside another while loop statement then it is known as a nested while loop statement. It is possible to have a while loop statement as a part of another while loop statement. Below is the general syntax of nested while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested while loop statement in c programming:

Example:-

Output:-

c-nested-while-loop-example

Nested do-while Loop

When there is a do-while loop inside another do while loop statement then it is known as a nested do-while loop statement. It is possible to have a do-while loop statement as a part of another do while loop statement. Below is the general syntax of nested do…while loop statement in c programming:

Syntax:-

Below is a simple example to demonstrate the use of a nested do-while loop statement in c programming:

Example:-

Output:-

c-nested-do-while-loop-example

When To Use a Nested Loop

Nested loops are handy when you want to loop through multi-dimensional data. For example, it is easy to traverse a multi-dimensional array using nested loops.

Break In Nested loop

The break statement is used to exit out of the loop. If the break statement is used inside a nested loop, it will terminate the innermost loop.

Continue In the Nested loop

The continue statement is used to skip the current iteration and move to the next iteration. When a continue statement is encountered inside a loop, it skips all the statements below it and jumps the control to the next iteration.

C Do While Loop

C do-while Loop

In c programming, the do-while loop executes a block of statements inside the loop body first and then tests the condition. The next iteration executes only if the test condition is true.

The do-while loop is much similar to the while loop with one major difference, in the do-while loop block of statements inside the loop body executes at least once.

Why We Need a do-while loop

The main difference between a while loop and a do-while loop is that a while loop is entry-controlled loop. On the other hand, the do-while loop is an exit-controlled loop. In a while loop, if the test condition becomes false in the first iteration then the loop body is not executed at all. But the do-while loop is somewhat different from the while loop. The do-while loop executes the loop body at least once even if the test condition is false for the first iteration. The do-while loop is mainly used where we want to execute the loop at least once.

C do while Loop Flowchart

The following flowchart image illustrates the working of the do-while loop in C programming:

c-do-while-loop-flowchart-diagram

Do while Loop Syntax

Below is the general syntax of the do-while loop in c programming:

Syntax:-

Here, the loop executes a block of statements inside the loop body first and then tests the condition provided along with the while keyword for the next iteration and executes next only if the condition is true. Condition is a Boolean expression that results in either True or False, if it results in True then statements inside the loop body are executed and the condition is evaluated again. This process is repeated until the condition is evaluated as False. If the condition results in False then the loop is terminated and control is transferred to the next statement.

Do while Loop Example

Below is a simple example to demonstrate the use of a do-while loop in c programming:

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have loop body inside the pair of curly brackets {} after do keyword, statements inside loop body are executed first then control is passed to the condition ctr <= maxCtr provided along with while keyword for next iteration. If the test condition returns True, then control is passed again to the loop body for the next iteration. If the condition results in False then the loop is terminated and control is transferred to the next statement. Inside the loop body, we have incremented the ctr value by one for each iteration. When we run the above c program, we see the following output –

Output:-

c-do-while-loop

Important Difference Between a while and do-while Loop

The main difference between a while loop and a do-while loop is that a while loop is an entry-controlled loop. On the other hand, the do-while loop is an exit-controlled loop. The while loop checks the test condition before executing the loop body. While the do-while loop checks the test condition after executing the statements inside the loop body. Here, we have listed some of the important Difference Between a while and do-while Loops.

# while do-while
Syntax while ( condition) {
statements; //body of loop
}
do{
.
statements; // body of loop.
.
} while( Condition );
Condition In the ‘while’ loop the test condition appears at the start of the loop. In the ‘do-while’ loop the test condition appears at the end of the loop.
Iterations The iterations do not occur if, the test condition for the first iteration is false. The iteration occurs at least once even if the test condition is false for the first iteration.
Type Entry-controlled loop Exit-controlled loop
Semi-colon Not used Used at the end of the loop

C while Loop

C While Loop

The c while loop will execute a block of statements as long as a test expression is true. While loop is useful when the number of iterations can not be predicted beforehand. The while loop evaluates test expression at beginning of each pass.

The while loop is an entry-controlled loop. In a while loop, the test condition is evaluated before processing the loop body. If a condition is true then and only then the body of a loop is executed. Once the loop body is executed then control goes back to the beginning, and the condition is checked again if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

C While Loop Flowchart

The following flowchart image illustrates the working of the while loop in C programming:

c-while-loop-flowchart-diagram

While Loop Syntax

Below is the general syntax of while loop in c programming:

Syntax:-

Working Of While Loop

Here, while loop firstly evaluates the Condition inside parentheses (). The Condition is a Boolean expression that results in either True or False. If the condition results in True then statements inside the loop body are executed and the condition is evaluated again. This process is repeated until the condition is evaluated as False. If the condition results in False then execution is skipped from the loop body and the while loop is terminated.

After exiting the loop, the control goes to the statements immediately after the loop. The loop body can contain one or more statements. If it contains only one statement, then the curly braces are not compulsory. It is a good practice to use the curly braces even we have a single statement inside the loop body.

While Loop Example

Below is a simple example to demonstrate the use of the while loop in c programming:

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have a while loop, that checks the condition ctr <= maxCtr for each of the iterations. If the test condition returns True, the statements inside the loop body are executed and if the test condition returns False then the while loop is terminated. Inside the loop body, we have incremented the ctr value by one for each iteration. When we run the above C program, we see the following output –

Output:-

c-while-loop

Properties of while loop

  • A conditional expression is used to check the condition. The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • The condition will be true if it returns 0. The condition will be false if it returns any non-zero number.
  • In a while loop, the condition expression is compulsory.
  • Running a while loop without a body is possible.
  • We can have more than one conditional expression in the while loop.
  • If the loop body contains only one statement, then the braces are optional.

C for Loop

C for Loop

The for loop executes a block of code repeatedly until a given condition evaluates to false. The C for loop is similar to the Java and C++ for loop. The for loop is used to repeat a block of code known number of times. When we want to execute a block of code for fixed number of times, it is recommended to use for loop. The for loop takes a variable as iterator and assign it with an initial value. It iterate through the loop body as long as the test condition is true. Once the loop statements are executed for current iteration, the iterator is updated with new value. If the test condition is still valid, we loop another time. When test condition return a false value, then the for loop is ended.

For Loop flowchart

The flowchart image illustrates the working of for loop statement:

c-for-loop-flow-chart-diagram

for Loop Syntax

The general for loop syntax in c programming is as follows:

Syntax :-

Above is the general syntax of a for loop. We just initialize iterator, check condition and then increment or decrement iterator value. The for loop has three components separated by semicolons.

initialization :- In this section is used to declare and initialize the loop iterator. This is the first statement to be executed and executed only once at the beginning.

condition :- Next, the test condition is evaluated before every iteration, and if it returns a true value then the loop body is executed for current iteration. If it returns a false value then loop is terminated. Then control jumps to the next statement just after the for loop.

incr/decr :- Once, the loop body is executed for current iteration then the incr/decr statement is executed to update the iterator value and if condition is evaluated again. If it returns a true value then the loop body is executed another time. If test condition returns a false value then loop is terminated and control jumps to the next statement just after the for loop.

How for Loop Works In C

The for loop is an entry-controlled loop. In for loop, test condition is evaluated before processing the loop body. If a condition is true then and only then the body of a loop is executed. Once the loop body is executed then control goes back to the beginning, and the condition is checked again and if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

Why We Need For Loop?

  • Like any other loop “for loop” can also be used to execute the block of code over and over again.
  • The advantage of a for loop is that we know exactly how many times the loop will be executed before the loop starts.

For Loop Example In c

Below is the simple for loop example to demonstrate the use of for loop in c programming:

Example:-

Here, we have initialized ctr with initial value of 1, in the next we checks the condition ctr <= 5 for each of the iteration. If the test condition returns True, the statements inside loop body are executed and if test condition returns False then the for loop is terminated. In incr/decr section we have incremented the ctr value by one for each if the iteration. When we run the above C program, we see the following output –

Output:-

c-for-loop

Various Forms of for loop In C

Let’s understand the various possible forms of for loop in c programming. Various forms of for loop are as follows:

for loop without initialization

Initialization can be skipped from a for loop. Even though we can skip initialization part but semicolon (;) before test condition is must. If you missed semicolon (;), then you will get a compilation error.

Example:-

for loop without increment or decrement

It is also possible to skip the increment or decrement part in for loop. In this case semicolon (;) is must after test condition. In this case the increment or decrement part is done inside the loop body.

Example:-

for loop without initialization and without increment

In this for loop form, the loop variable can be initialized before the loop and can be increment or decremented inside the loop.

Example:-

Multiple Initialization In for Loop

It is also possible to initialize multiple variables in for loop.

Example:-

Multiple test conditions can be joined with logical AND (&&) and/or OR (||) operators. It has two variables in increment part. Should be separated by comma.

C Loops

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:-