Hi,
Here is a collection of few examples for NESTED FOR LOOP.
Simple codes in how to use nested loops,
There is only the code, no explanation (not good at explaining anything)
So, just try to understand the program
Hope this will help as much people as possible
Let's Stop talking and just begin
OK
Here we go!
1.Get the following output :
a.
1
12
123
1234
12345
123456
1234567
12345678
123456789
//Numbers Ladder
//Nested Loops
//By ALPHA
//On 22 Oct 2008
//www.CodeCall.net
//----------------------
public class testFor
{
public static void main(String [] args)
{
for (int i=1; i<=9; i++)
{
System.out.println();
for (int j=1; j<=i; j++)
{
System.out.print(j);
}
}
System.out.println();
}
}
No use of the goes-to operator? Shame on you, /prog/!
#include <cstdio>
#define MAX 10
int main(int argc, const char * argv[]) {
int i = MAX;
int j;
while (i --> 1) {
j = MAX;
while (j --> i)
printf("%i", MAX - j);
printf("\n");
}
return 0;
}
Name:
Anonymous2011-07-07 17:45
>>41
This operator is superior. #include <cstdio>
#define MAX 10
int main(int argc, const char * argv[]) {
int i = 1;
int j;
while (i ++< MAX) {
j = 1;
while (j ++< i)
printf("%i", j-1);
printf("\n");
}
return 0;
}
Name:
Anonymous2011-07-07 17:51
print '\n'.join(''.join(map(str, xrange(1, i+1))) for i in xrange(1,10))
Ironic how the FIOC version has no indentation. Problem Lispfags?
>>50
I think that's a good thing. This code isn't typical (or even correct, it works but >> and side-effects are non-deterministic) but there's been a lot of Haskell influence on the Perl 6 implementers and probably a good amount of APL influence on Larry Wall.
>>40
I forgot to change a java statement when I rewrote it in C# (They're pretty much the same language):
namespace ForLoopsAreStillForFags{
class Program
{
static void Main(string[] args)
{
int i = -1, j;
while (i++ <= 9)
{
j = -1;
while (j++ <= i) Console.Write(j);
Console.WriteLine();
}
}
}
}
If you must have for loops:
namespace ForLoopsAreStillForFags{
class Program
{
static void Main(string[] args)
{
for(int i = 0; i <= 9; i++)
{
for(int j = 0; j <= 9; j++)
Console.Write(j);
Console.WriteLine();
}
}
}
}
I wish Java had closures and attributes and I wish C# had anonymous/inner classes.
In general though I wish a large corporation would back a streamlined, simple, extensible language. The closest things are Blizzard's support of Lua and Google's of Python.
function TNumberGenEnumerator.GetCurrent: longint;
begin
result := fCurrent;
end;
function TNumberGenEnumerator.MoveNext: Boolean;
begin
inc(fCurrent);
result := fCurrent <= fNumbers;
end;
function TNumberGen.GetEnumerator: TNumberGenEnumerator;
begin
result := TNumberGenEnumerator.Create(fNumbers);
end;
constructor TNumberGen.Create(ANumbers: longint);
begin
inherited Create;
fNumbers := ANumbers;
end;
function TNumberGeneratorFactory.GetInstance(ANumbers: longint): INumberGen;
begin
result := TNumberGen.Create(ANumbers);
end;
procedure WriteNumbers;
var i, num: longint;
begin
for i := 1 to 9 do
begin
for num in INumberGenFactory(TNumberGeneratorFactory.Create).GetInstance(i) do
write(num);
writeln;
end;
end;
>>54
That's not fair. Array processing in APL is mad skills and that influence shows in this example. I'd argue Perl 6 doesn't quite have enough influence in that regard.