Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What instead of if

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What instead of if

    What can we use instead of if in programs? I don't mean the switch case.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: What instead of if

    Explain what you want to do.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: What instead of if

    I read the title of your post and thought you were proposing a "what" keyword. That would be cool, but I suspect it violates some deep rules of logic.

    But you're not proposing that, so I don't really know what you're asking.

    boolean done = false;
    for(;<condition>&&!done;) {
        <whatever>;
        done = true;
    }
    for(;!done;) {
        <whatever else>;
        done = true;
    }

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What instead of if

    I don't want to use " if " or " switch ". Is anything else available in java for this functionality? For example, if I don't want to use "for" I can use "while".
    Sorry for misunderstanding, my level in English...

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: What instead of if

    There is a related but different construct: the ternary operator.

    But nothing that's intended to replace the functionality of if. Is there anything in other languages?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What instead of if

    I don't know if other languages have sth more but I'm trying to learn java and I'm wondering if there is something else instead of "if ". Thank you for your answer.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: What instead of if

    What statement you use depends on what the program logic requires. the if statement has a purpose and usage that is more natural that using a for or while with an extra boolean variable to end its looping.