Here's a very detailed introduction to if
, else
, else if
, ternary operator (? :
), and switch
statements in Dart using DartPad, with solved and unsolved problems for practice.
🧠 Introduction to Decision-Making in Dart
In programming, decision-making allows your program to take different paths based on conditions. Dart provides several ways to perform this:
-
if
-
else if
andelse
-
Ternary Operator (
condition ? trueResult : falseResult
) -
switch
statement
We'll explain each in simple English, give syntax, examples, and visuals, and provide problems at the end.
🧩 1. if
Statement
Used to execute code only when a condition is true.
📦 Syntax
if (condition) { // code if condition is true}
// code if condition is true}
✅ Example
void main() { int age = 20; if (age >= 18) { print('You are an adult.'); }}
int age = 20; if (age >= 18) { print('You are an adult.'); }}
🧩 2. else
and else if
Used when you want to provide alternative paths.
📦 Syntax
if (condition1) { // true block} else if (condition2) { // another condition} else { // default}
// true block} else if (condition2) { // another condition} else { // default}
✅ Example
void main() { int marks = 65; if (marks >= 90) { print('Grade: A'); } else if (marks >= 75) { print('Grade: B'); } else if (marks >= 60) { print('Grade: C'); } else { print('Fail'); }}
int marks = 65; if (marks >= 90) { print('Grade: A'); } else if (marks >= 75) { print('Grade: B'); } else if (marks >= 60) { print('Grade: C'); } else { print('Fail'); }}
🧩 3. Ternary Operator
Used for shorter if-else logic when assigning or returning values.
📦 Syntax
condition ? valueIfTrue : valueIfFalse;
condition ? valueIfTrue : valueIfFalse;
✅ Example
void main() { int age = 16; String type = (age >= 18) ? 'Adult' : 'Minor'; print(type); // Output: Minor}
int age = 16; String type = (age >= 18) ? 'Adult' : 'Minor'; print(type); // Output: Minor}
🧩 4. switch
Statement
Useful when comparing a single value against multiple constant cases.
📦 Syntax
switch (variable) { case value1: // code break; case value2: // code break; default: // default code}
case value1: // code break; case value2: // code break; default: // default code}
✅ Example
void main() { String grade = 'B'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; case 'C': print('Average'); break; default: print('Fail or Unknown grade'); }}
String grade = 'B'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; case 'C': print('Average'); break; default: print('Fail or Unknown grade'); }}
🎯 DartPad Practice
Use this link to try: https://dartpad.dev
🧠 Solved Problems
1. Check if a number is positive, negative, or zero
void main() { int num = 0; if (num > 0) { print('Positive'); } else if (num < 0) { print('Negative'); } else { print('Zero'); }}
int num = 0; if (num > 0) { print('Positive'); } else if (num < 0) { print('Negative'); } else { print('Zero'); }}
2. Find the largest of three numbers
void main() { int a = 10, b = 25, c = 15; if (a >= b && a >= c) { print('$a is the largest'); } else if (b >= a && b >= c) { print('$b is the largest'); } else { print('$c is the largest'); }}
int a = 10, b = 25, c = 15; if (a >= b && a >= c) { print('$a is the largest'); } else if (b >= a && b >= c) { print('$b is the largest'); } else { print('$c is the largest'); }}
3. Use a switch to print day name from number (1-7)
void main() { int day = 3; switch (day) { case 1: print('Monday'); break; case 2: print('Tuesday'); break; case 3: print('Wednesday'); break; case 4: print('Thursday'); break; case 5: print('Friday'); break; case 6: print('Saturday'); break; case 7: print('Sunday'); break; default: print('Invalid day'); }}
int day = 3; switch (day) { case 1: print('Monday'); break; case 2: print('Tuesday'); break; case 3: print('Wednesday'); break; case 4: print('Thursday'); break; case 5: print('Friday'); break; case 6: print('Saturday'); break; case 7: print('Sunday'); break; default: print('Invalid day'); }}
❓ Unsolved Practice Problems (Try in DartPad)
1. Write a program that checks if a year is a leap year.
Hint: Leap year if divisible by 4 and (not divisible by 100 or divisible by 400).
Hint: Leap year if divisible by 4 and (not divisible by 100 or divisible by 400).
2. Accept a number and print if it's even or odd using ternary operator.
3. Based on temperature input, print:
-
"Cold"
if < 15
-
"Warm"
if 15–30
-
"Hot"
if > 30
"Cold"
if < 15
"Warm"
if 15–30
"Hot"
if > 30
4. Use a switch to display months with 31, 30, or 28/29 days. Input is month name.
5. Ask for marks and print:
-
90+ → Excellent
-
75-89 → Good
-
60-74 → Average
-
below 60 → Needs Improvement
90+ → Excellent
75-89 → Good
60-74 → Average
below 60 → Needs Improvement
Use if-else if
.