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

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

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