Results 1 to 2 of 2
  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default nested for loop!

    I'm trying to print the figure only once and i get it that is printing 3 times because is inside the loop that prints everything 3 times, but i need everything else to print 3 times so is there any way that i can print the figure only once without using an if else statement?/can someone please help me!

    public class Staircase {
    public static final int STAIRS=5;

    public static void main (String [] args){
    int stairs = STAIRS;




    for(int stairCount = 1; stairCount <= STAIRS; stairCount++) {

    for(int i = 1; i <= 3; i++) {// print everything inside this loop 3 times!

    for(int j = 1; j <= stairs*5-5; j++) {
    System.out.print("$");


    }


    for ( int k = stairs ; k<=stairs; k++){
    System.out.print(" o ******");/////
    System.out.print(" /|\\ * ");
    System.out.print(" / \\ * ");}


    for(int j = 1; j <= (stairs*(-5))+(STAIRS*5); j++) {

    System.out.print("&");

    }

    System.out.println("*");

    }stairs--;
    }

    for(int i = 1; i <= STAIRS*5+7; i++) {

    System.out.print("*");

    }}

    }// end of class

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

    Default Re: nested for loop!

    without using an if else statement?
    Please explain why.
    if statements are useful for conditionally executing statements. They are probably more efficient than other conditional statements.
    One solution would be to set a condition in the inner for loop so it only executed one time.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Last edited by Norm; 02-04-2014 at 01:29 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. how to use nested for loop
    By geekgeek1 in forum New To Java
    Replies: 5
    Last Post: 02-04-2014, 06:16 AM
  2. nested for loop!
    By Yoana in forum New To Java
    Replies: 3
    Last Post: 01-23-2014, 06:57 PM
  3. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  4. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01: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
  •