Alternatives for "if"

This is a discussion on Alternatives for "if" within the C++ Programming forums, part of the General Programming Boards category; I have a project that specifies that we cannot use if statements. It's to find the second highest value in ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Alternatives for "if"

    I have a project that specifies that we cannot use if statements. It's to find the second highest value in an array, without using if statements. All I am looking for is alternatives to if statements, because apparantly my prof doesn't like them. He said something about a statement that's formatted something like this:

    (statement)? ( ) : ( ). but I had no clue what he was talking about because I have a hard time understanding him (he's from Romania)... Any help would be greatly appreciated.

    edit: it's not finding the second highest value in an array, it was determining if one array was identical to another array. my bad, got those mixed up.
    Last edited by criticalerror; 01-22-2004 at 04:34 PM.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    int c = a > b ? a : b;     // c = a if a > b, else c = b
    ...alternatives to if statements, because apparantly my prof doesn't like them.
    If he does not like 'em, then I don't like him. Statement above may save line but it's harder to understand than an if statement.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    alright, looks to be pretty easy, however I agree it is much less clear than if...else. Can you do something like setting a boolean value to true in addition to
    Code:
    int c = a > b ? a : b;
    what I mean is can you add something like found = true right after the b?
    Last edited by criticalerror; 01-22-2004 at 04:33 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by criticalerror
    alright, looks to be pretty easy, however I agree it is much less clear than if...else. Can you do something like setting a boolean value to true in addition to
    Code:
    int c = a > b ? a : b;
    what I mean is can you add something like found = true right after the b?
    Yes. The real format of this statement is:
    Code:
    val = (expr) ? (vTrue) : vFalse ;
    expr can an be anything that returns True or False
    vTrue and vFalse are the values that val will contain debending on the outcome.

    If all you want is a boolean value, you can simply use:
    Code:
    val = a < b;
    Here val is loaded with the outcome of a < b, True or False
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Board Conservative UnregdRegd's Avatar
    Join Date
    Jul 2003
    Posts
    154

    Re: Alternatives for "if"

    Originally posted by criticalerror
    I have a project that specifies that we cannot use if statements.
    As a rule, whatever can be done using ifs, whiles, fors, and the like can be done using the much more elegant goto statement. Of course, you'll still need to use one of those constructs--or inline assembly--to complement the goto.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a = 5;
        int b = 6;
        while (a < b)
            goto bob;
        goto death;
        bob:
            cout << "Bob has been executed." << endl;
        death:
        cout << "Now dead and buried." << endl;
        return 0;
    }
    I am a programmer. My first duty is to God, then to nation, then to employer, then to family, then to friends, then to computer, and finally to myself. I code with dignity, honor, and integrity.

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Re: Re: Alternatives for "if"

    Originally posted by UnregdRegd
    As a rule, whatever can be done using ifs, whiles, fors, and the like can be done using the much more elegant goto statement. Of course, you'll still need to use one of those constructs--or inline assembly--to complement the goto.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a = 5;
        int b = 6;
        while (a < b)
            goto bob;
        goto death;
        bob:
            cout << "Bob has been executed." << endl;
        death:
        cout << "Now dead and buried." << endl;
        return 0;
    }
    Why use a statement its own creators abhor. The goto statement is there to wean you off BASIC or FORTRAN, IMHO. It doesn't support modular programming concepts and is "infinetly abusable" why fall back on bad habits. Besides The conditional operator is succint and in some cases can even increase performence.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    37
    [CODE]
    int array[] = {100,2,5,1,89,4,6};
    int arrayLen = 6;

    int oldValue = array[0];

    for (int i = 1 ; i < 6 ; i++)
    {
    oldValue = array[i] > oldValue ? array[i] : oldValue;
    }

    cout << "Max value is " << oldValue;
    {/CODE]

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Some assumptions...

    1 - Your professor doesn't have a problem with if-statments. He just wants you to learn something else. You cannot program without if-statements. All programming languages will have the equivalent of an if-statement. In C/C++ "if" is the most common form of conditional logic. And IMHO, conditional logic (or "conditional branching") is one of the two most important fundamental programming concepts. (The other is looping.)

    2 - for this assignment, your professor wants you to use this: val = (expr) ? (vTrue) : vFalse ; Since that's what he covered in class. I believe I've heard this called "The Tertiary Statement." And, maybe I'm a whimp... but I agree with alphaoide. I always use if/else 'cause it's easier and clearer.

    3 - UnregdRegd is kidding when he suggests using goto. He forgot to put a big in his post! caroundw5h is not kidding! [EDIT]- Besides that, goto itself isn't "conditional". In the example, the while() statement is the conditional statement.

    4- Next time (maybe this time) your professor tells you not to use if, he will want you to use the switch/case statement. In many cases switch/case is much cleaner than a bunch of if-statements.
    Last edited by DougDbug; 01-22-2004 at 09:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::map Alternatives
    By EvilGuru in forum C++ Programming
    Replies: 8
    Last Post: 04-12-2008, 04:45 AM
  2. Client /server alternatives
    By caroundw5h in forum Tech Board
    Replies: 2
    Last Post: 03-08-2004, 03:54 PM
  3. automake alternatives
    By bludstayne in forum Tech Board
    Replies: 1
    Last Post: 01-04-2004, 02:15 PM
  4. MFC alternatives?
    By Hubas in forum Windows Programming
    Replies: 6
    Last Post: 08-01-2002, 09:04 PM
  5. The 'delay()' function and its alternatives...
    By Nutshell in forum Game Programming
    Replies: 1
    Last Post: 04-27-2002, 02:45 AM

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