CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium
A developer is creating a program where a specific block of code needs to be executed only if two separate conditions are both true. Which type of logical operator should be used to combine these conditions?
- AAND
- BXOR
- CNOT
- DOR
Show answer & explanationAnswer & explanation
Correct answer: A. AND
The AND logical operator is used to combine two or more conditions, returning true only if all individual conditions are true. This precisely matches the requirement for code to execute only when 'two separate conditions are both true'.
Why the other options are wrong
- B. XOR returns true if exactly one condition is true, not both.
- C. NOT negates a single condition.
- D. OR returns true if at least one condition is true.
Logical AND Operator
The logical AND operator (often represented as && or AND) combines two or more Boolean expressions and returns true only if all of the expressions are true.
- Evaluates to true if and only if all operands are true.
- Used to create compound conditions.
- Common in conditional statements (if statements) and loops.
Memory trick: Logic gates open only when conditions are met.