Like Tree5Likes
  • 1 Post By std10093
  • 1 Post By GReaper
  • 1 Post By std10093
  • 1 Post By iMalc
  • 1 Post By Elysia

2 Questions !

This is a discussion on 2 Questions ! within the C++ Programming forums, part of the General Programming Boards category; Hi Guys , i have 2 questions ... i appreciate u if u answer ... 1. In "If&Else" when we ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Exclamation 2 Questions !

    Hi Guys , i have 2 questions ... i appreciate u if u answer ...

    1. In "If&Else" when we use "==" istead of "= " The compiler behave the same ... do u know why ??

    2. and what is the preference of ++ ? (instead we plus 1)

    SenioRija
    Tnx !
    Last edited by Seniorija; 10-27-2012 at 07:47 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    1. One equal sign is for assignment.Two for comparison.
    example
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
          int a;
          a = 5; // assignment
          if(a == 5) {
                cout<<"a is five"<<endl;
          }
          if( a = 10 ) {
                cout<<"An assignment was just performed!!!Not a comparison!"<<endl;
          }
          return 0;
    }
    2.What do you mean by preference?
    It following are equivalent
    Code:
    a++;
    a = a + 1;
    a+= 1;
    You should write what makes you feel more comfortable The ++ and -- give a shorter code
    Seniorija likes this.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    1) It doesn't, you think it does. What really happens when you use "=" instead of "==" is that an assignment is performed instead of a comparison.
    2) It's shorter, meaning more compact and as a result less errors may happen, and of course someone may want to use the side effects of it ( pre-increment vs post-increment )
    Seniorija likes this.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Tnx ...

    1.I know that 1 equal sign is for assignment and 2 equal sgin is for comparison ,
    but my question is about if&else ... i mean when i use == istead of = the compiler behave the same ... !! do u know why compiler behave like this and run the program without any Errors ?!!!

    2. I get ur answer ... Tnx so much !

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    The compiler does not behave the same.It does what you tell him to do!
    This not a syntax error,so that the compiler should say something ,but in most cases a logical errors.The compiler thinks that you know what are doing.But it is very usual that people just forget the 2nd equal sign.The compiler can't say something because if you write this
    Code:
    if( n=5) {
        printf("this is going to be printed\n");
    }
    then what is inside the condition is not false,thus it will enter the body of the if.
    It is the programmer's responsibility to use the appropriate operator
    Seniorija likes this.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    There is a difference between the resulting code behaving the same, and the compiler itself behaving the same. The former is false as the two simply do not generate code that does the same thing, and the later depends upon what compiler and what its warning flags/levels are.
    If the warning levels do not permit it to tell you about the possible mistake, then it will just blindly compile the code, because that's what you've entrusted it to do.

    For number 2, you should probably refine your question. There are a bunch of different things you could mean. You could be talking about operator precedence, for example. But under the assumption you are asking why one would use different methods of incrementing over others, well there are several parts to that.
    One is that the following statements on their own all do the same thing:
    Code:
    ++x;
    x++;
    x += 1;
    x = x + 1;
    But when used inside other expresions, the above are not all the same.
    In general, you should prefer to use the first of the above ways of incrementing a variable.
    Seniorija likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Code:
    int n = 10;
    if (n == 5)
    	std::cout << "n is 5!";
    if (n = 5)
    	std::cout << "n is 5!";
    Just a short demonstration. As you run this code, you will see it prints "n is 5", which is clearly not true.
    It happens because the last if assigns 5 to n, then evaluates "n" which is greater than 0, and therefore true.
    Again, they are not the same thing!
    Seniorija likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Some questions
    By Overlord in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2006, 10:41 AM
  3. A few questions...
    By Unimatrix_001 in forum Game Programming
    Replies: 2
    Last Post: 06-14-2003, 07:08 PM
  4. C99 Questions
    By Mister C in forum C Programming
    Replies: 4
    Last Post: 06-07-2003, 01:37 AM
  5. 2 questions
    By myUserName in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2003, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21