Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Nested For Loop [EXAMPLES!]

Name: ALPHA 2011-07-05 13:38

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();
    }
}

Name: Anonymous 2011-07-05 14:10

your fucking retarded

Name: Anonymous 2011-07-05 17:21

>>2
fucking retarded whom?

Name: Anonymous 2011-07-05 18:03

>>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.

Name: Anonymous 2011-07-05 18:04

>>3
I think >>2 was referring to the dumbshit OP.

Name: ALPHA 2011-07-05 18:25

>>4
doland knuth said: 'its better to show by example then by explanation'

so there no need to explain

Name: Anonymous 2011-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: Anonymous 2011-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.

Name: Anonymous 2011-07-05 21:13

>>8
what the heaven is an ``hourly job''?

Name: Anonymous 2011-07-05 21:15

mapM_ putStrLn [ take n ['1'..] | n <- [1..9] ]

Name: Anonymous 2011-07-05 21:47

Ok, I have stumped my stupid noob self.  Why doesn't this work?

(defun test-for ()
  (setq num-list ())
  (dotimes (i 9)
    (cons num-list i)
    (print num-list)))

Name: Anonymous 2011-07-05 21:53

>>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.

Name: >>10 2011-07-05 21:57

This one's a little shorter:
mapM_ (putStrLn . (`take` ['1'..])) [1..9]

Name: Anonymous 2011-07-05 22:00

>>12
I also tried append with (list i) but I was too dumb to make that work as well :'(

Somebody write a correct version in CL, please!

Name: Anonymous 2011-07-05 22:00

>>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.

Name: Anonymous 2011-07-05 23:12

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))))

Name: Anonymous 2011-07-06 5:25

>>16
Still easier to read than machine code.

Name: Anonymous 2011-07-06 9:55

>>17
No it's not

; assemble with:
;   nasm -f elf64 -o prog.o prog.asm
;
; link with:
;   gcc prog.o
;
; run with:
;   ./a.out

; Purpose: nested for-loop
; author:  anonymous
; licence: false

; TODO: optimize shit, it's slower than C now ;/

extern printf
extern putchar

section .data
    format_string: db "%u", 0h

section .text
global main

main:
    push rbp
    mov rbp, rsp
    xor rdi, rdi
    push r10
    push rbx
    xor r10, r10

    ; initialize loop
    xor bx, bx
    first_loop:
        inc bx
        xor r10w, r10w ;second loop's counter
        second_loop:
            inc r10w
            ; print number
            xor rsi, rsi
            mov si, r10w
            mov rdi, format_string
            call printf
            cmp r10w, bx ; second loop end
            jl second_loop
        mov edi, 0ah
        call putchar
        cmp bx, 9h ; first loop end
        jl first_loop

    pop rbx
    pop r10
    ; return success
    leave
    xor rax, rax
    ret


simple as hell

Name: Anonymous 2011-07-06 10:32

>>; TODO: optimize shit, it's slower than C now ;/
GCC unrolls simple loops like that

Name: Anonymous 2011-07-06 11:20

>>18
I don't know how to feel regarding your understanding of ``machine code'' and that indentation style.

Name: Anonymous 2011-07-06 11:29

>>18
That's incredibly readable compared to any Lisp code.

Name: Anonymous 2011-07-06 13:21

<-- I got dubz

Name: Anonymous 2011-07-06 13:29

>>21
wrong.

Name: ALPHA 2011-07-06 13:40

Could we please get back on the Topic?
Which is code Criticism.

Name: Anonymous 2011-07-06 13:42

>>22
Nice dubz.

Name: Anonymous 2011-07-06 13:53

>>24
I thought the topic was to optimize that crappy assembly code.

Name: Alpha Mail 2011-07-06 14:25

lol

Name: Anonymous 2011-07-06 14:45

>>24
Which is code Criticism.
Your code sucks. How is that?

Name: ALPHA 2011-07-06 15:06

>>26
Please make your own Thread.
>>28
That's not very constructive. Please tell me what's wrong.

Name: LAMBDA 2011-07-06 15:19

>>29
Please capitalize Properly Your senteces.

Name: Anonymous 2011-07-06 15:29

//
// Triangle.cpp
//

#include <iostream>
#include <string>
#include <sstream>

