blob: 29475da4ac30047465fcc37995c93801385dafaf (
plain)
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
|
#include <iostream>
/*
*
* 4.22
*
*
*/
void cond_op_grade() {
std::string finalgrade;
unsigned int grade = 0;
if(std::cin >> grade) {
if(grade > 100) { return; }
} else {
return;
}
finalgrade = (grade >= 90) ? "high pass" : (grade >= 75) ? "pass" : (grade >= 60) ? "low pass" : "fail";
std::cout << finalgrade << std::endl;
}
void if_grade() {
std::string finalgrade;
unsigned int grade = 0;
if(std::cin >> grade) {
if(grade > 100) { return; }
} else {
return;
}
if(grade >= 90) {
finalgrade = "high pass";
} else if(grade >= 75) {
finalgrade = "pass";
} else if(grade >= 60) {
finalgrade = "low pass";
} else {
finalgrade = "fail";
}
std::cout << finalgrade << std::endl;
}
int main () {
/*
Not really sure which of these is the 'better' alternative
i do like how conditional operator is very terse and can
do the same thing in a very small line. But it could also
be a bit more difficult to read and understand. It could
simply be a skill issue whether it's hard to read or not
but i am not really sure.
I do think the if statement is a bit easier to read because
it is simply just so common and widely used that you really
just default to it. I know that all operators and if
statements have their best moments to be used but i can't
really judge which one would be best for this case.
I guess the best use for the the conditional operator
is best when we want to do a simple if statement where there
are just two simple outcomes
*/
cond_op_grade();
if_grade();
return 0;
}
|