Created
August 23, 2021 07:24
-
-
Save mhmadip/8a893d79dad3b4d02ccfe240388ad480 to your computer and use it in GitHub Desktop.
Dart Control Flow Statements
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
// Chapter 5: Control Flow Statements | |
void main() { | |
//if statment | |
var testList = [2, 4, 8, 16, 32]; | |
print(testList); | |
if (testList.isNotEmpty) { | |
print("Emptying List"); | |
testList.clear(); | |
} | |
print(testList); | |
//if-else statments | |
var pointsA = 50; | |
var pointsB = 64; | |
if (pointsA > pointsB) { | |
print("Team A Wins!"); | |
} else { | |
print("Team B Wins!"); | |
} | |
// if-else if statment | |
if (pointsA > pointsB) { | |
print("Team A Wins!"); | |
} else if (pointsB > pointsA) { | |
print("Team B Wins!"); | |
} else { | |
print("It's a Tie!"); | |
} | |
//Ternary operatror ? : | |
var a = 5; | |
var b = 2; | |
var result = a > b ? a - b : b - a; | |
print(result); | |
// Basic for loop | |
for (var i = 0; i < 5; i++) { | |
print(i); | |
} | |
// Iterating Through a Collection | |
var colorList = ['blue', 'yellow', 'green', 'red']; | |
for (var i = 0; i < colorList.length; i++) { | |
print(colorList[i]); | |
} | |
// Conditions with Loops | |
var intList = [6, 7, 3, 9, 2, 5, 4]; | |
for (var i in intList) { | |
if (i % 2 == 0) { | |
print(i); | |
} | |
} | |
//while in Action | |
var count = 1; | |
while (count <= 10) { | |
print(count); | |
count += 1; | |
} | |
//do-while | |
var alwaysOne = 1; | |
do { | |
print("Using do-while: $alwaysOne"); | |
} while (alwaysOne != 1); | |
//The break Statement | |
var intList1 = [7, 3, 9, 6, 2, 5, 4]; | |
for (var i in intList1) { | |
if (i % 2 == 0) { | |
print(i); | |
break; | |
} | |
} | |
//The continue Statement | |
var experience = [5, 1, 9, 7, 2, 4]; | |
for (var i = 0; i < experience.length; i++) { | |
var candidateExperience = experience[i]; | |
if (candidateExperience < 5) { | |
continue; | |
} | |
print("Call candidate $i for an interiew."); | |
} | |
//switch and case | |
var command = 'OPEN'; | |
switch (command) { | |
case 'CLOSED': | |
print('closed'); | |
break; | |
case 'PENDING': | |
print('pending'); | |
break; | |
case 'APPROVED': | |
print('approved'); | |
break; | |
case 'DENIED': | |
print('denied'); | |
break; | |
case 'OPEN': | |
print('open'); | |
break; | |
default: | |
print('command unknown'); | |
} | |
//Assertion with assert | |
var variable; | |
print(variable); | |
assert(variable != null); | |
variable = 5; | |
print(variable); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment