Thread: FOR and IF

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    FOR and IF


    hello, we were ask to do this pattern in a combination of FOR and IF LOOPS, but how do i do it?
    Code:
        1
       121
      12321
     1234321
    123454321
    i can only do it using FOR LOOPS only and without IF
    Code:
    public class pattern
    {
        public static void main(String[] args)
        {
            int space, var1, var2, var3;
    
            for(var1=1; var1<=5;var1++)
            {
                    for(space=4; space>=var1; space--)
                    {
                        System.out.print(" ");
                    }
                    for(var2=1; var2<=var1; var2++)
                    {
                        System.out.print(var2);
                    }
                    var3=0;
                    for(var3=var1-1; var3>=1; var3--)
                    {
                        System.out.print(var3);
                    }
                    System.out.println();
            }
        }
    }
    Last edited by Kuiva; August 17th, 2013 at 10:14 AM.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    What does the current code print out when executed?

    What is the pattern of the printed spaces and numbers as related to the row that is being printed?

    The variable names have no meaning except for space.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by NormR
    What does the current code print out when executed?

    What is the pattern of the printed spaces and numbers as related to the row that is being printed?

    The variable names have no meaning except for space.
    it prints out the pattern shown above

    var1=creates how many lines

    var2=right part pattern
    Code:
    1
    12
    123
    1234
    12345
    var3=the left part pattern
    Code:
    1
    21
    321
    4321
    problem is, how am i gonna apply IF statement in combination with FOR loop to create it.
    Last edited by Kuiva; August 17th, 2013 at 10:39 AM.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    if statements are used to make a decision to chose one of two paths. What decision does the code need to make?

    The variable names should say what they are used for. For example:
    var1 should be rowNumber or lineNumber

    varN doesn't say anything about what is in the variable.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by NormR
    if statements are used to make a decision to chose one of two paths. What decision does the code need to make?

    The variable names should say what they are used for. For example:
    var1 should be rowNumber or lineNumber

    varN doesn't say anything about what is in the variable.
    Code:
    public class pattern
    {
        public static void main(String[] args)
        {
            int space, rownum, column_1_5, column_6_9;
    
            for(rownum=1; rownum<=5;rownum++)
            {
                    for(space=4; space>=var1; space--)
                    {
                        System.out.print(" ");
                    }
                    for(column_1_5=1; column_1_5<=rownum; column_1_5++)
                    {
                        System.out.print(column_1_5);
                    }
                    for(column_6_9=rownum-1; column_6_9>=1; column_6_9--)
                    {
                        System.out.print(column_6_9);
                    }
                    System.out.println();
            }
        }
    }
    if statements are used to make a decision to chose one of two paths.
    -i know that

    What decision does the code need to make?
    -this is what i want to know our professor told us that he did that pattern in a combination of FOR LOOP and IF STATEMENT. That is what he want us to do, apply if statement and for loop in order to make that pattern, unfortunately i dont have the slightest idea on how to do it that is why i am asking help. just a hint perhaps. i can only do it using for loops alone
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Your solution looks like the correct one.
    The only decisions made in a for loop is when to end the looping. Replacing those loop ending conditions with if statements would make for poor, messy code.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    so um,forget about using IF STATEMENT
    Code:
    public class pattern2
    {
        public static void main(String[] args)
        {
            int rownumber=0, column=0, space, extra=0;
            
            
            for(rownumber=1; rownumber<=5; rownumber++)
            {
                for(space=4; space>=rownumber; space--)
                {
                    System.out.print(" ");
                }
                for(column=1; column<=rownumber; column++)
                {
                    System.out.print(column);
                }
                for(column=rownumber-1; column>=1; column--)
                {
                    System.out.print(column);
                }
                System.out.println();
            }
            
            for(rownumber=4; rownumber>=1; rownumber--)
            {
                for(column=1; column<=rownumber; column++)
                {
                    System.out.print(column);
                }
                for(column=rownumber-1; column>=1; column--)
                {
                    System.out.print(column);
                }
                System.out.println();
            }
        }
    }
    output:
    Code:
        1
       121
      12321
     1234321
    123454321
    1234321
    12321
    121
    1
    how am i gonna work on the spaces below in order to make it diamond in shape? i ran out of ideas -_-
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Problem solved, please delete this thread if possible. Thank you

IMN logo majestic logo threadwatch logo seochat tools logo