-
A simple comparison
i have a question plz
i made this program to make a Comparison between two values As follows
Code:
#include <iostream>
using namespace std;
int main()
{
// define two integers
int x;
int y;
cout <<"Enter X : " << endl;
cin >> x;
cout <<"Enter Y : " << endl;
cin >> y;
if (x > y)
cout << "X is bigger than Y" << endl;
else
cout << "X is smaller than Y" << endl;
return 0;
}
My Question is :-
What will happen if the two values X and Y are equal ? ? ?
-
The code in the else will be executed.
Jim
-
You compile it, you run it, you type in two equal numbers and you understand.
-
thx mr.jim
but how ? ? ?
the 2 values are equal ?
i can not understand this point cuz i am still still Beginner and i dun have any compiler yet and
this is my assignment plz help me :)
-
sry mr. salem i canot understand u iam still beginner
-
Salem is pretty clear in what he wants you to do, just actually compile the program and try it yourself.
When the program prompts you for numbers, input the same number twice.
-
He says he doesn't have a compiler yet guys, jeez. He just wants someone to explain why the else will be executed.
Anyway, the else will be executed if the two values are equal because the if will ONLY be executed IF(x > y). If anything ELSE other than (x > y) then it will print x is smaller than y. That's the way the compiler is reading your program because that's the way it's set up. There are only two options, if(x > y) print "blaha blah" ANYTHING ELSE- print "something else."
BTW- Google bloodshed dev c++ or code::blocks, they are both free C++ IDE's.
-
mr. nick753
thank u very very very much
i got it
thx :)
-
Code:
else if (x == y)
cout << "X and Y are both equal" << endl;
Seems like it needs this lol.
Thanks nick753, I was also wondering about this same thing earlier.