Why is this the output?

Hi :) I understand if this is something no one might want to help with. But I was hoping to get a clearer understanding of why these questions have a certain output.

These questions won't be on the exam I'm taking as I already know the answers but I want to know why these are the answers. I'm only fuzzy on these 3 problems.

Determine if the following Boolean expressions are true or false:
3 - 8 % 5 == 6 - 12 / 2

I know that it's "true" but whenever I calculate it, it doesn't come out to true. I get 2==0. I know I'm calculating it wrong. I thought I was doing it by order of presidence though.

Then this question.
Given an input of 3 4 5, show the output of the following program fragment:
cin >> a >> b >> c;
if (a <= b || b >= c)
cout << b;
else if (a < b && b < c)
cout << a;
if (b < c)
cout << c;

I know it's 45. Is it because the else if statement here is never true? If so I don't understand why.

And the last one.
Show the output of the following program fragment:
for (int i = 1; i <= 3; i++)
cout << i << 14 % i;

The answer is 102032. I can't get this one at all because we just studied this and I'm still fuzzy on how to calculate this.

Any help is greatly appreciated.
I know that it's "true" but whenever I calculate it, it doesn't come out to true. I get 2==0.

Perhaps you need to research operator precedence.

I know it's 45. Is it because the else if statement here is never true?

No, in this case it's because the first if() statement executes. If the if() evaluates to true then the else is not executed. Perhaps some formatting would help?
1
2
3
4
5
6
7
8
    if(a <= b || b >= c)
        cout << b;
    else 
        if(a < b && b < c)
            cout << a;

    if(b < c)
        cout << c;



The answer is 102032. I can't get this one at all because we just studied this and I'm still fuzzy on how to calculate this.

Again perhaps reformatting things may help you understand what is happening.
1
2
for (int i = 1; i <= 3; i++)
   cout << "i: " <<  i << endl << "14 % " << i << " = " << 14 % i << endl;


Ah, I see now. Especially on the second problem I asked. Thank you for showing me reformatting it to better understand. :)

With the boolean expression problem this is the order of precedence right?
3 - (8 % 5) == 6 - (12 / 2)

Modulus operator and division get precedence. But even if I calculate both sides I still get 1.4==0. Can't figure out what I'm doing wrong there. Am I thinking about it the wrong way?
But even if I calculate both sides I still get 1.4==0.

Don't forget that you're doing integer math, there are no fractions. Again perhaps if you reformatted the output you may be able to better understand what is going on.

std::cout << "3 - 8 % 5 is equal to: 3 - " << (8 % 5) << " which is equal to: " << (3 - 8 % 5) << std::endl;

When you don't understand complex formulas it is usually easier to break the complex formula into it's intermediate calculations.


With the boolean expression problem this is the order of precedence right?


Correct.

Okay I see what my problem is. I just don't understand modulators. I need to better learn those.
So the left side of 3 - (8 % 5) == 6 - (12 / 2) is 3-3 so it equals 0. I see why the right side is 0 but because I don't understand modulus operators I couldn't get that (8%5) is 3. I need to research that more.
The result of the modulo operator is the remainder after division. So if you divide 8 by 5 you have 1 and a remainder of 3 (8 minus 5).

Wow. I was making this more complicated than it needs to be. I understand now completely. I was using it like regular division so I was getting 1.6. That's gonna help me a lot now that I understand that. Thank you very much! I understand how the third problem works now as well. You've been very helpful, thank you so much :D
Topic archived. No new replies allowed.