C program - trouble with IF statement

In summary, this conversation highlights a problem with an "if" statement in a program, where the desired output is not being printed correctly. The solution to this problem is to use more brace brackets in the code. Additionally, it is important to make sure that the "if" statement is using double equals (==) and not single equals (=). Using a debugger can also be helpful in identifying and solving coding issues. Finally, in this particular case, the problem was resolved by creating a new file and pasting the code back in.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
I feel silly asking this but I can't get my "If" statement to work correctly. This should print "a is NOT equal to b" but it prints "a is equal to b".
Any help? Thanks!

#include <stdio.h>
int main(void)
{
int a = 10;
int b = 15;

if(a == b)
printf("a is equal to b \n");
else
printf("a is NOT equal to b \n");

return 0;
}
 
Physics news on Phys.org
  • #2
Try using more brace brackets

#include <stdio.h>
int main(void)
{
int a = 10;
int b = 15;

if(a == b)
{printf("a is equal to b \n");}
else
{printf("a is NOT equal to b \n");}

return 0;
}
 
  • #3
works for me:

Code:
#include <stdio.h>

int main(void)
{
int a = 10;
int b = 15;

if(a == b)
printf("a is equal to b \n");
else
printf("a is NOT equal to b \n");

return 0; 
}


$ gcc _test.c

$ ./a.exe
a is NOT equal to b
 
  • #4
Another tip: Make sure you are have a == b and not a = b in the code.
 
  • #5
One more thing: Get a debugger. You can see what is happening when you step through the code using a debugger than using any other method.
 
  • #6
e(ho0n3 said:
Another tip: Make sure you are have a == b and not a = b in the code.

I do have double equals. I cut and pasted exactly what I am trying to run.

Thanks.
 
  • #7
robphy said:
works for me:

bizarre! I create a new.c file and pasted the code back in and now the logic works fine!
I guess it will remain a mystery!

thanks for running that on your end, robphy.
 
  • #8
AKG said:
Try using more brace brackets

I'll keep that in mind - thanks!
 

Related to C program - trouble with IF statement

1. Why is my IF statement not executing even though the condition is met?

There could be several reasons for this. One possible reason is that the code in the IF statement is not correctly indented, causing it to be outside the scope of the IF statement. Another reason could be that there is a logical error in the condition itself, causing it to always evaluate to false. It is also possible that there is a syntax error in the IF statement that is preventing it from executing. Check your code carefully to identify and fix any of these potential issues.

2. How can I check if multiple conditions are met in an IF statement?

To check for multiple conditions in an IF statement, you can use logical operators such as "&&" (AND) or "||" (OR). For example, if you want to execute a code only if both condition A and condition B are true, you can use the syntax "if (condition A && condition B) { // code to execute }". If you want to execute a code if either condition A or condition B is true, you can use "if (condition A || condition B) { // code to execute }".

3. Can I have multiple IF statements in a single code block?

Yes, you can have multiple IF statements in a single code block. This is known as nesting IF statements. However, it is important to use proper indentation and curly braces to ensure that the code is executed correctly. Each IF statement should have its own set of curly braces to indicate the code that should be executed if the condition is met.

4. How can I handle situations where the condition in an IF statement is not met?

To handle situations where the condition in an IF statement is not met, you can use an ELSE statement. This allows you to specify a block of code to execute if the condition is not met. For example, you can use the syntax "if (condition) { // code to execute } else { // code to execute if condition is not met }". This way, your program can handle different possible scenarios based on the condition.

5. What is the difference between the IF statement and the ELSE IF statement?

The IF statement is used to check a single condition and execute a block of code if that condition is met. However, in some cases, you may want to check for multiple conditions and execute different code blocks based on those conditions. This is where the ELSE IF statement comes in. It allows you to specify additional conditions to check if the first condition is not met, and execute corresponding code blocks based on those conditions. Think of it as a series of IF statements, with each one being checked only if the previous one was not met.

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
782
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
771
  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
960
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
692
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top