Logical OR Operator
Part of the Fundamentals section of Coddy's GO journey — lesson 25 of 109.
The logical OR operator (||) returns true when at least one condition is true.
Create a program that checks if a number is either negative or even:
num := 6
isNegative := num < 0
isEven := num%2 == 0
result := isNegative || isEven
fmt.Println(result)This outputs:
trueThe result is true because num is even, even though it's not negative. Only one condition needs to be true for the OR operator to return true.
Challenge
BeginnerIn this challenge, you'll practice using the logical OR operator (||) in Go.
You have variables representing whether a student has completed their homework and whether they've completed their extra credit assignment. A student passes the class if they've completed either their homework or their extra credit assignment (or both).
Your task is to use the logical OR operator to determine if the student passes the class.
Cheat sheet
The logical OR operator (||) returns true when at least one condition is true.
num := 6
isNegative := num < 0
isEven := num%2 == 0
result := isNegative || isEven
fmt.Println(result) // trueThe result is true because num is even, even though it's not negative. Only one condition needs to be true for the OR operator to return true.
Try it yourself
package main
import "fmt"
func main() {
// These variables track whether assignments are complete
homeworkComplete := false
extraCreditComplete := true
// TODO: Use the logical OR operator (||) to determine if the student passes
// A student passes if either homework OR extra credit is complete
passesClass :=
// Display the result
fmt.Println("Homework complete:", homeworkComplete)
fmt.Println("Extra credit complete:", extraCreditComplete)
fmt.Println("Student passes class:", passesClass)
}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