class Printer {
private:
    std::string str;
public:
    Printer(std::string str);
    ~Printer();
    void setString(std::string str);
    void print();
};

Printer::Printer(std::string str = "") {
    // set the string
    this->str = str;
}

Printer::~Printer() {
}

void Printer::setString(std::string str) {
    // set the string
    this->str = str;
}

void Printer::print() {
    // print the string
    std::cout << this->str;
}

class PrinterFactory {
private:
    std::string defaultStr;
public:
    PrinterFactory(std::string defaultStr);
    ~PrinterFactory();
    Printer *makeNewPrinter();
};

PrinterFactory::PrinterFactory(std::string defaultStr = "") {
    // set the default string
    this->defaultStr = defaultStr;
}

PrinterFactory::~PrinterFactory() {
}

Printer *PrinterFactory::makeNewPrinter() {
    // create and return a new printer
    return new Printer(defaultStr);
}

int main(int argc, char *argv[]) {
    PrinterFactory pf;

    for (int i = 1; i < 10; ++i) {
        for (int j = 1; j <= i; ++j) {
            // create a printer to print the number
            Printer *p = pf.makeNewPrinter();
           
            // convert the number to a string
            std::stringstream str;
            str << j;
           
            // print the number
            p->setString(str.str());
            p->print();

            delete p;
        }

        // create a printer to print a newline
        Printer *p = pf.makeNewPrinter();
        p->setString("\n");

        // print the newline
        p->print();

        delete p;
    }

    return 0;
}


>>1
Your gay.

Name: Anonymous 2011-07-06 16:00

>>31
ENTERPRISE QUALITY

Name: Anonymous 2011-07-06 17:30


++++++++++[->+>+>>+++++<<
<<]>>->>-<<[->>[.>]<[->+>
+<<]>+>[-<<+>>]<[<]<<.>]

nested loops? please you are small time

Name: ALPHA 2011-07-06 17:46

>>33
Do you want
Fuck?

Name: Anonymous 2011-07-06 19:10

>>34
Distributed under the terms of the WTFPL

Name: Anonymous 2011-07-07 1:11


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();
    }
  }
}

Name: reddened anus 2011-07-07 11:50

BOOMPSIE

Name: Anonymous 2011-07-07 11:52

plox in C# neone?

Name: Anonymous 2011-07-07 13:33

C#:

namespace ForLoopsAreStillForFags{
    class Program
    {
        static void Main(string[] args)
        {
            int i = -1, j;
            while (i++ <= 9)
            {
               j = -1;
               while (j++ <= i) System.out.Write(j);
               Console.WriteLine();
            }
        }
    }
}

Name: Anonymous 2011-07-07 13:48

>>39
thanxx can u do it w\ fore loops im getting error .out not namespace in system why?????

Name: Anonymous 2011-07-07 16:34

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: Anonymous 2011-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: Anonymous 2011-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?

Name: Anonymous 2011-07-07 18:01

Name: Anonymous 2011-07-07 18:16

>>42
66went to99 operator considered harmful

Name: Anonymous 2011-07-07 18:51

<?php
for ($i = 1; $i < 10; $i++) {
    echo implode(range(1, $i)), "\n";
}

Name: Anonymous 2011-07-07 19:16

>>45
My bad. How's this?

#include <stdio.h>

int main(void) {
  int i = 1
  int j;
  durr:
  j = 1;
  hurr:
  printf("%d", j);
  j++;
  if (j <= i) goto hurr;
  printf("\n");
  i++;
  if (i < 10) goto durr;
}

Name: Anonymous 2011-07-07 22:58

>>39
java ``faggot''

Name: Anonymous 2011-07-08 0:25

([\~] 1..9)>>.say;

nested for what now?

Name: Anonymous 2011-07-08 1:03

>>49
At first, I thought it was Haskell, then APL. Then I realized it was Perl 6.

Name: Anonymous 2011-07-08 1:23

>>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.

Name: Anonymous 2011-07-08 1:34

>>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();
            }
        }
    }
}


>>48
Actually, no. C++ "faggot"

Name: Anonymous 2011-07-08 2:12

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.

Name: Anonymous 2011-07-08 2:22

APL
influence
If anything, being influenced by APL is a Very Bad Thing.

