Logical AND Operator
Part of the Fundamentals section of Coddy's GO journey — lesson 24 of 109.
The logical AND operator (&&) returns true only when both conditions are true.
Here is a program that checks if a number is both positive and even:
num := 6
isPositive := num > 0
isEven := num % 2 == 0
result := isPositive && isEven
fmt.Println(result)This outputs:
trueYou can also use the operator directly in conditions:
age := 25
hasLicense := true
canDrive := age >= 18 && hasLicense
fmt.Println(canDrive)This outputs:
trueChallenge
BeginnerIn this challenge, you'll practice using the logical AND operator (&&) in Go.
You have variables representing whether a person meets different eligibility criteria for a job. A person is qualified for the job if they have both the required experience AND the necessary education.
Your task is to use the logical AND operator to check if the candidate is qualified, and store the result in the isQualified variable.
Cheat sheet
The logical AND operator (&&) returns true only when both conditions are true.
Basic usage with variables:
num := 6
isPositive := num > 0
isEven := num % 2 == 0
result := isPositive && isEven
fmt.Println(result) // trueDirect usage in conditions:
age := 25
hasLicense := true
canDrive := age >= 18 && hasLicense
fmt.Println(canDrive) // trueTry it yourself
package main
import "fmt"
func main() {
// Candidate qualifications
hasExperience := true
hasEducation := true
// TODO: Use the logical AND (&&) operator to check if the candidate
// has both experience AND education, store the result in isQualified
isQualified := false // Replace this line with your code
// Print the result
fmt.Println("Does the candidate qualify for the job?", isQualified)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions