×

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language. The Iterations can be able to modify the control flow of the program thus they referred as “Control Statements.”

Loops:

The Continuous repetition of statements until a condition is satisfies are called loops.

The loops consist of initialization, condition and updating. In initialization, a variable is declared a variable with a value. In Condition, a specific condition is provided so that the repetition of the code is controlled to specific number of times. In Updating, the value which is initialized earlier is being increased/ decreased according to the condition.

The Loops are classified into two types:

  1. Pre-test Loop.
  2. Post-Test Loop.

Pre-Test Loop:

In the Pre-Test Loop, the checking of the condition whether its true or false is done at the start of loop and then enters the loop body.

The Pre-Test Loops are:

While Loop:

In while loop, the condition is checked first and allows the flow of execution into the loop body if the condition is true. The Initialization of the variable in while loop id done before creating a loop body. The Condition is mentioned after the “while” keyword in the Parenthesis i.e., (). The Updating is done inside Loop block.

Syntax:

while(condition)


{
	//Loop statements and Updating(Increasing/ Decreasing)
}

Example:

//Program for while loop
#include<stdio.h>
#include<conio.h>
void main (void)
{
	int n=3 , i=0; // initialization
	while(i<n) //conditon
	{
		printf(“Loop Executed successfully\n”);
		i=i+1; // Updating
}


}

Output:

Loop Executed Successfully
Loop Executed Successfully
Loop Executed Successfully

For Loop:

The For loop is ideal among all the loops as, the initialization, condition and updating can be done in a single line.

Syntax:

for(initialization; condition ; updating )
{
	// statements
}

Example:

// Program for “for Loop”
#include<stdio.h>
#include<conio.h>
void main (void)
{
	int i, n =3;
	for(i=0;i<n;i++)
	{
		printf(“For Loop Successfully Executed\n”);
}
}

Output:

For Loop Successfully Executed
For Loop Successfully Executed
For Loop Successfully Executed

Post-Test Loop:

In the post test loop, the loop condition is checked after execution of block of code.

The Pre-test loops are:

Do While Loop:

The Do-while loop is same as while loop but the block of code is executed at least once before checking the loop condition. The initialization is done outside the loop body. The condition is written after the block of code. The Increment/ Decrement (updating) is done inside the block.

Syntax:

do
{
//statements
} while(Condition);

Example:

// Program for do-while loop
#include<stdio.h>
#include<conio.h>
void main (void)
{
	int i , n=3; // initialization 
	do
	{
		printf(“do-while loop successfully executed\n”);
		i++; // updation
}  while(i<3); // conditon
}

Output:

do-while loop successfully executed
do-while loop successfully executed
do-while loop successfully executed

Summary:

The iterations in c language are the statements which are executed multiple number of times until a condition is reached which is referred as loops. These loops are of two types of Pre-test and Post-test loops. The Pre-test loops are “For Loop” and “While Loop”. The Post-Test Loops are “Do-while Loop”.


Related Topics

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.