code fragment pls help

This is a discussion on code fragment pls help within the C Programming forums, part of the General Programming Boards category; Trying to rewrite the following code fragment using while() and do-while() statements. > int i, j; > for (j=0; j ...

  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,796
    >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, 06:52 AM
  3. help on my code PLS!!!
    By gtr_s15 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 06: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, 12:33 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21