Menu
Coddy logo textTech

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:

true

The 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 icon

Challenge

Beginner

In 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) // true

The 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)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals