YouTip LogoYouTip

Dart Control Flow

```html\\nControl flow determines the order in which code executes in a program.\\n\\nThis chapter introduces conditional statements (if/else, switch) and loop statements (for, while) in Dart, as well as the use of break and continue.\\n\\n* * *\\n\\n## if / else / else if Conditional Statements\\n\\nThe if statement is the most basic control flow structure that decides whether to execute a block of code based on a condition.\\n\\n## Example\\n\\nBasic if judgment:\\n\\nvoid main(){\\n\\nint score =85;\\n\\n// Basic if: executes when condition is true\\n\\nif(score >=60){\\n\\n print('TUTORIAL Congratulations, you passed!');\\n\\n}\\n\\n// if-else: choose one of two\\n\\nif(score >=90){\\n\\n print('Excellent');\\n\\n}else{\\n\\n print('Keep it up');\\n\\n}\\n\\n// if-else if-else: multiple branch selection\\n\\nif(score >=90){\\n\\n print('Grade: A');\\n\\n}else if(score >=80){\\n\\n print('Grade: B');\\n\\n}else if(score >=70){\\n\\n print('Grade: C');\\n\\n}else if(score >=60){\\n\\n print('Grade: D');\\n\\n}else{\\n\\n print('Grade: F (Fail)');\\n\\n}\\n\\n}\\n\\nTUTORIAL Congratulations, you passed! Keep it up Grade: B\\n> Dart's conditional expressions must be of type bool. Unlike JavaScript, you cannot use 0, '', null, etc. as conditionsβ€”these will cause compilation errors in Dart.\\n\\n### Nested if Statements\\n\\nif statements can be nested within other if statements to handle more complex logic.\\n\\n## Example\\n\\nvoid main(){\\n\\nbool isLoggedIn =true;\\n\\nbool isAdmin =false;\\n\\n String username ='tutorial';\\n\\nif(isLoggedIn){\\n\\n// Outer layer: check if logged in\\n\\nif(isAdmin){\\n\\n// Inner layer: check if admin\\n\\n print('Welcome back,Administrator $username');\\n\\n}else{\\n\\n print('Welcome back,$username');\\n\\n}\\n\\n}else{\\n\\n print('Please log in first');\\n\\n}\\n\\n}\\n\\nWelcome back,tutorial\\nExcessive nesting levels reduce readability. It is recommended to keep it to no more than 3 levels.\\n\\nIf the logic is indeed complex, consider using early returns or extracting into separate functions for better readability.\\n\\n## Example\\n\\nUsing early returns instead of deep nesting:\\n\\n// Using early returns instead of deep nesting for better readability\\n\\n String getAccessMessage(bool isLoggedIn,bool isAdmin, String name){\\n\\n// Handle "return early if condition not met" first\\n\\nif(!isLoggedIn){\\n\\nreturn'Please log in first';\\n\\n}\\n\\nif(!isAdmin){\\n\\nreturn'You do not have admin privileges';\\n\\n}\\n\\n// Finally, the main logic\\n\\nreturn'Welcome back,Administrator $name';\\n\\n}\\n\\nvoid main(){\\n\\n print(getAccessMessage(true,false,'tutorial'));\\n\\n print(getAccessMessage(true,true,'tutorial'));\\n\\n}\\n\\nYou do not have admin privilegesWelcome back,Administrator tutorial\\n\\n* * *\\n\\n## switch / case Multiple Branch Selection\\n\\nWhen a variable has multiple fixed possible values, switch is clearer than if-else if.\\n\\n## Example\\n\\nvoid main(){\\n\\n String day ='Wednesday';\\n\\nswitch(day){\\n\\ncase'Monday':\\n\\n print('A new week begins');\\n\\nbreak;// break ends current case to prevent "fall-through" to next case\\n\\ncase'Tuesday':\\n\\n print('Second day of striving');\\n\\nbreak;\\n\\ncase'Wednesday':\\n\\n print('Halfway through the week');\\n\\nbreak;\\n\\ncase'Thursday':\\n\\n print('Almost Friday');\\n\\nbreak;\\n\\ncase'Friday':\\n\\n print('TGIF!');\\n\\nbreak;\\n\\ncase'Saturday':\\n\\ncase'Sunday':\\n\\n// Multiple cases can share the same code\\n\\n print('Happy weekend!');\\n\\nbreak;\\n\\ndefault:\\n\\n// Executes when no case matches\\n\\n print('Unknown date');\\n\\n}\\n\\n}\\n\\nHalfway through the week\\n> Dar\\n```
← Dart FunctionsDart Variables And Types β†’