Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining a Function (or operator)

Status
Not open for further replies.

theDexMonster

Programmer
Jan 25, 2004
1
US
This may be a stupid question but I have to ask.
I am attempting to learn C++ using a book by Deitel and noticed an if/else statement written:

if ( grade >= 90 )
cout << &quot;A&quot;;
else if ( grade >= 80 )
cout << &quot;B&quot;;
else if (grade >= 70 )
cout << &quot;C&quot;;
else if (grade >= 60 )
cout << &quot;D&quot;;
else
cout << &quot;F&quot;;

now... this looks all and good, except I don't understand why it would only print a &quot;C&quot; if you have a 72, rather than printing all that applies. (ie. >= 70 = C, B, A)
is there already, or can I define an operator or function called &quot;between&quot; and say: (>79 but <90);
 
To see why this works, just go through each statement and see if it evaluates to true. Is 72 >= 90? No, so go to the else statement. Is 72 >= 80? No, so go to the next else statement. Is 72 >= 70? Yes, so print &quot;C&quot; and exit the series of if/else statements. Even though, 72 >= 60, the point of the else is to only do that if the statements above it have not been met. That is why &quot;D&quot; and &quot;F&quot; are not also printed.

As far as an operator for between, you could write your own function, but I don't see the need for it. It would seem simpler to say (grade > 79 && grade < 90). Of course, in your example that isn't necessary because it takes advantage of the if/else statement.
 
> I don't understand why it would only print a &quot;C&quot; if you have a 72, rather than printing all that applies

Think about the meaning of the word &quot;else.&quot; It means exactly what it does in English.

The else clause only gets executed if its corresponding if was false. Thus, in an if/else construct, only one thing ever happens: this, or else that.


Note that:

Code:
if (x)
    a();
else if (y)
    b();
else if (z)
    c();
else
    d();
is just a neater way of writing:

Code:
if (x)
    a();
else
{
    if (y)
        b();
    else
    {
        if (z)
            c();
        else
            d();
    }
}
but it has the effect that only one clause is going to get executed in an if ... else if ... else if ... else construct.


To get the behavior you expected, you'd use several if statements (no elses). Each one that evaluates to true will have its result executed.


There's no need to create a function/operator for this kind of behavior. Go reread the chapter on ifs and elses and work through the examples until you understand what's going on.


Note that if/else are neither functions nor operators. They're a built-in part of the C/C++ language. You couldn't program anything like them unless you reprogrammed your compiler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top