For Loop
For Loop:
A for loop is a iterative control structure that allows you to write a loop that needs to execute a specific number of times.(OR ’n’ number of times taken as a input).
Syntax:
for(initialisation ; condition; change in value of the variable)
{
Body of the loop;
}
Example:
for(int i=1;i<=5;i++) //Starting from 1 to 5
{
cout<<“Hello\n”; //prints Hello
}
output:
Hello
Hello
Hello
Hello
Hello
Explanation:
In the above example the loop runs 5 times , ‘i’ value starting from 1 and end value 5 incrementing at the rate of 1.
Example Designs:
1)
1
12
123
1234
2)
1
21
321
4321
3)
1
212
32123
4321234
543212345
543212345
4)
1
212
32123
4321234
543212345
4321234
32123
212
1
Practicing to do designs like this in for loops can help you in the future programs to design perfect loops.
Nested For loop:
A nested for loop is simply a for loop inside an another for loop.
Note: The below given example is just an basic example.
The below example shows the nesting of only two for loops, in reality any number of for loops can be nested in any possible way until a normal human can manage them.
Example:
for(int i=1;i<=5;i++) //i loop runs 5 times
{
for(int j=1;j<=1;j++) //j loop runs 5 times
{
cout<<j; //printing j
}
cout<<endl;
}
output:
1
12
123
1234
12345
Explanation:
In the above example we see from the first for loop clearly that it runs everything inside 5 times and the second for loop i.e the j loop would run starting form 1 and to i.
Execution process:
First i is initialised to 1.
i j output comment
1 1 1 j runs from 1 to i(i.e 1);end
of j loop goes to new line.
2 1 1
2 2 12 end j again goes to new line.
3 1 1
3 2 12
3 3 123 output on screen at the end of
3 loop of i
4 1 1
4 2 12
4 3 123
4 4 1234
5 1 1
5 2 12
5 3 123
5 4 1234
5 5 12345
Q)Write A Program to Print A Design As follows:
1
212
32123
4321234
543212345
Program :
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++) //for running the loops for five times
{
for(int j=5;j>=i;j—)// for spaces
cout<<" ";
for(int h=i;h>=1;h—)//for printing first half (i to 1)
cout<<h;
for(int k=2;k<=i;k++)//for printing second half (2 to i)
cout<<k;
cout<<endl;
}
return 0;
}
Other applications of For loop:
- One of the applications is being able to print different designs as shown in the above example question.
- using for loop we can manage two or multidimensional arrays very effectively even though not the fastest way.
- for loops can be used for calculating expressions like
for example
X=(1+2)/3 +(2+3)/4………+((n-1)+n)/n+1.
which would be difficult calculating manually.
Now an another example explaining how to calculate expressions using for loop.
Program to perform
X=(1+2)/3 +(2+3)/4………+((n-1)+n)/n+1.
#include<iostream>
using namespace std;
int main()
{
int n,i,x;
cout<<"Enter the no of series required:”; //accepting from the user the
number of series required.
cin>>n;
for(i=2;i<=n+1;i++)
{
x=x+(((i-1)+i)/i);
}
cout<<"\nValue of X=“<<x<<endl; //printing the total value of x
return 0;
}
Tip:
while performing the expression calculation as shown above the calculation part would be easy if we write summation form of the expression
Comments
Post a Comment