-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_making.rs
120 lines (100 loc) · 3.48 KB
/
decision_making.rs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Decision Making.
* Conditional statement.
*/
/**
* Conditional statement
* @ if, else, if else, nested if and if else statement.
*/
fn conditional_statement() {
// let numb: i32 = -10;
let numb: i32 = 70;
// if statement.
// An if statement consists of a Boolean expression followed by one or more statements.
// If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed.
if numb > 0 {
println!("Number: {} is positive number.", numb);
}
// let voting_age: i32 = 21;
let voting_age: i32 = 16;
//if...else statement.
// An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
// An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if statement evaluates to false.
if voting_age >= 18 {
println!("You are allowed to vote.");
} else {
println!("Sorry! You can't vote!");
}
let user_name: &str = "jhondoe";
// let password: &str = "abc";
let password: &str = "123abc";
// Checking multiple condition with if else statement.
if user_name == "jhondoe" && password == "123abc" {
println!("Welcome {}.", user_name);
} else {
println!("Oops! Looks like the username or the password you entered was incorrect!");
}
// let operating_system = "ChromeOS";
// let operating_system = "MacOS";
// let operating_system = "Linux";
let operating_system: &str = "Windows";
// else if statement.
// You can use one if or else if statement inside another if or else if statement(s).
if operating_system == "Windows" {
println!("You are using {}.", operating_system);
} else if operating_system == "Linux" {
println!("You are using {}.", operating_system);
} else if operating_system == "MacOS" {
println!("You are using {}.", operating_system);
} else {
println!("You are using operating system is unknown.");
}
let university = "Stanford University";
// let university = "UC Berkeley";
let major = "CSE";
// let major = "EEE";
// Nested ifstatement.
// It is possible to use one if or else if statement inside another if or else if statement(s).
if university == "Stanford University" {
if major == "CSE" {
println!("You are studying at {} and your major is {}.", university, major);
} else {
println!("You are studying at {}.", university);
}
} else {
println!("Sorry! University not found.");
}
}
/**
* Conditional statement
* @ match statement.
*/
fn match_statement() {
// let gpa: i8 = 90;
// let gpa: i8 = 50;
let gpa: i8 = 85;
// The match statement checks if a current value is matching from a list of values.
// double dot (..) is range operatot.
// underscore (_) is the else part of the match.
let result = match gpa {
97..= 100 => "A+",
93..= 96 => "A",
90..= 92 => "A-",
87..= 89 => "B+",
83..= 86 => "B",
80..= 82 => "B-",
77..= 79 => "C+",
73..= 76 => "C",
70..= 72 => "C-",
67..= 69 => "D+",
65..= 66 => "D",
_ => "F",
};
println!("Letter Grade : {}", result);
}
fn main() {
// if, else, if else, nested if and if else statement.
conditional_statement();
// match statement.
match_statement();
}