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

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


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





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



    }// end of class

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    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:
    to get highlighting and preserve formatting.
    Last edited by Norm; 02-04-2014 at 02: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, 07:16 AM
  2. nested for loop!
    By Yoana in forum New To Java
    Replies: 3
    Last Post: 01-23-2014, 07:57 PM
  3. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 06:10 PM
  4. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 04:39 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02: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