C Programming Loops Explained with Examples for Beginners

0
C Programming Loops Explained with Examples for Beginners
C Programming Loops Explained with Examples for Beginners

📑 Table of Contents

Loop Definition in Programming

C Loops: Loop in C programming can be used to execute a block of code multiple times until a certain condition is meet. In other word we can say that the body continuously until a required condition is fulfill is called looping. It is used to perform looping operation. When the condition will become false the loop execution will be stopped. Loop is very important part of every programming languages like C, C++, Java, Python and more computer programming languages. I can explain every loops in this article with example simple and easy way.

Types of Loops in C Programming

  • While Loop in C
  • Do While Loop in C
  • For Loop in C 
  • Nested Loop in C 

Definition of While Loop in C

While Loop: A while loop in c programming language is used to execute a block of code as long as the specified condition is true. While loop before value initialization starting value and the given condition. If you are not follow while loop syntax your loop statement are infinite executed.

Syntax of While Loop in C

while(condition)
{
body Statements;
increment/decrement;
}
 

Explain While Loop Syntax

int n=1;
while(n<=10)
{
printf("Number is:%d\n",n);
i++;
}

Example of While Loop in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i=1;
while(i<=5)
{
printf("Your Number is:%d\n",i);
i++;
}
getch();
}

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5

Print the Table of Any Number Using While Loop

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n,i=1;
printf("Enter Any Number:");
scanf("%d",&n);
while(i<=10)
{
printf("Table is:%d X %d=%d\n",n,i,i*n);
i++;

}
getch();
}
============OUTPUT============
Enter Any number:6

Table is:6 X 1=6
Table is:6 X 2=12
Table is:6 X 3=18
Table is:6 X 4=24
Table is:6 X 5=30
Table is:6 X 6=36
Table is:6 X 7=42
Table is:6 X 8=48
Table is:6 X 9=54
Table is:6 X 10=60

Definition of Do While Loop in C

Do-While Loop: A do-while loop in c programming language is used to execute a block of code as long as the specified condition is true. The do-while loop in C is used to repeatedly execute a block of code execute at least one time and then continue executing it as long as a specified condition remains true.

Syntax of Do While Loop in C

do
{
body Statements;
increment/decrement;

while(condition);
}

 

Explain Do-While Loop Syntax

int n=1;
do
{
printf("Number is:%d\n",n);
i++;

while(i<=10);
}

Example of Do While Loop in C 

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i=1;
do
{
printf("Your Number is:%d",i);
i++;
}
while(i>=5);
getch();
}

============OUTPUT============
Your Number is:1
Note: Here condition is false but do-while loop at least one time execute.

Example of Do While Loop in C 

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int i=1;
do
{
printf("Your Number is:%d\n",i);
i++;
}
while(i<=5);
getch();
}

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5

Print the Table of Any Number Using Do While Loop

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n,i=1;
printf("Enter Any Number:");
scanf("%d",&n);
do
{
printf("Table is:%d X %d=%d\n",n,i,i*n);
i++;

}
while(i<=10);
getch();
}
============OUTPUT============
Enter Any number:6

Table is:6 X 1=6
Table is:6 X 2=12
Table is:6 X 3=18
Table is:6 X 4=24
Table is:6 X 5=30
Table is:6 X 6=36
Table is:6 X 7=42
Table is:6 X 8=48
Table is:6 X 9=54
Table is:6 X 10=60

Definition of For Loop in C Programming

For Loop: A for loop in c programming language is used to execute a block of code as long as the specified condition is true. A for loop in C language is used to execute a block of code a specific number of times. For loop there are three main part in c like initialization, condition and increment /decrement. initialization part executes only one at a time. 

Syntax of For Loop in C

for(initialization;condition;increment/decrement)
{
body Statements;
}
 

Explain For Loop Syntax

for(int i=1;i<=10;i++)
{
printf("Number is:%d\n",i);
}

Example of For Loop in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
for(int i=1;i<=5;i++)
{
printf("Your Number is:%d\n",i);
}
getch();
}

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:3
Your Number is:4
Your Number is:5

Write a Program to Print Table of a Number Using For Loop

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n;
printf("Enter Any Number:");
scanf("%d",&n);
for(int i=1;i<=10;i++)
{
printf("Table is:%d X %d=%d\n",n,i,i*n);
}
getch();
}
============OUTPUT============
Enter Any number:6

Table is:6 X 1=6
Table is:6 X 2=12
Table is:6 X 3=18
Table is:6 X 4=24
Table is:6 X 5=30
Table is:6 X 6=36
Table is:6 X 7=42
Table is:6 X 8=48
Table is:6 X 9=54
Table is:6 X 10=60

Definition of Nested Loop in C

Nested Loop: A nested loop in C programming is a loop that exists inside another loop. The inner loop is executed completely for each iteration of the outer loop. Nested loops are commonly used for iterating over multi-dimensional data structures like arrays or matrices.

Syntax of Nested Loop in C

for(initialization;condition;increment/decrement)
{
    for(initialization;condition;increment/decrement)
        {

            inner body Statements;
        }
    outer body Statements;
}
 

Nested Loop Program in C

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n,i,j;
printf("Enter Any Number:");
scanf("%d",&n);
for(i=2;i<n;i++)
{       
        int x=i,y=0;
        for(j=2;j<=x-1;j++)
        {
            if(x%j==0)
            {
                y=1;
            }
        }
        if(y==0)
        printf("%d ",x);
}

getch();
}
============OUTPUT============
Enter Any Number:20

2 3 5 7 11 13 17 19


Conclusion of Loops in C Programming Language

Loop Conclusion: A loops in a main part of every programming languages because loop concept are perform different kind operation like fetch data, print multiple information and more operations. I can explain every loop in this article if your any question or problems than you can contact me by email. I can every solution loop related in next coming soon article.

Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top