while loop and numbers

This is a discussion on while loop and numbers within the C Programming forums, part of the General Programming Boards category; I was wanting to output numbers in the format below i-> 0 j-> -0 i-> 0 j-> -1 i-> 0 ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    while loop and numbers

    I was wanting to output numbers in the format below

    i-> 0 j-> -0
    i-> 0 j-> -1
    i-> 0 j-> -2

    i-> 1 j-> -0
    i-> 1 j-> -1
    i-> 1 j-> -2

    i-> 2 j-> -0
    i-> 2 j-> -1
    i-> 2 j-> -2

    So i wrote this while loop :

    Code:
    # include <stdio.h>
    
    int main(void){
    int i=0; int j=0;
    
    while(i<3){
           while(j<3){
               printf("\n i->%d j->%d",i,j);
               j++;
           }       
           i++;
       }     
    }
    but it outputs
    i-> 0 j-> -0
    i-> 0 j-> -1
    i-> 0 j-> -2

    only.
    Wonder if someone could tell me where I am going wrong.thanks alot.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Because you aren't resetting the value of j, so it never goes back into that loop.

    Code:
    # include <stdio.h>
    
    int main(void){
    int i=0, j;
    
    while(i<3){
        j = 0;
           while(j<3){
               printf("\n i->%d j->%d",i,j);
               j++;
           }       
           i++;
       }     
    }
    Sent from my iPadŽ

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Because you forgot to reset j to zero after the first inner loop.

    Edit: too slow.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    thanx for the replies.you ppl were fast

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    i might be too later but the program doesn't print what you wanted, rather...
    Code:
    i-> 0 j-> 0
    i-> 0 j-> 1
    i-> 0 j-> 2
    i-> 1 j-> 0
    i-> 1 j-> 1
    i-> 1 j-> 2
    i-> 2 j-> 0
    i-> 2 j-> 1
    i-> 2 j-> 2

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well, I think that was just typos with his example output, otherwise, I'd just have to say that negetive zero isn't a number. If you did want negetive numbers, though. Just do i-- and compare i to being greater than -3 in your conditional.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  2. for loop to find prime numbers
    By lsecrease in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:58 AM
  3. for loop to find prime numbers
    By 1rwhites in forum C Programming
    Replies: 11
    Last Post: 10-21-2005, 11:37 AM
  4. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 12:40 PM
  5. Totaling up numbers in a for loop?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 04:23 PM

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