Last active
November 28, 2020 08:35
-
-
Save olaide-ekolere/64699e0cfb008ad1c6c488bb653ae8a2 to your computer and use it in GitHub Desktop.
List, Enums & Control Statements - Introduction to dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
int score1 = 36; | |
int score2 = 85; | |
int score3 = 66; | |
int score4 = 73; | |
int total = score1 + score2 + score3 + score4; | |
var average = total / 4; | |
print("the average score is: $average"); | |
//control statement | |
if(score4>average){ | |
print("$score4 is greater than average"); | |
} | |
else { | |
print('$score4 is below average'); | |
} | |
print(scoreGrade(60)); | |
printGrade(scoreGrade(score4)); | |
printGrade("Banana"); | |
//enums | |
print(scoreEnumGrade(60)); | |
printEnumGrade(scoreEnumGrade(score4)); | |
printEnumGrade(Grade.Fail); | |
showAverage("CSC101", score1, score2, score3, score4); | |
//increasing number of scores issue | |
//List | |
List<int> scores = [45, 85, 56, 73]; | |
print(scores[0]); | |
print(scores[1]); | |
print(scores[2]); | |
print(scores[3]); | |
print(scores.length); | |
//a control flow statement | |
for(int i = 0; i < scores.length; i++){ | |
print("Item $i is ${scores[i]}"); | |
} | |
showAverageScores("CSC201", scores); | |
} | |
String scoreGrade(int score){ | |
//control statements | |
if(score >= 70) return 'Excellent'; | |
else if(score >= 60) return 'Good'; | |
else if(score >= 50) return 'Average'; | |
else if(score >= 40) return 'Pass'; | |
else return 'Fail'; | |
} | |
void printGrade(String grade){ | |
print("your grade is $grade"); | |
} | |
enum Grade { Excellent, Good, Average, Pass, Fail } | |
Grade scoreEnumGrade(int score){ | |
if(score >= 70) return Grade.Excellent; | |
else if(score >= 60) return Grade.Good; | |
else if(score >= 50) return Grade.Average; | |
else if(score >= 45) return Grade.Pass; | |
else return Grade.Fail; | |
} | |
void printEnumGrade(Grade grade){ | |
print("your grade is $grade"); | |
} | |
void showAverage( | |
String subject, int score1, int score2, int score3, int score4) { | |
int total = score1 + score2 + score3 + score4; | |
var average = total / 4; | |
print("$average is the average score in $subject"); | |
} | |
void showAverageScores(String subject, List<int> scores){ | |
int total = 0; | |
for(var score in scores){ | |
total = total + score; | |
} | |
var average = total / 4; | |
print("$average is the average list score in $subject"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment