for loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confusedwithjava
    New Member
    • Apr 2008
    • 1

    for loops

    How do you write a for loop to print the following line:

    *****
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by confusedwithjav a
    How do you write a for loop to print the following line:

    *****
    pseudo....

    While ( * @ the same line < 5 ) {

    print *;

    }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by confusedwithjav a
      How do you write a for loop to print the following line:

      *****
      Try reading the Java tutorial first.

      Comment

      • pralu
        New Member
        • Mar 2008
        • 34

        #4
        Originally posted by confusedwithjav a
        How do you write a for loop to print the following line:

        *****

        see first of all see what u have to do .... ur requirement is to print 5 stars in single line ....and u must also be knowing that the for loop consists of 3 parts nmely:
        1) initialization 2) condition and 3) increment and decrement

        now u see that u have to print first single star then 2nd then 3rd and then so on
        so loop will be like initialization : 1(i=1) n condiotn will be like untill n unless its equal to 5 (i<=5)and increment with 1 (i++)
        and then print the star..

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by pralu
          see first of all see what u have to do .... ur requirement is to print 5 stars in single line ....and u must also be knowing that the for loop consists of 3 parts nmely:
          1) initialization 2) condition and 3) increment and decrement

          now u see that u have to print first single star then 2nd then 3rd and then so on
          so loop will be like initialization : 1(i=1) n condiotn will be like untill n unless its equal to 5 (i<=5)and increment with 1 (i++)
          and then print the star..
          AAMOF you can use any loop as long as its body is executed 5 times in this case.
          The following loop will also be valid:

          [code=java]
          String loop= "sillyloops ";
          for (int i= 0; i++ < loop.length(); i++)
          System.out.prin t("*");
          [/code]

          kind regards,

          Jos

          Comment

          Working...