Output writing – If-else ambiguities – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #CPP#3249    siteicon   siteicon  

Problem Statement - Output writing – If-else ambiguities

Write output of the following code. Assume that required header files are included.

int main()
{
  int x=10,y=20;
  if(x=0) cout<<"Hello"<<endl;
  else cout<<"Sorry"<<endl;
  if(y=10) cout<<"Hello"<<endl;
  else cout<<"Sorry"<<endl;
  if(x+y==10) cout<<"Hello"<<endl;
  else cout<<"Sorry"<<endl;
  return 0;
}

Solution

TC++ #3249

 

Run Output

Sorry
Hello
Hello

Notes

int x=10,y=20;  //Initial values

if(x=0) cout<<“Hello”<<endl; else cout<<“Sorry”<<endl;
Here instead of comparison only assignment is done. Since by assignment there is zero value put so the selection answer would be false. In c++ any value of 0 is considered as equivalent to false. So it will go to the else condition Sorry would be printed.

if(y=10) cout<<“Hello”<<endl; else cout<<“Sorry”<<endl;
Here again assignment is there but this time it as positive value being assigned. So it will assume the condition to be true and Hello will be printed.

if(x+y==10) cout<<“Hello”<<endl; else cout<<“Sorry”<<endl;
Now x has become 0 and y has become 10 due to previous two assignments within the if condition checks.  So the condition x+y==10  will be true. Here addition will occur before assignment. Now Hello would be printed.

 

Common Errors

  • Student often forget to watch that there is assignment happening in first two if conditions and comparison is just not happening.

 


Share

sunmitra| Created: 2-Feb-2018 | Updated: 18-Dec-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back