×

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

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.