Question

My question that needs an answer is this: Can the C++ compilers catch the mistake of...

My question that needs an answer is this:

Can the C++ compilers catch the mistake of using an "=" instead of the comparison operator "==".

Show code how you can test this using an if statement in C++, write a small program to demonstrate it.

If the compiler does not catch the mistake how would one go about ensuring they are using the correct syntax for the comparison operator?

Thank you

Homework Answers

Answer #1
  • = is an assignment operator which is used to assign a value to a variable
  • == is a conditional operator which is used to check if both the variables/ values are equal
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 20;
        int y = 18;
        if (x = y) //TYPO
        {
          cout << "x is equal to y";
        }
        else
         cout << "x is not equal to y";
        cout <<x;
        cout << y;
        
    
        return 0;
    }
  • Consider the above program, there we have a typo in if condition. I used '=' in place of '=='
  • Whenever if-statement gets executed, x is assigned the value of y and until y is not equal to zero we get the statements inside if clause executed. The output of the program will be as follows
  • x is equal to y 18 18
  • if we make the value of y= 0 then the program will inside if clause x is assigned the value of y(which is 0) and as if(x) is zero we get x is not equal to y as output.
  • Running a modern compiler at the highest warning level will cause it to issue a warning when an assignment is used in a conditional statement or a note that the statement does nothing when an equality test is used instead of an assignment outside of a conditional. This is one issue that is essentially fixable -- if you use the higher warning levels.

  • So programmers must be careful while using == operator.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q.6) What does it mean if you compile your C# source code file and you only...
Q.6) What does it mean if you compile your C# source code file and you only get copyright information about Microsoft as a response? a) You have not set the path to the compiler correctly. b) Your program has syntax errors that you must correct before you can compile the program. c) The compiler cannot find the source code file that you are trying to compile. d) The compiler successfully compiled your program. Q.4) To compile a C# source code...
This is my assignment with using C++ programming. Im not good at C++ programming so can...
This is my assignment with using C++ programming. Im not good at C++ programming so can you write a Remark of each Code? Thank you. -Suppose a car has an identifier (carid) and consists of an engine(Engine) and a body (Body). -Engine is an integer type and has engine number (engineNo) and string type engine name (engineName). -Body is an integer type and has a body number (bodyNo) and a string type body name (bodyName). -Write a program that creates...
UNITY QUESTION I am working with my team in Unity to create a character. I am...
UNITY QUESTION I am working with my team in Unity to create a character. I am responsible for creating a character's moves. Nothing really has been done yet, but could you provide an example of code using C# or C++ or even pseudocode Please provide functions for my character to go walk, run, move up, move down, move forward, and move backwards. I will give a thumbs up if it is decent :) Just give me an example code for...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code without testing it. I will downvote your answer and mark as spam.** I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!! In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this...
This problem requires using R studio. This question is from Verzani (Problem 2.15) Can some write...
This problem requires using R studio. This question is from Verzani (Problem 2.15) Can some write the code for the answer to the question below: The negation operator ! is used with reverse Boolean values. For example: A <- c(True, False, True, False) !A ##[1] False True False False One of De Morgan's laws in R code is ! (A & B) = !A | !B . Verify this with B <- c(False, True, False, True) and A as above.
Hi, there. A question about web development. I'm using the jquery to try the reset method...
Hi, there. A question about web development. I'm using the jquery to try the reset method in an input form in the PHP file but it didn't work. Here is a part of my code. <div id='signbox' class='modal-window'> <form id= 'signF' method='post' action='centerControl.php'> ... <input id='resetSign' type='button' value ='Reset'>; ... </form> </div> <script> $(document).ready(function(){ $("#resetSign").click(function(){ $("#signF")[0].reset() }); }); </script> And one more question that I'm a new player so what kind of tool I can check all my jquery PHP...
In this module you learned about making decisions. You learned about the syntax and rules used...
In this module you learned about making decisions. You learned about the syntax and rules used to develop programs containing decisions and how the logic can make your programs more robust. Draw a flowchart for a program that shows the logic for a program that generates a random number. The program will simulate tossing coin. The program should generate a random number(0 and 1). If the random number is 0, then it should display the word "Heads". If the random...
The following question can be answered in R code (using R-Studio or a program of your...
The following question can be answered in R code (using R-Studio or a program of your choice). Load the “star” data from the “faraway” package, and model “temp” using “light”. One may also suspect that the residuals (use all the data) follow a t distribution instead of normal. Using the same logic that we produced the QQ plot for comparing to the normal distribution, produce a QQ plot for comparing to a t distribution. You need to decide the degrees...
Hello. for one of my assignment, I need to create any program using the C language...
Hello. for one of my assignment, I need to create any program using the C language and that must contain the following: - Error Checking - Use of Functions - Menu system - Array Processing It can be about anything, nothing specified but must contain the features mentioned above Thank you
Hey. Please, can you check my work? Thank you Question 2 1.     During the planning phase...
Hey. Please, can you check my work? Thank you Question 2 1.     During the planning phase of the writing process, which of the following will help you prioritize informational needs and focus on the most important questions? 1. Statement of purpose and audience needs 2. Analytics matrix 3. Executive dashboard 4. Statement of purpose 5. Work timeline Question 4 1.     Which of the following is a shortcoming of conventional search engines? 1. Results are not evaluated by human editors. 2....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT