for loop

Hello

i need to make a for loop that changes direction everytime a specific number is randomized, im not really familiar with that.

example: if number 4 is randomized the loop changes from ++ to -- also the values changes and if another 4 is the value the loop changes from -- to ++

any help appreciated
closed account (S6k9GNh0)
I of course don't understand your objective. Without it I can't make an accurate method of accomplishing what your wanting.

However, I'm great at guessing. I'm assuming that you want something to lower or heighten the value of a variable depending if it's lower or higher than a constant value. This can be accomplished using if / else statements:

1
2
if (myNumber > 10) std::cout << "myNumber is greater than 10!" << std::endl;
else if (myNumber >=0) std::cout << "myNumber is less than 10! NOOOOO" << std::endl;
Last edited on
No not really, i loop out something in a array in a order i type in like 0 to 5 but if i type in the number 4 i want it to loop from 4 to 0 aka backwards lets say i loop out 0 to 5

0 1 2 3 4 5

and when i type in 4 it shuld be like this

4 3 2 1 0

also i want it to do that in the same loop as the other
Last edited on
closed account (z05DSL3A)
Like computerquip, I don't fully understand what you are trying to do

perhaps somthing along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    int step = 1;

    for (int i = 1; i > 0; i += step)
    {
        cout << i << endl;

        if(i == 5) step = -1;
    }
    return 0;
}


Edit:
from the above post, It looks like you want two loops the first to display, then get some input, then another to display the reverse.
Last edited on
ok the thing is i think i got an error in my code somewhere since when i try to step backwards the output becomes like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
8    ☼   zÏ?|8 '|ÿÿÿÿoö↕  ☻  ☺   ☼      "☻'|Rr r   8     ♣
Ôø↕ è☼'|☺   ☼   ☺   ♦   s  î☺  øø↕ ♠   ☼   Û☺'|E ‼ ♦   Ä
Pö↕ er   é?|` '|♠   ☼       8 8     Ú8Xx♦   ☺   ☼
      Hsfr            ☼   @♠8 K ° þÿÿÿÚ8Xx    ☺   ☼
 ♠   ☼       9 8  P8     ¸¢[x☺   ☼   ♠   ä▼%  %ú↕ jÔ'|♣
   ☻   ☺   ☼   öÿÿÿ¨P8 R r ◄   x☺8 ♣   ☼   x☺8 10  ~P8
   Sr  ?   !   ♠   ☼   ÈN8 2   x☺8 ►   î☺  ☺   ☼   ♠   º-
  ☼       6   →_Xx  8     ☺   ☼   c   @ù↕ Rr ↕ ♣"KsPx♣   ☼
 ☻   ☼   ☺   ¶ù↕ Rrtd ?|.4'|ÿÿÿÿ♣   ☼   ♦   E   uÈ↨♂♀ù↕ ë5Tx
ver  STK   ♠   ☼     ‼ 3 HxdwPx] \s☺   ☺   ☼
  çÝr    Hx    ♠   ☼       D ↕ (èKxÔù↕  é?|☺   ☼   ♦
Kx♣   ☼   6?Kx4 Hx☺   Æ?Kxd>øØ☺   ☼   ♥     ?|r    ?|X☻?
'   Ð▼% ☺   ☼   ♣   ☺   Hrter ♥Ô'|☺      ☼   ↑û↕ 5 '|ÉË'|
ù↕ er ⌂À▲% °Ö'|♠   ☼   £Ù'|4 %  ðý⌂ ðý⌂    ☺   ☼
 

closed account (z05DSL3A)
Maybe you should post the code that you are having a problem with?
closed account (S6k9GNh0)
I don't think I could get that output if I tried.
closed account (z05DSL3A)
If, as you have previously said, you are outputting from an array, have you got guards to stop it going out of bounds?
That was the problem actually i forgot it starts on 0 :/

is it possible to change a boolean from true to false and false to true in 1 if statement btw?
closed account (z05DSL3A)
is it possible to change a boolean from true to false and false to true in 1 if statement btw?

Again I'm not entirely sure what you want, possibly:
1
2
3
bool test = true;
//...
test = !test; // set test to its opposite value 
Last edited on
could a ternary statement be what you're looking for?
1
2
bool bVar = false;
bVar = ( /* some boolean expression */ ? true : false );
closed account (S6k9GNh0)
This is ridiculous. If we can't figure out what he wants, there's no point in wasting our wisdom on guessing.
ZOMG! Gret Wolf has 1337 posts!!!

Grey Wolf (1337) Oct 28, 2009 at 4:16pm
Last edited on
closed account (z05DSL3A)
ZOMG! Gret Wolf has 1337 posts!!!

???

Edit:
sorry just noticed. :0)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <winuser.h>

// ...

BOOL  bDirection = TRUE;
int	  i = 0;

for(; /* set condition here*/; (bDirection ? i++ : i--))
{
  // do stuff
  
  if(IsFour())
    bDirection = TRUE;
  if(IsTen())
    bDirection = FALSE;
}

// if bDirection == TRUE, then i++
// if bDirection == FALSE, then i-- 


?
Last edited on
looks good to me..
Topic archived. No new replies allowed.