For Loop

Pages: 12
Can someone help me to practice "for" loop. give me link plz.
try search in in forum

example for u :

1
2
3
4
5
6
7

int x[10];

for ( int i = 0 ; i  < 10 ; i ++)
{
     x[i]=i+1;
}


just try with your own code and see what is the output for the array
closed account (28poGNh0)
Try to imagine you need to write this line "This is all bla bla bla bla bla ",
10 times ,I know that's fun !.What if you need to write it 1000 times ,oh now the boring stuff,
well that is one of the reason of creation for/while/do while loops
so we can write 1000 lines of "This is all bla bla bla bla bla " using for loop just in second or 2 or 3 or... at this way

1
2
3
4
5
6
7
8
9
10
# include <iostream>
using namespace std;

int main()
{
	for(int i=0;i<1000;i++)
	      cout << "Line " << i+1 << " : This is all bla bla bla bla bla " << endl;

	return 0;
}


We can also draw a Triangle using two for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include <iostream>
using namespace std;

int main()
{
	// This is a program to draw triangle only with two for loop

	for(int i=0;i<7;i++)// I need 7 lines
	{
		for(int j=0;j<i;j++)// Write "*" j times
			cout << "*" ;
		cout << endl;
	}
	
}


also fun stuff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# include <iostream>
# include <ctime>
using namespace std;

int main()
{
	// This one countdown
	double startTime = 0;
	for(int i=10;i>=1;i--)
	{
		startTime = clock();
		cout << i << endl;
		for(;(clock()-startTime)!=1000;);
	}cout << "Fire !"  << endl;

	return 0;		
}


Or to initialise an array like BasicNewbie did

All you need to know is : how many times you want to do something

Have a nice day
Last edited on
In the First (This is bla bla bla ....) , Why did you use i+1 in the line 7. Is it count 0-1000?
he is telling you the line[i] is keep increasing.
1
2
3
4
5
Line 1 : This is all bla bla bla bla bla
Line 2 : This is all bla bla bla bla bla
Line 3 : This is all bla bla bla bla bla
.... 
Line 1000 : This is all bla bla bla bla bla


can you put some output instead of you keep asking for "is it count 0-1000"?

can you try it out first?
I feel that a lot thread that you posted , you haven't try it out and keep asking here only.. can do some experiment on it?
Try it bro..

As I also basic here , I also keep learning & try it out..
why don't you just do it ? keep it up.
Because still I can't take an idea about loops. and I can't adjust them. still trying
closed account (28poGNh0)
Well thank you BasicNewbie for the explanation

and for you prabhanuka once again

All you need to know is : how many times you want to do something

Try the exemples above,then try to undestand theme, then we'll show you couple of other uses of for;

for expemle can try to write a program for as that shows only the pair numbers from 0 to 30 ,the only thing you gonna use is for loop ,post your program so we can see It

Bon courage
pair numbers means? ex : 20 / 25 / 23 or even numbers?
For Even Numbers,Below. If it needs any modification please tell me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    for (int x = 0; x <= 30; x++)
    {
        if (x % 2 == 0)
        {
              cout << x << "\n";
         }
     }
        return 0;
}

yes . you are right .

the program will print for the even number . I saw some of your thread .

so what the question you want to ask?
I was able to do a program like below, It prints squires.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <iostream>
using namespace std;

int main()
{
    int rows, colms;
    char key;
    
    cout << "How many Rows = ";
    cin >> rows;
    cout << "How many Columns = ";
    cin >> colms;
    cout << "What is the character to print";
    cin >> key;
    
    for (int x = 0; x < rows; x++)
    {
        for (int y = 0; y < colms; y++)
        {
            cout << key;
            }
            cout << "\n";
        }
        return 0;
    }



I need to print Triangles not like your example. its like this,

x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
xxxxxxxxxxx


Can you give me an idea?
1
2
3
4
5
6
7
8
9
10
11
12
13
char a ='*';
	for ( int a = 1; a <=6; a++)
	{
		for (int b = 0; b < 9-a; b++ )
		{
			cout<<(" "); 
		}
		for (int b = 0; b < 2*a - 1; b++ )

		cout<<'*';

		cout<<( "\n");
	} 


already give you the concept , try to do it yourself.
There is a small problem, the output is like, I tried but I cannot understand where can I put a line break.

        *       ***      *****     *******    *********   ***********


By the way, What do you use to write c++ programs. Dev C++/Visual C++ /?
Last edited on
I got the point. I missed a curl bracket.

I'm little bit difficult to understand 2nd and 3rd for loops. Can you describe it as a favor. Cos I'm not a fluent in C++.

Thanks
visual c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<conio.h>
using namespace std;

void main()
{
	char a ='*';
	for ( int a = 1; a <=6; a++)
	{
		for (int b = 0; b < 9-a; b++ )
		{
			cout<<(" "); 
		}
		for (int b = 0; b < 2*a - 1; b++ )

		cout<<'*';

		cout<<( "\n");
	} 
		getch();
}


second loop is for the space which is " "
You try to decrease it and you will see what is the function are

for the 3rd loop.
it's to print the "*" .
actually you can delete the char a="*";
my fault. no need to declare that char variable
Thanks. Everything is fine. I'm using DevC++. In my software I tried to prevent closing but I can not. Earlier I used system ("pause"). but someone in this forum told me to not to use it. I checked the forum and read some pages those codes not working in my application. Do you have a different way? Your software perfectly working on Windows 7? What is the version?
im using microsoft visual studio 2010.

1
2
3
4
#include<conio.h>


getch()
add tis ,
In my software I tried to prevent closing but I can not.
Execute from a console.
I checked the forum and read some pages those codes not working in my application
I cannot work with that, you need to be more specific. Read the sticky.
I wanted to create output like below.
x
xx
xxx
xxxx
xxxxx


using following code I have done it. but now I want to change output like below
xxxxx
xxxx
xxx
xx
x
what is the change I have to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    for (int x=0; x < 5; x++)
    {
        for (int y=0; y <= x ; y++)
        {
            cout << 'x';
            }
            cout << "\n";
        }

    return 0;
}
what is the change I have to do?
If this is still an issue (you marked it as solved):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    for (int x=0; x < 5; x++)
    {
        for (int y=x; y > 0 ; y--) // Note
        {
            cout << 'x';
            }
            cout << "\n";
        }

    return 0;
}
Pages: 12