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();
}
}
>>1
If you can't explain it, then why bother showing it you fucking retard? I mean, it's not like I can't just google the same shit and get a much better answer.
>>4
doland knuth said: 'its better to show by example then by explanation'
so there no need to explain
Name:
Anonymous2011-07-05 18:29
>>6
You sir are no knuth. By the way, if you had a real clue as to what was going on, you could actually show the real beauty of a nested for looping treating the iterators as a vector space and then showing how the various values map over this space.
Name:
Anonymous2011-07-05 18:31
>>6
And also, I bet you're some stupid shit wanna be coder that works an hourly job. Now if I had asked to explain this for loop in an interview, and you troubles explaining it, I would ended the job interview right then and there.
>>11
When you're calling cons, you're not storing the result and num-list remains unchanged. Also, the first argument to cons should be an item and the second should be a list, not the other way around, but that's not why your code doesn't work.
>>11
What are you trying to do? What >>1 asked for?
The cons is dead code, the num-list is not a lexical variable, you're also not actually changing it.
Here's how I would solve >>1:
(dotimes (i 9)
(dotimes (j (1+ i) (terpri))
(princ (1+ j))))
Or with LOOP:
(loop for i from 1 to 9 do
(loop for j from 1 to i do (princ j) finally (terpri)))
The nested loop would look better with iter, but the difference is too small to matter. I could also write a DO version, a tail-recursive version, a GO/TAGBODY version, something involving catch or conditions, but whatever, this isn't a code obfuscation contest.
this isn't a code obfuscation contest.
Now it is. ((((λ(h)(λ(u)(λ(d)((((λ(f)((λ(x)(x x))(λ(x)(f(λ(y)((x x)y))))))(λ(l)(λ(i)(λ(x)((((((λ(m)(λ(n)((λ(n)((n(λ(x)(λ(y)(λ(z)z))))(λ(x)(λ(y)x))))(((λ(m)(λ(n)((n(λ(n)(λ(f)(λ(x)(((n(λ(g)(λ(h)(h(g f)))))(λ(u)x))(λ(x)x))))))m)))m)n))))u)i)(λ()i))(λ()((λ(_)((λ (_)((l((λ(n)(λ(f)(λ(x)(f((n f)x)))))i))(((λ(x)(λ(y)(λ(k)((k x)y))))i)x)))(h)))(((λ(f)(λ(xs)(((λ(f)((λ(x)(x x))(λ(x)(f(λ(y)((x x)y))))))(λ (l)(λ (xs)(((((λ(p)(p(λ(x)(λ(y)(λ(z)(λ(w)w))))))xs)(λ()xs))(λ()((λ(_)(l((λ(p)(p(λ(x)(λ(y)y))))xs)))(f((λ(p)(p(λ(x)(λ(y)x))))xs)))))))))xs)))d)(((((λ(f)((λ(x)(x x))(λ(x)(f(λ(y)((x x)y))))))(λ(f)(λ(g)(λ(z)(λ(xs)(((((λ(p)(p(λ(x)(λ(y)(λ(z)(λ(w)w))))))xs)(λ()z))(λ()(((f g)((g((λ(p)(p(λ(x)(λ(y)x))))xs))z))((λ(p)(p(λ(x)(λ(y)y))))xs))))))))))(λ(x)(λ(y)(λ(k)((k x)y)))))(λ(x)(λ(y)(λ(z)y))))x))))))))))(λ(f)(λ(x)(f x))))(λ(x)(λ(y)(λ(z)y)))))))newline)(λ(f)(λ(x)(f(f(f(f(f(f(f(f(f(f(f x))))))))))))))(λ(x)(display((x(λ(x)(+ x 1)))0))))
public class ForLoopsAreForFags
{
public static void main(String[] args)
{
int i = -1, j;
while (i++ <= 9)
{
j = -1;
while (j++ <= i) 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.