Solving C++ Logical Ops 2: Print "Special Number

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion focuses on correcting a logical expression in C++ to print "Special number" when the variable specialNum is -99, 0, or 44. The correct expression is (specialNum == -99) || (specialNum == 0) || (specialNum == 44). A common mistake highlighted is the use of the assignment operator = instead of the equality operator ==, which leads to incorrect program behavior. The importance of distinguishing between these operators is emphasized for accurate logical comparisons in C++.

PREREQUISITES
  • Understanding of C++ syntax and operators
  • Familiarity with conditional statements in programming
  • Knowledge of variable assignment versus comparison
  • Basic experience with compiling and running C++ programs
NEXT STEPS
  • Review C++ operator precedence and usage
  • Practice writing conditional statements in C++
  • Explore debugging techniques for C++ code
  • Learn about common logical errors in programming
USEFUL FOR

Beginner to intermediate C++ programmers, educators teaching programming concepts, and anyone looking to improve their understanding of logical operations in C++.

ineedhelpnow
Messages
649
Reaction score
0
Write an expression that prints "Special number" if specialNum is -99, 0, or 44.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int specialNum = 0;

   specialNum = 17;
   
   if (<STUDENT CODE>) {
      cout << "Special number" << endl;
   }
   else {
      cout << "Not special number" << endl;
   }

   return 0;
}

i thought it would be
Code:
(specialNum == -99) || (specialNum == 0) || (specialNum = 44)
but its not
 
Technology news on Phys.org
ineedhelpnow said:
Write an expression that prints "Special number" if specialNum is -99, 0, or 44.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   int specialNum = 0;

   specialNum = 17;
   
   if (<STUDENT CODE>) {
      cout << "Special number" << endl;
   }
   else {
      cout << "Not special number" << endl;
   }

   return 0;
}

i thought it would be
Code:
(specialNum == -99) || (specialNum == 0) || (specialNum = 44)
but its not

That last condition is an assignment. You want specialNum == 44, because "specialNum = 44" is a statement that means "assign 44 to specialNum, and return that value". Thus it logically becomes true (as 44 is not zero) and you always get "special number" (and if you printed specialNum it would mysteriously always be 44.

Be careful between = and == :)
 
i literally read over that piece of code i wrote like ten times and i didnt even notice that i missed a '='. thanks bacterius!
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K