×

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a block of code multiple times and it reduces the length of the code. Example:  Let suppose we have to print counting 1to 50 then we have to use 50 times lines of code but if we use loop then print it by 2 or 5 lines of code.
  • for loop
  • while loop
  • do while loop
Difference between conditional and looping statement: The conditional statement executes only once in the program but looping statements executes repeatedly several number of times. for Loop The for loop statement is used to execute the code until a condition is false. In this section, there are 3 parts initialization, condition and increment or Decrements. 

   

The following syntax of for Loop:
for(initialization;condition;incr/decr)
{
//code to be executed
}
 Example1
#include<stdio.h>
int main(){
int i;
 for(i=0;i<=5;i++){
 printf("%d \n", i);
 } 
 return 0;
}
Output
0 
1 
2 
3 
4 
5
Example 2 //To print any childish table of any number
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,temp,i;
printf("Enter any number for Table :");
scanf("%d",&n);
for(i=1;i<=10;i++){
 temp=n*i;
printf("%d * %d = %d \n",n,i,temp);
}
return 0;
}
Output
Enter any number for Table :32764 * 1 = 32764 
32764 * 2 = 65528 
32764 * 3 = 98292 
32764 * 4 = 131056 
32764 * 5 = 163820 
32764 * 6 = 196584 
32764 * 7 = 229348 
32764 * 8 = 262112 
32764 * 9 = 294876 
32764 * 10 = 327640
While Loop
While loop is an entry-controlled loop statement. The basic format of the while statement is:

Syntax:
while( test condition{
body of loop;
}
Or
Assignment;
while(condition)
{
Statements;
......
Increment/decrements (++ or --);
}
    

Note: While loop is used when the number is uncertain or unknown.

Example:
#include<stdio.h>
int main(){
printf("To Print Counting by using While Loop \n");
int i=1;
while(i<5){
printf("\n%d",i);
i++;
}
 return 0;
}
Output
To Print Counting by using While Loop 

1
2
3
4
Do while Loop
Do while loop is used to execute the program. In do while loop, the first part of the execution is body then evaluate the text condition. If the condition is true, the body of the loop is executed again and again until the condition becomes false.

Syntax:
do{
Statement(s);
}while(condition);
Let us consider the flow chart of the C program 

   

Let us consider an example:
#include<stdio.h>
int main(){
 int a=1;
 do{
 printf("Couning: %d\n",a);
  a=a+1;
 }while(a<5);
return 0;
}
Output
Couning: 1
Couning: 2
Couning: 3
Couning: 4

Related Topics

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

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.

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.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.