简体   繁体   中英

The if statements' condition

In exercise ten of chapter 3 in this book im trying to go over, I was asked to write a program that takes an operation followed by two operands and output the results. Read the operation into a string called operations and use an if-statement to figure out which operation the user wants.Read the operands into variables of type double. Implement this for operations called +,-,*,/, you know (plus, minus, multiplication, division) the obvious.

This is what I wrote so far and it worked and all but my issues is the concept of the condition in the if-statements. From what I understand the if statement has parenthesis, that within are the conditions where the values are tested to see if they are true or false, Only after that the computer decided whether to run the code or not. So tell me why do I have to write the operation equal to the operator as the conditions, when I have to use the operator in the cout to get the results of the two input values anyway ? I don't understand this, isn't there a better way of writing this with less statements ?

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
    string operation = "";
    double operands1;
    double operands2;
    cout << "Give me either a minus, plus, division, or multiplication operations \n";
    cin >> operation;
    cout << " enter the value you would like to  \n";
    cin >> operands1;
    cout << " enter the second value you would like to \n";
    cin >> operands2;

    if (operation == "+")// I was told to write it like this in the book  {
        cout << operands1 + operands2 << endl;
    }
    else if (operation == "*") {
        cout << operands1*operands2 << endl;
    }
    else if (operation == "-") {
        cout << operands1 - operands2 << endl;
    }
    else if (operation == "/") {
        cout << operands1 / operands2 << endl;
    }
    else {
        cout << " seriously dude im talking about numbers over here \n";
    }
    return 0;
}

For single character operations you can use a switch statement :

switch (c) {
    case '+' : std::cout << op1 + op2 << std::endl; break;
    case '*' : ... 
    ...
    default  : std::cout << "my dud!" << std::endl;
}

If you care about number of statements, you could also use an ugly ternary operator:

std::cout << (c=="+" ? op1+op2 : c=="*" ? op1*op2 : "DUDEEE!") << std::endl;

If you're asking why you can't do something like this:

cout << operands1 operation operands2 << endl;

It's because that's not how the language is structured. You can't put a variable in a place where an operator would be and expect it to be substituted in. Suppose operation didn't contain an operand. What would happen then?

What you have now is the simplest way of handling this.

The assignment is somewhat confusing because the operator "+" is actually a string.

if (operation == "+")// I was told to write it like this in the book  {
      cout << operands1 + operands2 << endl;
}

The variable operation is a variable that holds letters and words. We call this type of variable a string variable. This is confusing to the least.

The if statement then asks whether the word contained in the variable operation is equal to the string "+". This is more confusing because we usually see a plus-sign as a symbol. In your program, the plus-sign "+" is a string.

So, the conditional in the parenthesis is asking if the operation string contains the string "+". If it does, then the code following that will be executed.

The + in the next line is now a real addition symbol, which will add the values in the two operands for printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM