Thread: A simple comparison

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    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 ? ? ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The code in the else will be executed.

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You compile it, you run it, you type in two equal numbers and you understand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    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

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    sry mr. salem i canot understand u iam still beginner

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    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.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    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.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    mr. nick753

    thank u very very very much

    i got it

    thx

  9. #9
    Registered User MrMatt027's Avatar
    Join Date
    Nov 2010
    Location
    USA, Florida
    Posts
    21
    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.
    Last edited by MrMatt027; 12-05-2010 at 02:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM