Booleans & Logic
Booleans are data types that can only have two values: true or false. They are essential for making decisions in your programs and controlling program flow.
Boolean Values
Rui has two boolean values:
suppose isTrue = true
suppose isFalse = false
write("True: " + isTrue)
write("False: " + isFalse)Boolean Expressions
Boolean expressions are expressions that evaluate to true or false:
suppose age = 18
suppose hasLicense = true
// Comparison expressions
suppose isAdult = age >= 18 // true
suppose canDrive = age >= 16 and hasLicense // true
suppose isTeenager = age >= 13 and age < 20 // trueLogical Operators
Rui supports three logical operators for working with booleans:
AND Operator (and)
The and operator returns true only if both operands are true:
suppose a = true
suppose b = false
suppose result1 = a and a // true
suppose result2 = a and b // false
suppose result3 = b and b // false
write("true and true: " + result1)
write("true and false: " + result2)
write("false and false: " + result3)OR Operator (or)
The or operator returns true if at least one operand is true:
suppose a = true
suppose b = false
suppose result1 = a or a // true
suppose result2 = a or b // true
suppose result3 = b or b // false
write("true or true: " + result1)
write("true or false: " + result2)
write("false or false: " + result3)NOT Operator (not)
The not operator reverses the boolean value:
suppose a = true
suppose b = false
suppose result1 = not a // false
suppose result2 = not b // true
write("not true: " + result1)
write("not false: " + result2)Truth Tables
Here are the truth tables for logical operators:
AND Truth Table
| A | B | A and B |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
OR Truth Table
| A | B | A or B |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
NOT Truth Table
| A | not A |
|---|---|
true | false |
false | true |
Complex Boolean Expressions
You can combine multiple logical operators:
suppose age = 25
suppose hasLicense = true
suppose hasCar = false
suppose hasJob = true
// Complex conditions
suppose canDrive = age >= 18 and hasLicense
suppose canAffordCar = hasJob and (age >= 18 or hasLicense)
suppose needsTransportation = not hasCar and hasJob
write("Can drive: " + canDrive)
write("Can afford car: " + canAffordCar)
write("Needs transportation: " + needsTransportation)Boolean Functions
Functions can return boolean values:
define isEven(number) {
return number % 2 == 0
}
define isPositive(number) {
return number > 0
}
define isInRange(number, min, max) {
return number >= min and number <= max
}
// Use the functions
write("Is 8 even? " + isEven(8))
write("Is 7 even? " + isEven(7))
write("Is 5 positive? " + isPositive(5))
write("Is -3 positive? " + isPositive(-3))
write("Is 15 in range 10-20? " + isInRange(15, 10, 20))Boolean in Conditionals
Booleans are commonly used in if statements and loops:
suppose isRaining = true
suppose hasUmbrella = false
if (isRaining and not hasUmbrella) {
write("You'll get wet!")
} else if (isRaining and hasUmbrella) {
write("You're prepared for the rain")
} else {
write("No need for an umbrella")
}Example: User Authentication
Here's an example of boolean logic for user authentication:
define checkLogin(username, password) {
suppose correctUsername = "admin"
suppose correctPassword = "password123"
suppose usernameValid = username == correctUsername
suppose passwordValid = password == correctPassword
return usernameValid and passwordValid
}
// Test the login function
suppose username = "admin"
suppose password = "password123"
if (checkLogin(username, password)) {
write("Login successful!")
} else {
write("Invalid username or password")
}Example: Grade Checker
Here's an example that uses boolean logic to check grades:
define isPassingGrade(score) {
return score >= 60
}
define isHonorRoll(score) {
return score >= 90
}
define canGraduate(score, attendance) {
return isPassingGrade(score) and attendance >= 0.8
}
// Check student status
suppose studentScore = 85
suppose attendanceRate = 0.9
write("Student Results:")
write("Passing grade: " + isPassingGrade(studentScore))
write("Honor roll: " + isHonorRoll(studentScore))
write("Can graduate: " + canGraduate(studentScore, attendanceRate))Example: Weather App
Here's a weather app that uses boolean logic:
define shouldWearJacket(temperature, isRaining) {
return temperature < 15 or isRaining
}
define shouldStayInside(temperature, isRaining, windSpeed) {
return temperature < 0 or (isRaining and windSpeed > 30)
}
// Weather conditions
suppose temperature = 12
suppose isRaining = true
suppose windSpeed = 25
write("Weather Advisory:")
write("Wear jacket: " + shouldWearJacket(temperature, isRaining))
write("Stay inside: " + shouldStayInside(temperature, isRaining, windSpeed))Boolean Short-Circuiting
Rui uses short-circuit evaluation for logical operators:
- For
and: If the first operand isfalse, the second operand is not evaluated - For
or: If the first operand istrue, the second operand is not evaluated
define expensiveOperation() {
write("This is expensive!")
return true
}
// Short-circuiting example
suppose result1 = false and expensiveOperation() // expensiveOperation() is not called
suppose result2 = true or expensiveOperation() // expensiveOperation() is not calledNext Steps
Now that you understand booleans and logic, learn about CLI Usage to run Rui programs from the command line.