   Loop Problem
  What I'm trying to do is make a counter of sorts, that will count up to 10000, then reset the counter to 0 and start counting again. When the counter is over 5000, something will happen, and something else will happen if less than 1000. I am accomplishing this via a for loop (which is in the main while loop). Here is the code:for(levtimer = 0; levtimer < 10000; levtimer++){if (levtimer >= 10000){levtimer = 0;}}if(levtimer > 5000){levitate++;}else{levitate--;}The counter (levtimer) imeadiatly goes to 10000 and stays there, while levitate is constantly going up.   
  That is because that is what you have written.Your loop body condition can never be true. The condition after is always true. Your code can be optimised to:levtimer = 10000;levitate++;To fix it, we need to know a little more about what is going on. It sounds like you might have some kind of higher level loop wrapped around this code. Is this the case?   ;  The loop can be thought as:{  levtimer = 0;   while(levtimer < 10000)  {if (levtimer >= 10000){levtimer = 0;}levtimer++;  }}   ;  If you go through your logic, your running the for loop from 0 to 9999. The if statement inside of your for loop is never resetting your for loop to 0 because it is never going to reach that value.Now you got your checks for levtimer outside the for loop so that is not ever going to happen (your always going to trigger your else statement). It seems like from your code that you would like for an object to move up and down.I would recommend storing your direction in a variable. A 1 would mean the object moves down, a -1 means the object movies up.In your initalization you would set ObjY to the start location of the object, and set DirY to the start direction (1 for down, -1 for up).Then in your while loop you would do (psedo code)if(ObjY<MinYVal)   DirY=1;if(ObjY>MaxYVal)   DirY=-1;ObjY+=DirYWhere ObjY is the y position of your object and DirY is a variable storing which way to move. MinYVal and MaxYVal would be the range of Y values you want the object to have.To move faster replace 1 and -1 with Speed and -Speed.   ;  I think what you want is something like this: levtimer = 0;while(true){   if (levtimer > 5000) levitate++;   else levtitate--;   if (levtimer >= 10000> levtimer = 0;   levtimer++;}Of course, if you want to exit the loop you'll have to put a break statement somewhere, or use a condition in the loop setup that will become false at some point. Without further information it's difficult to tell precisely what you want this loop to do.   ;Quote:Original post by AristeI think what you want is something like this: levtimer = 0;while(true){   if (levtimer > 5000) levitate++;   else levtitate--;   if (levtimer >= 10000> levtimer = 0;   levtimer++;}Of course, if you want to exit the loop you'll have to put a break statement somewhere, or use a condition in the loop setup that will become false at some point. Without further information it's difficult to tell precisely what you want this loop to do.Thanks for the help, this seems like the most logical answer. Shadowisadog, yours also seems like it would work, and you are correct in me trying to move a shape up and down, but that shape is a quad, and it's 4 Y positions are height, height, -height, and -height. I tried getting it to work, but it doesn't.Ariste, your's also seems logical, and I know this isn't your fault (it happened to me when I tried a when loop too.), and you probably won't be able to fix it... I'm programming on the Wii right now, and I'm trying to make a USB backup loader (if you don't know what that is, Google it). Right after the progress bar it's supposed to render the cover pictures (the quads), but when I have a while loop in there, it doesn't get that far. Could this be the cause of having a while loop inside a while loop? Almost the whole thing is run on one while loop (to check for button presses, to draw the covers, etc.), and I put the other while loop near the top of it.EDIT: I just tried moving Ariste's code into the .c file where the covers are being drawn, and now it gets a bit farther than before but still freezes. If it helps anyone, here is the code:GRRLIB_3D_Init();    GXTexObj texObj;    u16 width, height;    Mtx m, mv;    GX_InitTexObj(&texObj, tex.data, tex.w, tex.h, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);    width = tex.w * 0.01 * scale;    height = tex.h * 0.01 * scale;    guMtxIdentity (m);    guMtxScaleApply(m, m, 1.0, 1.0, 10.0);    Vector axis = (Vector) {0, 1, 0 };    guMtxRotAxisDeg (m, &axis, degrees);if(scale &gt; 1)guMtxTransApply(m, m, loc, 0, 6.0f+falloff);elseguMtxTransApply(m, m, loc, 0, 8.0f+falloff);    guMtxConcat (view, m, mv);// Draw the Front CoverGX_LoadTexObj(&texObj, GX_TEXMAP0);    GX_SetTevOp (GX_TEVSTAGE0, GX_MODULATE);    GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);    GX_LoadPosMtxImm (mv, GX_PNMTX0);levitate = 1;levtimer = 0;while(true){if (levtimer &gt; 5000)levitate++;elselevitate--;if (levtimer &gt;= 10000)levtimer = 0;levtimer++;}    GX_Begin(GX_QUADS, GX_VTXFMT0, 4);    GX_Position3f32(-width, -height + levitate, 0);    GX_Color1u32(color);    GX_TexCoord2f32(1, 1);    GX_Position3f32(width, -height + levitate, 0);    GX_Color1u32(color);    GX_TexCoord2f32(0, 1);    GX_Position3f32(width, height + levitate, 0);    GX_Color1u32(color);    GX_TexCoord2f32(0, 0);    GX_Position3f32(-width, height + levitate, 0);    GX_Color1u32(color);    GX_TexCoord2f32(1, 0);    GX_End(); [Edited by - G2K on May 27, 2009 7:32:22 PM];  Your program is freezing because it's getting stuck in an infinite while loop.If you want to use my method, you'll have to create some sort of exit condition for the loop. The "while (true)" part initiates an infinite loop; unless you use a break statement inside the loop or change the loop condition to a falsifiable statement, it will never exit. Figure out what condition should cause the "cover pictures" to stop being rendered, and build that into your loop. If, for example, you want them to stop after a certain amount of time has elapsed, use something like "while (GetTickCount() < maxTime)."   ;while( true ){   levtimer = ++levtimer % 10000;   levitate = levtimer - 5000;}