By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,567 Members | 1,651 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,567 IT Pros & Developers. It's quick & easy.

Simple code, but cannot understand

P: n/a
using System

namespace ConsoleApplication1

public class Class

public static void Main(

for (int i = 1; i < 10; i++

for (int j = 1; j < 10; j++

for (int k = 1; k < 10; k++

Console.WriteLine(i+" "+j+" "+k)


the running result is
6 3
6 3
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 6
6 6
6 6
6 6
6 6
......

but i think the result should be like
1 1
1 1
1 1
.......

Really confuse about it, thank you so much for help!
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You got 3 loops, and the innermost (k) one will run every time, the midle
one (j) will run each time the innermost one is finished, and the outer
one (i) will run every time the middle one is finished

so the result will be

i, j, k
i, j, k+1
i, j, k+2
....
i, j+1, k
i, j+1, k+1
i, j+1, k+2
....
i, j+2, k
i, j+2, k+1
i, j+2, k+2
....
....
....
....
....
....
i+1, j, k
i+1, j, k+1
i+1, j, k+2
....
i+1, j+1, k
i+1, j+1, k+1
i+1, j+1, k+2
....
i+1, j+2, k
i+1, j+2, k+1
i+1, j+2, k+2
....
....
....
i+2, j, k
i+2, j, k+1
i+2, j, k+2

and so on.

The result will indeed be
1 1 1
1 1 2
and so on, but it will write so fast you won't see the first results
--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #2

P: n/a
Your correct, the output is
1 1
....
9 9

Your problem is your screen buffer size for height is to small. Try setting it to something like 800 to 1000
----- Lionheart wrote: ----

using System

namespace ConsoleApplication1

public class Class

public static void Main(

for (int i = 1; i < 10; i++

for (int j = 1; j < 10; j++

for (int k = 1; k < 10; k++

Console.WriteLine(i+" "+j+" "+k)


the running result is
6 3
6 3
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 6
6 6
6 6
6 6
6 6
.....

but i think the result should be like
1 1
1 1
1 1
......

Really confuse about it, thank you so much for help!
Nov 15 '05 #3

P: n/a

Hello, Lionheart!
for (int i = 1; i < 10; i++)
{ .... 6 4 3
6 4 4 ....
but i think the result should be like:
1 1 1
1 1 2
1 1 3
.......

Really confuse about it, thank you so much for help!


The result have more than 900 rows! You must configure your console buffer
to height near 1000 or more :-)
--
Roman S. Golubin
ICQ UIN 63253392
go*****************@arhcity.ru
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.