Results 1 to 12 of 12

Thread: While loop

  1. #1
    sjhentges is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default While loop

    Hi, I am pretty bad at this stuff so far haha, so i'm just wondering how to make a simple square of '#' symbols. Lets say it should be 10 rows, with each row containing 15 symbols. What would this code look like?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    It would print the first row of 15 #s.
    Then in a loop it would print the next 8 rows with # spaces #
    Finally it would print the last row of 15 #s.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sjhentges View Post
    Hi, I am pretty bad at this stuff so far haha, so i'm just wondering how to make a simple square of '#' symbols. Lets say it should be 10 rows, with each row containing 15 symbols. What would this code look like?

    Best thing is you should make an attempt and see, if so can you post what you have done so far here.

    Here is a very simple way,

    Java Code:
            for(int i = 0; i < 10; i++) {
                System.out.println("###############");
            }

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Or
    Java Code:
    for(int i = 0; i < 10; i++) {
            for(int j=0;j<15;j++){
                System.out.println("#");}
            System.out.println();
            }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    I am pretty bad at this stuff so far haha
    lol me too ,loops make my head spin but my advice to you is- just try and make mistakes u'll learn more.:)

  7. #7
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    Loops, powerful things, you may not like them now, but believe me, once you get the hang of them, they're quite useful/essential,
    so go make friends with loops.

    BTW, a teacher of mine hates a do/while. In her opinion, it's useless, unless you use it for primitive menus.
    What do you guys think?

  8. #8
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    for me i think they all the sameactually but i prefer for loops

    unless you use it for primitive menus.
    what do u mean by primitive menus :o

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    do/whiles are annoying. You can do the same with a simple for or while loop, and that's more readable. Personally, I never use do/while loops; they look ugly and a for loop is better readable.
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Supamagier View Post
    do/whiles are annoying. You can do the same with a simple for or while loop, and that's more readable. Personally, I never use do/while loops; they look ugly and a for loop is better readable.
    For and while loops are the most usable loops in programming. But in some cases I feel that do-while loop is much easier that for or while loop.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    The main use I see for a do{} while is when you ALWAYS want to execute the code ONE time.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM
  2. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  3. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  4. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •