Why is my if statement executing when its false

  • Thread starter Thread starter ProPatto16
  • Start date Start date
  • Tags Tags
    If statement
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the execution of an if statement in Java. Participants explore the behavior of boolean variables and the distinction between assignment and comparison within conditional statements.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes a problem where an if statement executes unexpectedly, despite the boolean variable being set to false.
  • Another participant points out that the issue arises from using a single equals sign for assignment instead of a double equals sign for comparison in the if statement.
  • A further explanation clarifies the difference between assignment and comparison, illustrating how assignment can lead to unintended behavior in conditional statements.
  • A participant acknowledges the simplicity of the solution once identified.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the problem being related to the misuse of assignment versus comparison in the if statement.

Contextual Notes

The discussion does not delve into the broader implications of this issue or explore potential edge cases related to boolean logic in Java.

ProPatto16
Messages
323
Reaction score
0
quick question, not sure why i can't work this out.
in the following code my if statement executes, but as far as i can see i have set the boolean so that it shouldnt.
Code:
public void actionPerformed(ActionEvent e)
               {
                   boolean contains = false; 
                   for (int i = 0; i < studentStrings.size(); i++)
                    {
                        if( studentStrings.get(i).contains( enterID.getText()))
                        {
                            System.out.println( " get constains executed");
                            contains = true;
                        }
                    }
                    if ( contains = true)
                    {
                        updateOldRecord();
                        System.out.println( "if executed");
                    }
                    else
                    {
                        newRecord();
                        System.out.println( "else executed");
                    }

you can see there are 2 ifs and one else. When and if the first one executes it should set the boolean contains to true. then if boolean contains is true my second if should execute and not my else. and if the first if doesn't execute the boolean should still be false so my else will execute. the system.print methods are just so i can see which ones are executing.

in the for statement the idea is i look for a string in the arrayList that is entered into a student ID text field. if the ID already exists it updates that student, if not it creates a new record.

the problem is whether or not that first if statement executes, only my second if executes, never the else.

so if my jtextfield has an existing ID number executed i get

get contains executed
if executed

and if it does not contain an existing ID then boolean should remain false and then only execute the else but i get

if executed.


i don't know why my if statement arguments arent working?

thanks
 
Physics news on Phys.org
You are missing an equals sign. If (contains = true) sets contains equal to true and then tests whether it is true. If (contains == true) just tests whether contains is true without changing its value.
 
Code:
There is a difference between assignment and comparison.

The expression of assigning a boolean can be treated as a boolean.

Code:
boolean b = false;

if( b = true) //expression in () sets b to true and evaluates to true
{
    System.out.println( "THIS LINE WOULD GET PRINTED");
}
As an aside, one may write
Code:
if(b == true)
more concisely as
Code:
if(b)
 
Thanks, knew it would be simple!
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
4K