Help on break/continue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stone
    New Member
    • Jul 2006
    • 11

    Help on break/continue

    I am looking for a code that gives the following output.It will help
    understand the break and continue statements.

    0
    0 1
    0 2 4
    0 3 6 9
    0 4 8 12 16
    0 5 10 15 20 25
    0 6 12 18 24 30 36
    0 7 14 21 28 35 42 49
    0 8 16 24 32 40 48 56 64
    0 9 18 27 36 45 54 63 72 81
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    For what it's worth, I don't think I ever used a continue statement in my four years of academic development. The break statement is not necessary for this either, only two for loops.
    Code:
    int i, j, total = 0;
    for(i = 0; i < LIMIT; i++) // LIMIT = 10 for your example
    {
       total = 0;
       for(j = 0; j < i; j++)
       {
           System.out.print(j + " ");
           j += i;
       }
       System.out.println();
    }

    Comment

    • Stone
      New Member
      • Jul 2006
      • 11

      #3
      Thanks D_C.
      The above mentioned code prints the 0s.
      Do I need to add more inner loops after j?

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Nah, some of the logic was goofed. Didn't you catch it? :D
        Code:
          int i, j, total = 0;
          for(i = 0; i < LIMIT; i++) // LIMIT = 10 for your example
          {
            total = 0;
            for(j = 0; j <= i; j++)
            {
              System.out.print(total + " ");
              total += i;
            }
            System.out.println();
           }

        Comment

        • Stone
          New Member
          • Jul 2006
          • 11

          #5
          :) Umm I have recently started studying this language therefore
          I am having problems. I was told to do this program with
          break/continue. Would u plz give the complete code for the output.
          I mean the way each line looks different from the other. Thanks again

          Comment

          • D_C
            Contributor
            • Jun 2006
            • 293

            #6
            I'll give you the C++ version, but you have to translate it to Java. One thing to note is that C++ doesn't support labels, but Java does. The labels would replace the while(true) loops. Ignore the last two lines of code.
            Code:
            int main()
            {
              int total;
              int line = 1;
              const int MAX = 10; // does 1-9
            
            // First label would go here
              while(true)
              {
                total = 0;
                // Second label would go here
                while(true)
                {
                  cout << total << " ";
            
                  total += line;
                  if(total <= line*line)
                    continue;
                  else
                  {
                    cout << endl;
                    break;
                  }
                }
                if(++line < MAX)
                  continue;
                else
                  break;
              }
            
              system("PAUSE");
              return EXIT_SUCCESS;
            }

            Comment

            • rishabh011
              New Member
              • Nov 2007
              • 1

              #7
              int j=0;
              int i=0;
              for(i=0;i<=9;i+ +)
              {
              for(j=0;j<i+1;j ++)
              {
              cout<<i*j"\t";
              }
              cout<<\n;
              }


              above is the basic code plz dont mind if syntax for cout is wrong sry to b late or 2 late

              Comment

              Working...