-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditionals.dart
96 lines (83 loc) · 1.9 KB
/
conditionals.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
import 'dart:math'; // include this package for use at the end
void main() {
// Simple if statements
var animal = 'moose';
if (animal == 'cat' || animal == 'dog') {
print('Animal is a house pet.');
} else {
print('Animal is NOT a house pet.');
}
String timeOfDay = "";
print(timeOfDay);
// More complicated if/else if/else
var hourOfDay = 12;
if (hourOfDay < 6) {
timeOfDay = "Early morning";
} else if (hourOfDay > 6 && hourOfDay < 12) {
timeOfDay = "Morning";
} else if (hourOfDay < 17) {
timeOfDay = "Afternoon";
} else if (hourOfDay < 20) {
timeOfDay = "Evening";
} else if (hourOfDay < 24) {
timeOfDay = "Late evening";
} else {
timeOfDay = "INVALID HOUR!";
}
print(timeOfDay);
//// Redoing this as a switch statement has a gotcha...
// switch (hourOfDay) {
// case 6: // The gotcha is that each case must be a constant, not an expression
// timeOfDay = "Early morning";
// break;
// case 12:
// timeOfDay = "Morning";
// break;
// case 17:
// timeOfDay = "Afternoon";
// break;
// case 20:
// timeOfDay = "Evening";
// break;
// case 24:
// timeOfDay = "Late evening";
// break;
// default:
// timeOfDay = "INVALID HOUR!";
// break;
// }
// A better example of switch statements in action that everyon can relate to
var grade = "A";
switch (grade) {
case "A":
{
print("Excellent");
}
break;
case "B":
{
print("Good");
}
break;
case "C":
{
print("Fair");
}
break;
case "D":
{
print("Poor");
}
break;
default:
{
print("Invalid choice");
}
break;
}
// Demonstrating a ternary operator (and using the math library as well)
var piVsE = e > pi ? 'e' : 'pi';
print(piVsE);
print(e);
print(pi);
}