Thread: code fragment pls help

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question code fragment pls help

    Trying to rewrite the following code fragment using while() and do-while() statements.


    > int i, j;
    > for (j=0; j < 4; j++)
    > for (i=0; i < 3; i++)
    > cout << "The coordinate is " << i << ", " << j <<
    >endl;

    A hand would be very much appreciated...

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    maybe...
    Code:
    int i = 0, j = 0;
    
    while ( j < 4 ) {
       do while ( i < 3 ) {
          cout << "The coordinate is " << i << ", " << j << 
          i++
       }
       j++
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Trying to rewrite the following code fragment using while() and do-while() statements.
    That snippet is better suited to for loops since you know exactly how many times each loop is to run, but since you asked the equivalent while and do..while snippets would be:

    With while loops:
    Code:
    int i, j = 0;
    while ( j < 4 ) {
      i = 0;
      while ( i < 3 ) {
        cout<<"The coordinate is "<< i <<", "<< j <<endl;
        i++;
     }
     j++;
    }
    With do..while loops
    Code:
    int i, j = 0;
    do {
      i = 0;
      do {
        cout<<"The coordinate is "<< i <<", "<< j <<endl;
        i++;
      } while ( i < 3 );
      j++;
    } while ( j < 4 );
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. help on my code PLS!!!
    By gtr_s15 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 07:48 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM