• Register
1 vote


I have a sample c++ code below that throws me back an error: “c++ expression must be a modifiable value” What could be the reason?

int A = 3; 
int B = 5; 
int match = 3;
for ( int k =0; k < B; k ++ )
    match --; 
    if ( match == 0 && k = A )
         std::cout << " equals" << std::endl;

Thanks for your help.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


You need to remember, the assignment operator has lower precedence than &&, so your condition is comparable as:

if ((match == 0 && k) = a)

But the left-hand side of this is an rvalue, namely the boolean resulting from the evaluation of the sub­expression match == 0 && k, so you cannot assign to it.

On the contrary, the comparison has higher precedence, so match == 0 && k == a is comparable to:

if ((match == 0) && (k == a))

I hope you get the point now.

13 9 6
94,240 points

Related questions

0 votes
1 answer 3 views
Problem: I have this following code: int M = 3; int C = 5; int match = 3; for ( int k =0; k < C; k ++ ) { match --; if ( match == 0 && k = M ) { std::cout << " equals" << std::endl; } } But it gives out an error saying: Error: expression must be a ... trying to modify "match" or "k" value here, but why this error? if I only write it like: if ( match == 0 ) it is ok. Could someone explain it to me?
asked Dec 23, 2020 Mashhoodch 5.6k points
1 vote
2 answers 258 views
Problem: Hello all, How to declare an array properly in c/c++? I am a new student of c++ and I am trying to declare two-dimensional arrays in my program. Unfortunately, my arrays are not working. Here is my sample code int row = 5; int col= 5; int [row][col]; The above codes throw an error expression must have a constant value c++. What&rsquo;s wrong with it?
asked Apr 7, 2020 Gavin 15.3k points
0 votes
1 answer 151 views
Problem : Looking for solution of following doubt if one for loop is nested within another, then the counter variables for the two loops should be
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 6 views
Problem: My programming teacher gave us an assignment in which we have to tell which of the three loops in C++ does not have an entry condition? As far as my guess is concerned, the 'for' loop does not need an entry condition. I want you to guide me if I am wrong and tell me the correct answer to this question. Thanks!
asked Dec 17, 2020 Code Learner 5.5k points
0 votes
1 answer 3 views
Problem: Attention reader! Det hold of all the important Java and Collections concepts with theat a student-friendly price and become industry ready.Please write relevant answer if you find anything correct, or you want to share more information about the topic discussed above.
asked Dec 12, 2020 Mashhoodch 5.6k points
0 votes
1 answer 121 views
Problem: I am working with loops. My query is that can I increment a foor loop by 2? If yes, then how? Please explain in detail and with syntax and examples because I am a newbie and have no experience in programming. Thanks
asked Oct 15, 2020 Code Learner 5.5k points
0 votes
1 answer 35 views
Problem: I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }
asked Dec 26, 2020 Mashhoodch 5.6k points
0 votes
1 answer 545 views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 1.6K views
Problem: I've got a problem with c++ code. I am attaching the sample code snippet below. Please pull me up. Yee, of course, I am pretty new in C++ #include <iostream> using namespace std; class Myclass { public: void myfunc() { cout<<"Hello World"<<endl; } }; int main ... = new Myclass(); // myfunc is part of the class object, rather than // part of the pointer to that class' object. a.myfunc(); }
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 13 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 5.5k points