Is there a "when" statement in C++?

This is a discussion on Is there a "when" statement in C++? within the C++ Programming forums, part of the General Programming Boards category; Is there a "when" statement in C++? I mean, like: Code: when(TRUE) { //do this } Thanks in advance....

  1. #1
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176

    Is there a "when" statement in C++?

    Is there a "when" statement in C++? I mean, like:
    Code:
    when(TRUE)
    {
    //do this
    }
    Thanks in advance.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes, except it's cunningly referred to as while.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Nope, just use while loops
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Originally posted by Eibro
    Yes, except it's cunningly referred to as while.
    No, when I use while, it won't execute the code anytime the code is true. It only checks when its reached.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a "when" statement in C++?
    No, just negate the while loop:
    Code:
    while ( !TRUE )
    
    or
    
    while ( FALSE )
    >Yes, except it's cunningly referred to as while.
    You're confusing while with some form of until. while continues as long as the condition is true, when or until continues as long as the condition is false.

    -Prelude
    My best code is written with the delete key.

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I think what you are saying is that you want your program to do something whenever a condition is true ( for example x == 3 ), but otherwise just be running some other section of code normally.

    While not the most efficient way, one method would be to spawn another thread which is constantly checking the condition and acting appropriately like
    while(true) if(x == 3)doSomething();

    But this is probably not the optimal solution to your problem, but if you describe more about what you are trying to do, then I can probably suggest a better way.

  7. #7
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    I'm making a RPG. I want
    Code:
    levup(/*the exp needed to levelup*/100)
    to activate (the exp needed is gonna go up every time levup() is called )whenever the exp meets a certain requirement. I don't want it to be check every so often.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >to activate (the exp needed is gonna go up every time levup() is called )whenever the exp meets a certain requirement. I don't want it to be check every so often.<

    Why not just check for a level up whenever exp is added? Create a table of exp needed and cross reference this from your level up function.
    Joe

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, it's called an if check. Every time you give experience, you check to see if it levels them. How hard is that?
    Code:
    You::GainExp( expValue )
    {
        itsExp += expValue;
        CheckLevelUp( );
    }
    
    You::CheckLevelUp( )
    {
        if( itsExp >= someFormulae )
            GainLevel( );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    its probably more efficient to read some tutorials or books on c++ rather than posting a whole thread on each thing as simple as an if statement

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Using an if statement within a while loop is(in my book) considered an when statement.

    Because within the while loop, things are happening. The if statement is like when this happens(if(this is true)) do this within the while loop.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    or a while loop in an if .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 03:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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