Menu
CoddyTech

Boolean Conditions

Lesson 4 of 48 in Coddy's Swift for Dart Developers course.

Dart and Swift conditions require Boolean values; neither treats an empty string as false. Both short-circuit && and ||, so the right side may not execute. Swift writes the Boolean type as Bool; Dart writes bool. A logical negation with ! reverses a Boolean, but that same symbol has a different role after an optional expression. Use explicit comparisons when a condition depends on data. Here a door alarm combines a sensor flag and maintenance mode, rather than testing numeric interval boundaries.

Complete Dart program. Each labelled block is a separate file.

main.dart

void main() {
  final open = true;
  final maintenance = true;
  print(open && !maintenance);
  print(!open || maintenance);
}

Complete Swift program. Each labelled block is a separate file.

main.swift

import Foundation
let open = true
let maintenance = true
print(open && !maintenance)
print(!open || maintenance)
challenge icon

Challenge

Easy

Implement inside(_:low:high:) for a closed interval. Input has three integers: value, low, high, each -1000 through 1000 with low no greater than high. Output true or false.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
return inside(7, low: 7, high: 9) && !inside(10, low: 7, high: 9) && inside(9, low: 7, high: 9)
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

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

All lessons in Swift for Dart Developers

Practice on your own: Swift playground