C – Loop

After reading this C loop topic, you will understand loop types and you will know the for loop flowchart, theory, and examples.

In real life, many times we need to perform some task repeated over and over, until a specific goal is reached. That’s why C language provides us a repetitive structure called loop.

C Loop that executes the same statement(s) again and again as long as the condition is true. C language supports three types of loops are: –

for loop

C programming for loop executes statements a fixed number of times. This loop is suitable when one knows already how many times the loop will be executed.

General Form (Syntax):

The syntax of for loop is given below,

for (Expression1; Expression2; Expression3)
{
statement(s);
}

where,

Expression1 (Initialization): It is the initial part of for loop. It is the first step of for loop execution and always execute once in complete for loop execution.

Expression2 (Condition evaluation): It is test expression condition of for loop. It executes statement(s) of for loop as long as expression 2 condition is true.

Expression 3 (Re-Initialization): It decides the next iteration of for loop execution. It executes each time after the for loop statement(s) executed and each time its value update & compares with a value specified in expression 2.

for loop flowchart:

The flow chart of for loop is shown below,

Example

Program (1): To print a message “how are you” five times using for loop.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i;
for(i=1;i<=5;++i)
{
printf("How are you \n");
}
getch();
}

Output (1)

How are you
How are you
How are you
How are you
How are you

In this example, Expression 1 as variable i is initialized (assigned) to value 1 and after that this value evaluated in Expression 2 i.e. i<=5. Expression 2 will result true (1 is less than or equal to 5) and hence message “How are you” will be printed for the first time. Now, control transferred to Re-initialization where the variable i value to increment by 1 and transfer the control again to Expression 2 for further evaluation and this process run for five times. When variable i value equals to 6 the for loop gets terminated and the message “How are you” will get printed for five times.

Program (1): To print Fibonacci series, the number of terms to be print enter by user.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,i,b1=0,b2=1,num;
printf("Enter number of terms to be print\n");
scanf("%d",&num);
printf("the fibbonacci series is\n");
printf("%d\n",b1);
printf("%d\n",b2);
for(i=3;i<=num;i++)
{
a=b1+b2;
b1=b2;
b2=a;
printf("%d\n",a);
}
getch();
}

Output (1)

Enter number of terms to be print
5
the fibbonacci series is
0
1
1
2
3

Program (1): To find the sum of digits of a number entered by the user.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int rem,num;
int sum=0;
printf("Enter the number :- ");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("The sum of digits of enter number is %d",sum);
getch();
}

Output (1)

Enter the number :- 348
The sum of digits of enter number is 15

Program (1): To find the sum of all numbers from 10 to 100 that are not divisible by 3 or 5 using continue statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
unsigned int i,sum=0;
for(i=10;i<=100;++i)
{
if(i%3==0||i%5==0)
{
continue;
}
sum=sum+i;
}
printf("Sum=%u",sum);
getch();
}

Output (1)

Sum=2610

Program (1): To check enter number by the user is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int digit,num,orgnl;
int reverse=0;
printf("Enter the number :- ");
scanf("%d",&num);
orgnl=num;
while(num>0)
{
digit=num%10;
reverse=reverse*10+digit;
num=num/10;
}
if(orgnl==reverse)
printf("Number %d is palindrome",orgnl);
else
printf("Number %d is not palindrome",orgnl);
getch();
}

Output (1)

Enter the number :- 254
Number 254 is not palindrome

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *