-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_statements.dart
103 lines (85 loc) · 3.73 KB
/
conditional_statements.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This program demonstrates conditional statements and switch statements in Dart.
import 'dart:io';
void main() {
print(
"\n******************************** Conditional statements ********************************\n");
// Explain conditional statements
print(
"\nConditional statements allow you to control program flow based on certain conditions.\n");
print(
"If a condition is true, a specific block of code executes. Otherwise, another block might execute.\n\n");
// ---------- if-else statement ----------
print("*************** if else statement ********************\n");
print(
"This statement checks a single condition and executes corresponding code.\n");
// Example program
print("****** Example Program ******\n\n");
int firstNumber = 34;
int secondNumber = 23;
if (firstNumber > secondNumber) {
print(
"First number ($firstNumber) is greater than second number ($secondNumber).\n"); // Explain the values
} else {
print(
"Second number ($secondNumber) is greater than or equal to first number ($firstNumber).\n"); // Explain the values
}
// ---------- Nested if-else statement ----------
print("************** nested if else statement **********************\n\n");
print(
"Nested if-else allows for more complex logic by checking multiple conditions within a single statement.\n");
// Example program: Voting eligibility
print("Please enter your age to check if you are eligible to vote or not!\n");
int myAge = int.parse(stdin.readLineSync()!);
int allowedAge = 18;
if (myAge >= allowedAge) {
print(
"Your age is $myAge and you are allowed to vote.\n"); // Explain the output
} else if (myAge == 0) {
print("Please enter a valid age.\n"); // Explain the output
} else {
print(
"Your age is $myAge and you are not allowed to vote yet.\n"); // Explain the output
}
// ---------- Switch statement ----------
print(
"\n******************************** Switch Statement ********************************\n");
print(
"\nA switch statement executes code based on the value of an expression compared against different cases.\n");
// Example program: Scholarship eligibility
print("**************Example Program ******************\n");
print(
"Please enter your CGPA to check if you are eligible for the scholarship or not!\n");
int cGPA = int.parse(stdin.readLineSync()!);
switch (cGPA) {
case 5:
print("You are the top candidate for the scholarship!");
break;
case 4:
print("You are eligible for the scholarship.\n");
break;
case 3:
print("You are not eligible for the scholarship.\n");
break;
default:
print("Please enter a valid CGPA between 2 and 5.\n");
break;
}
print(
"\n******************************** Switch Statement ********************************\n\n");
}
/// This program demonstrates the use of conditional statements (if-else and nested if-else) and switch statements in Dart.
/// It covers:
/// - Basic conditional statements for decision-making based on conditions.
/// - Nested conditional statements for handling multiple conditions within a single statement.
/// - Switch statements for executing code based on different values of an expression.
/// It includes examples for:
/// - Comparing two numbers to determine the larger one.
/// - Checking voting eligibility based on age.
/// - Determining scholarship eligibility based on CGPA.
/// It demonstrates the use of:
/// - print() for output.
/// - stdin for user input.
/// - int type for representing integers.
/// - comparison operators (>, >=, ==) for condition checks.
/// - conditional statements (if, else if, else).
/// - switch statement for multiple case-based execution.