Skip to main content

for loop in C language

Loops in C language:

What are loops?

Loops are used for performing a task again and again in other words loops are used for checking number of iterations performed.
There are 3 main types of loops in c language.
  • for loop
  • while loop
  • do while loop

We are going to do for loop in this article with some couple of problems.

For executing a loop there are mostly three main steps
  • Initialization
  • Condition
  • Increment or Decrement operator.

1. Initialization:

It provides the starting value to the variable to start the loop or the value from where the loop should be started.

2. Condition:

It is used for checking the condition if the condition is true than the process is executed until the condition becomes false.

3.Increment or Decrement Operator:

It is used for incrementing or decrementing the value variable each time after the loop is executed
(i++) -  Increases the value of variable by 1.
(i+=1) -  Increases the value of variable by 1.
(i=i+1) -  Increases the value of variable by 1.
(i=i+2) -  Increases the value of variable by 2.
(i--) -  Decreases the value of variable by 1.
(i-=1) -  Decreases the value of variable by 1.
(i=i-1) -  Decreases the value of variable by 1.
(i=i-2) -  Decreases the value of variable by 2.

General Process Flow:

The loop first intialize then checks the condition after the condition is true it executes the code written in it and then goes for incrementing or decrementing the value of  variable.

for Loop:

Syntax:

for(initialization ; condition ; increment)
{
code to be executed if the condition is true;
}

Note:

  • There will be no semicolon after round brackets.
  • The code for the for loop is written in curly brackets.
  • There should always be semicolon between initialization, condition and the operator. 

Example Code 1:

Write a program to print even numbers by accepting number of terms from user.
Ans.
#include<stdio.h>
#include<conio.h>>
void main()
{
int n,i;
clrscr();
printf("\n enter the number of terms");
scanf("%d",&n);
printf("\n the even numbers are");
for(i=2 ; i<=n*2 ; i=i+2)
{
printf("\n %d",i);
}
getch();
}

The above codes is written in code blocks
Output:
enter the number of terms
5
the even numbers are
2
4
6
8
10
Explanation of the above code:
In the above code we have declared two integer variables n and i
n- stores the value of number of terms
i- used for executing the loop
As per the syntax of for loop we intialised the for loop from i=2 because as the first even number is 2.
For condition we have multiplied the value of n by 2 because we need to print 5 even numbers starting from 2 so they are 2,4,6,8,10 so we need the condition i<=10 thats the reason.
In case of incrementing we have incremented the value of i by 2 because we need to add 2 after every even number for getting the next even number. So after adding 2 to i whose value is 2 we get 4.
This goes on and on till the condition is false that is the value of i is greater than 10. After reaching 10 the next value of i after incrementing becomes 12 now when the loop checks the condition as it is false the loop stops executing and i comes out of the loop with a final value 12.

Example Code 2:

Write a program to print table of number entered by user.
Ans.
#include<stdio.h>
#include<conio.h>>
void main()
{
int n,i,r;
clrscr();
printf("\n enter the number");
scanf("%d",&n);
printf("\n the table is");
for(i=1 ; i<=10 ; i++)
{
r=n*i;
printf("\n %d * %d = %d",n,i,r);
}
getch();
}
Output:
enter the number
5
the table is
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Explanation of the above code:

In the above code we can see that the loop is initiated from 1 and the condition is i should be less than or equal to 10 also we are incrementing the loop by 1 so it is clear that the loop is going to be executed 10 times starting from i=1 till i=10 and we are calculating the result of multiplication by multiplying n by i and storing the result in r at the same time we print the statement for the table as the value of r changes every time after each iteration.

So for every value of i there is a different value of r and the loop keeps on printing till the condition fails. So when the value of i is greater than 10 it stops printing the table as the condition fails.

If you want to study the further topic of while and do while loop you can click on the link given below

https://www.codingninjaa.com/2020/05/while-loop-in-c.html

If you want to study the previous topic of nested if statement you can click on the link given below

https://www.codingninjaa.com/2020/05/nested-if-statements-in-c-programming.html

If you have any doubts please comment down below.

Comments

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS:
1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases.
1.1 Header Files:

Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h".
Example: #include<stdio.h> ↓                   ⇡
         Tells computer to                  header file with 
         include the header                 extension .h
         file          
There are many header files for different functions such as the following: <conio.h><math.h><string.h>Etc.

1.2 Starting of code and basic Syntax:

void main() or int main()

This tells the computer from where the code is starting. It is wri…

How to start programming for beginners

HOW TO START PROGRAMMING:
1. What is Programming?
Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs.
Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program.Programming can also be used to build a software to determine whether a particular number is prime or not.2. Can anyone learn Programming ?The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming. The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming?If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now.The other important Factor is Money if you become a master in programming then there is no limit on how…

Basic C Programming Questions

Basic Coding Questions:Exercise 1:Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); }

The above image code is written in code blocks
Output:
enter the length and breadth of rectangle 20(press enter) 10(press enter) the area of rectangle is 200 and the perimeter is 60


Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers.Taking input of length and breadth together by using scanf function.Substituting the values of length and breadth stored in l and b to find out area and perimeter.Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans: