What is Loop?
Ans: Loop is a control structure which is used to repeat one task severel time.
For example we want to print our name 5 time it is easy with cin>> statement but if we want ti print name 1000 time it is very difficult to print with cin>> statement. So for this purpose we use loops.
Types of loops on C++
There are three loops in C++
Syntax of For Loop.
for(Initialization; Condition; Increment/Decrement)
{
Body of Loop.
}
Initialization: From where we want to start our loop. for example we want to print counting from 1 to 100, here we start from 1, so our initialization is 1.
Condition: We want to print counting from 1 to 100 then our condition will be that loop process his work until reach to 100.
Increment/Decrement: We want to print counting from 1 to 100. So we make increment of 1 every time we next number will be greater than previous number by 1.
Now we write a program to print counting from 1 to 100.
#include <iostream.h> //header file
int main() // main function from where program will start.
{
int i; // declare a variable i.
for(i=1; i<=1; i=i+1) // loop
{ //start loop body
cout<<i; //loop body
} // end loop body
}
in this program i=1 is initialization and i<= 100 means loop run until i will be greater than 100. when i will be greater than 100 means 101 than loop will be terminated. And i=i+1 mean every time i will increased by 1. We can write i=i+1 like this i++.
cout<<i means print value of i as we learn in previous lectures.
While Loop: Used when don't know starting and ending point we know only condition.
Ans: Loop is a control structure which is used to repeat one task severel time.
For example we want to print our name 5 time it is easy with cin>> statement but if we want ti print name 1000 time it is very difficult to print with cin>> statement. So for this purpose we use loops.
Types of loops on C++
There are three loops in C++
- For Loop.
- While Loop.
- Do-While Loop.
Syntax of For Loop.
for(Initialization; Condition; Increment/Decrement)
{
Body of Loop.
}
Initialization: From where we want to start our loop. for example we want to print counting from 1 to 100, here we start from 1, so our initialization is 1.
Condition: We want to print counting from 1 to 100 then our condition will be that loop process his work until reach to 100.
Increment/Decrement: We want to print counting from 1 to 100. So we make increment of 1 every time we next number will be greater than previous number by 1.
Now we write a program to print counting from 1 to 100.
#include <iostream.h> //header file
int main() // main function from where program will start.
{
int i; // declare a variable i.
for(i=1; i<=1; i=i+1) // loop
{ //start loop body
cout<<i; //loop body
} // end loop body
}
in this program i=1 is initialization and i<= 100 means loop run until i will be greater than 100. when i will be greater than 100 means 101 than loop will be terminated. And i=i+1 mean every time i will increased by 1. We can write i=i+1 like this i++.
cout<<i means print value of i as we learn in previous lectures.
While Loop: Used when don't know starting and ending point we know only condition.