Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (simple question) how to make an if statement run only once?

    in order to run an if statement only once, I'm using a counter as a conditional and setting the counter to equal one once the if statement runs.

    Is there another way to run an if statement only once?

  • #2
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should be in the Javascript section...

    You really need to give a better explanation; how are you currently running whatever it is? You could place it within a function?

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its a simple if(x>5){
    then
    }

    The thing is that I just want it to run only once and when x becomes greater than 5.

    The reason why I don't use x==5 is because x may jump from 4 to 6, but I only need it to run once past the threshold of 5.

    I also can't use a range because perhaps x will take on a value in that range more than once.

    The simple way i do it is

    $m=0

    if($m==0 && $x>5){
    run...
    $m=1
    }

    So, it runs only once. But, can you do it without the counter?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You need some kind of flag to tell the if statement to run or not. The counter is probably the best way to do it. Why don't you want to use the counter? There is really no way to it without a counter. You will have to get the statement to become false.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'll need to use this function a lot. Adding a counter causes the system to keep track of another variable which might slow processing down.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •