Using a function once in a loop

Is there any way to make something happen once in a loop without using bools ?
Heres some relevant code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void PowerUp(Graphics PowerBar , int frame, SDL_Rect* Frame)
{
    SDL_Rect Clips;
    int x = frame*4;
    if(x >=196)
        x =196;
    Clips.h = 53;
    Clips.w = x;
    Clips.x = 0;
    Clips.y = 0;
    Frame = &Clips;

    PowerBar.ApplySurface(350,530,Frame);
}
int main(int argc, char* args[] )
{
    //...code.
    bool PowerUpOn = 0;
    bool PowerUpUsed =0;
    bool MudBombFired =0;
    //...code.
        while( quit == false )
            {
           //...code. 
                if( event.type == SDL_KEYDOWN )
                    switch( event.key.keysym.sym )
                    {
                        case SDLK_SPACE:
                        {
                          if (PowerUpUsed == 0)
                            PowerUpOn = 1;
                        }
                        default:break;
                    }
                if( event.type == SDL_KEYUP )
                    switch( event.key.keysym.sym )
                    {
                        case SDLK_SPACE:
                        {
                            PowerUpOn = 0;
                            PowerUpUsed = 1;
                        }
                        default:break;
                    }
                }
                //...code.
                if(PowerUpUsed == 1)
                            {
                                Character.Animate(FrameNumber);
                                if(MudBombFired == 0)
                                {
                                   MudBomb.ApplyForce(xx*2.5,100);
                                   MudBombFired = 1;
                                }

                                MudBomb.Graph.Animate(FrameNumber);
                                MudBomb.Show();
                            }





Obviously this is incomplete code as ive only posted the relevant parts.
Im not even sure if the brackets line up in the code just pasted.
But anyway is there any other way instead of using the bools to guarantee that the statements are only executed once ?
Such as
1
2
3
4
5
                                if(MudBombFired == 0)
                                {
                                   MudBomb.ApplyForce(xx*2.5,100);
                                   MudBombFired = 1;
                                }
Last edited on
you can do it many ways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

double d( 2.1 );
char ch( 'f' ); 
std::string str( "apple" );
bool bl( true );

for( int i( 0 ); i < 10; ++i )
{
    if( i == 3 ) function();
    if( d >2.099 && d < 2.101 ) function();
    if( ch == 'f' ) function();
    if( str == "apple" ) function();
    if( !bl ) function(); // or ->>>      if( bl == false ) function();
}
Sorry i think you misunderstood.
I understand i can use different data types.
I was asking if there was anyway to tell a statement to only execute once inside the loop without the use of a variable.
Maybe a switch? Otherwise I do not think so if you are looping other stuff and only that function once but if you are only trying to run the loop once you shouldn't use a loop or just a break I guess.


***Looks like there is a function mentioned by JLBorges***
Last edited on
No it runs multiple times but thanks anyway :)
That call once function is what i was looking for thanks :)
Topic archived. No new replies allowed.