📖 Scenario: You are creating a simple program to check if a person is eligible for a discount based on their age and membership status.
🎯 Goal: Build a Kotlin program that uses Boolean variables and logical operators to decide if a person gets a discount.
📋 What You'll Learn
Create Boolean variables to represent conditions
Use logical operators && (AND), || (OR), and ! (NOT)
Print the final eligibility result
💡 Why This Matters
🌍 Real World
Many apps check user eligibility for offers or access using true/false conditions combined with AND, OR, and NOT.
💼 Career
Understanding Boolean logic is essential for programming decisions, validations, and controlling program flow.
Progress0 / 4 steps
1
Create Boolean variables for age and membership
Create two Boolean variables called isAdult and isMember. Set isAdult to true and isMember to false.
Kotlin
Hint
Use val to create Boolean variables and assign true or false.
2
Create a Boolean variable for discount eligibility
Add a Boolean variable called hasDiscount that is true if the person is an adult AND a member. Use the && operator with isAdult and isMember.
Kotlin
Hint
Use val hasDiscount = isAdult && isMember to combine the two conditions.
3
Add a condition for senior discount using OR operator
Create a Boolean variable called isSenior and set it to true. Then update hasDiscount to be true if the person is an adult AND a member OR if the person is a senior. Use || operator.
Kotlin
Hint
Use parentheses to group isAdult && isMember before OR isSenior.
4
Print the discount eligibility result
Write a println statement to display the value of hasDiscount.
Kotlin
Hint
Use println(hasDiscount) to show the result.
Practice
(1/5)
1. What type of value does the Kotlin Boolean type store?
easy
A. Decimal numbers
B. True or false values
C. Text strings
D. Numbers only
Solution
Step 1: Understand the Boolean type
The Boolean type in Kotlin is designed to hold only two possible values: true or false.
Step 2: Identify the correct value type
Since Boolean stores true or false, it cannot store numbers or text strings.
Final Answer:
True or false values -> Option B
Quick Check:
Boolean = true/false [OK]
Hint: Boolean means true or false only [OK]
Common Mistakes:
Thinking Boolean stores numbers
Confusing Boolean with String
Assuming Boolean can hold multiple values
2. Which of the following is the correct syntax for the logical AND operator in Kotlin?
easy
A. &&
B. ||
C. and
D. !
Solution
Step 1: Recall Kotlin logical operators
Kotlin uses && for logical AND, || for OR, and ! for NOT.
Step 2: Match operator to AND
The operator && combines two Boolean expressions and returns true only if both are true.
Final Answer:
&& -> Option A
Quick Check:
AND operator = && [OK]
Hint: AND is double ampersand && in Kotlin [OK]
Common Mistakes:
Using || instead of && for AND
Writing 'and' as a keyword
Confusing NOT (!) with AND
3. What is the output of the following Kotlin code?
val a = true
val b = false
println(a || b && !b)
medium
A. false
B. null
C. true
D. Compilation error
Solution
Step 1: Evaluate NOT operator
!b means !false, which is true.
Step 2: Evaluate AND operator
b && !b means false && true, which is false.
Step 3: Evaluate OR operator
a || (result of AND) means true || false, which is true.
Final Answer:
true -> Option C
Quick Check:
Logical operators follow precedence: !, &&, then || [OK]