If Statement is Not Working Fine C++ Error [ Solved ]

0
71

These can be the reasons of not working if statement properly in C++.

  1. You are missing comparison operators (==) while comparing something in if statement.
  2. You are not using logical operators properly inside if statement.
  3. Missing curly braces if more than one statements are required to execute after if condition.

Reason 1.  You are missing comparison operators (==) while comparing something in if statement.

Sometimes in programming people forget to use comparison (==) operator inside if statement. Instead of using comparison operator people use assignment operator (=) which is used to assign a value to a variable. See the example below.

if statement not working fine in c++

In the above example the program is working but if statement is not working properly because comparison operator is missing.

Reason 2. You are not using logical operators properly inside if statement.

People who are beginner in programming do not pay attention to understand concepts so they do not use logical operators inside if statement properly. This reason can also cause of not working if statement properly. See the example below.c++ if statement error

In the above example you can see the wrong way of using logical operators. Check below the right way of using logical operator inside if statement.

 

if statement error in c++

Reason 3. Missing curly braces if more than one statements are required to execute after if condition.

If statement can work without curly braces if only one statement is required to execute after if condition. Sometimes people forget this concept that to execute more than one statements after a if condition than the curly braces should be placed. See the example below.

if statement error in c++

Solution

  1. Make sure you are using == sign while comparing inside if statement.
  2. Make sure you are using right way of logical operator inside if statement.
  3. Make sure you are using curly braces of after if condition.

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here