Understanding logic behind several if statements

Hey everyone! firstly i just want to apologize in advance if what I'm about to ask is a dumb question. I am new to c++ programming and this forum. I'm trying to learn c++ on my own from home and I am currently reading "how to program, 8th edition" by Dietel.

I'm having trouble with one of the exercises, you can see the code below. I am having trouble understanding the logic involving the 6 if statements. They are suppose to output one smallest number and one largest number. I just don't understand the logic behind it if someone can possibly dumb it down for me so I could better understand it. Thank you so much.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
  int x, y, z;
   int sum, average, product, smallest, largest;
 
  cout << "Input three different integers: ";
  cin  >> x >> y >> z;
 
   sum = x + y + z;
  average = (x + y + z)/3;
  product = x * y * z;
 
   cout << "Sum is " << sum << endl;
   cout << "Average is " << average << endl;
   cout << "Product is " << product << endl;
 
   if ((x<y)&&(x<z))
     cout << "Smallest is " << x << endl;
   if ((y<x)&&(y<z))
     cout << "Smallest is " << y << endl;
   if ((z<x)&&(z<y))
     cout << "Smallest is " << z << endl;
   if ((x>y)&&(x>z))
     cout << "Largest is " << x << endl;
   if ((x<y)&&(z<y))
     cout << "Largest is " << y << endl;
   if ((z>y)&&(z>x))
     cout << "Largest is " << z << endl;
 
   return 0;
}
I suppose if we are given three numbers, we scan across them and figure out which is the smallest and which the largest, without really thinking about the actual process.

In effect, the computer is only able to compare two numbers at a time. So we get comparisons such as if (x<y). Now if we find that is a true statement, then x might be the smallest of the three. But the only way to be certain is to now compare x with z. If (x<z) as well, then it is indeed the smallest. Hence we combine these two tests using the logical and operator && like this:
1
2
   if ((x<y)&&(x<z))
     cout << "Smallest is " << x << endl;

The next two if statements are exactly the same, but are checking the other two possibilities.

The last three tests use the same idea, but testing for biggest, not smallest.

One problem with this code is that if two or more of the numbers are the same, it may not report anything. This may or not be the desired behaviour.
Last edited on
yes, the code is looking for min/max numbers from user input...

1
2
3
if ((x<y)&&(x<z))
     cout << "Smallest is " << x << endl;
// etc... 



purpose of the above code is wrong, what if user inputs same numbers?
ie. x=1, y=1, z=1

there should also be an else statement printing an output saying that numbers are the same.

for example:
1
2
3
4
if ((x<y)&&(x<z))
     cout << "Smallest is " << x << endl;
else if ((y == z))
     cout << "Smallest is" << y << "AND" << z << endl;
The best way to understand code is to get a paper and pencil, make up 3 random integers and follow the logic

lets say x = 5, y = 15, z = 9

if ((x<y)&&(x<z))
cout << "Smallest is " << x << endl;
= if (5 < 15) and (5 < 9)


do you know the truth table behind && and ||? && is saying, both conditions MUST be true for this if statement to execute, so in this case, 5 < 15 and 5 < 9 are both true, so it will say that x is the smallest

lets say that y = 4, then we have ( 5 < 4 ) && ( 5 < 9), even though the second statement is true, the first one isn't, which means the whole condition is false

if you use ||, then only one condition needs to be true for the whole condition to be true



Topic archived. No new replies allowed.