Name: Anonymous 2011-07-08 2:28

>>54
What's wrong with nice array operations?

Name: Anonymous 2011-07-08 3:17

both array processing and list processing are cool and I feel like I'm wading around in molasses when I write C.

There I said it.

Name: ARRay Processor 2011-07-08 4:00

{ "defun", "sum-of-squares", { "x", "y" },
  { "+", { "*", "x", "x" }, { "*", "y", "y" } } }

Name: Anonymous 2011-07-08 4:05

>>57
ONE WORD: THE FORCED SUMMATION OF THE SQUARES. THREAD OVER

Name: Anonymous 2011-07-08 6:11

#include <stdio.h>

#include <ooc/ooc.h>
#include <ooc/lang/Integer/Integer.h>
#include <ooc/lang/String/String.h>
#include <ooc/util/LinkedQueue/LinkedQueue.h>

int main(void) {
  int i;
  Generic *myQueue;

  myQueue = new(LinkedQueue());

  for (i = 1; i < 10; i++) {
    Queue.add(myQueue, new(Integer(), i));
    putfree(String.replace(toString(myQueue), ", ", ""));
  }

  foreach (i, myQueue)
    del(i);

  del(myQueue);

  return 0;
}

Name: Anonymous 2011-07-08 6:13

one word: the forced ejaculation of the penis. thread over

Name: VIPPER 2011-07-08 7:03

COBOL cabal
Haskell shekels
Java barmitzvah

Name: Anonymous 2011-07-08 9:12

>>59
Your enterprise level is low

type
 TNumberGenEnumerator = class
 private
  fNumbers,
  fCurrent: longint;
 public
  constructor Create(ANumbers: longint);

  function GetCurrent: longint;
  function MoveNext: Boolean;
  property Current: longint read GetCurrent;
 end;

 INumberGen = interface
  function GetEnumerator: TNumberGenEnumerator;
 end;

 INumberGenFactory = interface
  function GetInstance(ANumbers: longint): INumberGen;
 end;

 TNumberGen = class(TInterfacedObject, INumberGen)
 private
  fNumbers: longint;
 public
  function GetEnumerator: TNumberGenEnumerator;
  constructor Create(ANumbers: longint);
 end;

 TNumberGeneratorFactory = class(TInterfacedObject, INumberGenFactory)
  function GetInstance(ANumbers: longint): INumberGen;
 end;

constructor TNumberGenEnumerator.Create(ANumbers: longint);
begin
   inherited Create;
   fCurrent := 0;
   fNumbers := ANumbers;
end;

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;

Name: Anonymous 2011-07-08 10:40

>>62
Pascal <3. ;_;

Name: Anonymous 2011-07-08 12:58

Nintendo 64 get.

Name: Anonymous 2011-07-08 14:56

>>63
Pascal is a pointless bastardized version of C.

Name: Anonymous 2011-07-08 17:01

>>65
I like its arrays.

Name: Anonymous 2011-07-09 21:11

>>43
print '\n'.join('123456789'[0:i] for i in xrange(1,10)])

Name: Anonymous 2011-07-09 21:17

>>67
'\n'.join(

Name: Anonymous 2011-07-09 21:23

>>68
Yes, that's what it says.

Name: Anonymous 2011-07-09 21:31

>>67
You must have missed the title of the thread.

Name: Anonymous 2011-07-09 21:43

>>70
You must have missed the rest of the thread.

Name: Anonymous 2011-07-09 22:35

>>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.

Name: Anonymous 2011-07-09 23:11

Oh, did someone mention APL? Here you go:

⍳¨⍳9

Name: Anonymous 2011-07-10 0:28

>>71
print """1
12
123
1234
12345
123456
1234567
12345678
123456789"""


How's that then?

Name: Anonymous 2011-07-10 1:06

1
12
123
1234
12345
123456
1234567
12345678
123456789

English language. Beat that.

Name: Anonymous 2011-07-10 2:48

worst thread ever

Name: Anonymous 2011-07-12 15:56

You are a wizard, Anon.

Name: Anonymous 2011-07-13 0:29

>>75
What are you talking about? All I see is code with syntax errors.

Don't change these.
Name: Email:
Entire Thread Thread List