Dart Programming (Conditional and Looping and Control Statements in Dart)
Control flow statements in Dart are used to manage the flow of execution in your code. Dart supports common control flow statements found in most programming languages. These include:
Conditional Statements:
if Statements: Used to execute code block(s) conditionally based on a boolean expression.
dart
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
else-if Ladder: Allows you to test multiple conditions one by one until one of them is true.
dart
if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition2 is true } else { // Code to execute if none of the conditions are true }
switch Statement: Used for multi-branch decision-making based on the value of an expression.
dart
switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; default: // Code to execute if none of the cases match }
Looping Statements:
for Loop: Executes a block of code a specified number of times.
dart
for (var i = 0; i < 5; i++) { // Code to repeat }
while Loop: Repeats a block of code as long as a condition is true.
dart
while (condition) { // Code to repeat as long as condition is true }
do-while Loop: Similar to a while
loop, but it guarantees that the code block is executed at least once before checking the condition.
dart
do { // Code to repeat at least once } while (condition);
for-in Loop: Iterates over the elements of an iterable (e.g., list or map).
dart
for (var item in iterable) { // Code to execute for each item in the iterable }
Control Statements:
break Statement: Used to exit a loop or switch statement prematurely.
dart
for (var i = 0; i < 5; i++) { if (i == 3) { break; // Exit the loop when i is 3 } // Code inside the loop }
continue Statement: Skips the rest of the current iteration and continues to the next iteration of a loop.
dart
for (var i = 0; i < 5; i++) { if (i == 2) { continue; // Skip the current iteration when i is 2 } // Code inside the loop }
These control flow statements in Dart allow you to make decisions, repeat code, and control the flow of your program based on conditions and loops.
// Conditional and
import 'dart:io';
void main()
{
stdout.write("Enter the integer value: ");
int a = int.parse(stdin.readLineSync()!);
// else
if(a%2 == 0){
print("\t${a}...Even...");
}
else{
print("\t${a}...Odd...");
}
// else-if
if(a < 0){
print("\t${a} is Negative");
}
else if(a > 0){
print("\t${a} is Positive");
}
else{
print("\t${a} is Zero");
}
}
// Switch Statement
import 'dart:io';
void main()
{
stdout.write("Enter the Week day number(1 to 7):");
int day = int.parse(stdin.readLineSync()!);
switch(day){
case 1:
print("Happy Saturday...");
break;
case 2:
print("Enjoy your Monday...");
break;
case 3:
print("Sweet Tuesday...");
break;
case 4:
print("Funny Wednesday...");
break;
case 5:
print("Interesting Thursdays...");
break;
case 6:
print("End Friday...");
break;
case 7:
print("Rest Saturday...");
break;
default:
print("Enter the valid option");
}
}
// Looping and Control Statements in Dart
import 'dart:io';
void main()
{
List<String> days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sundays"];
print("\n\n\tDays in a week (using for loop)");
for(int i=0; i<days.length; i++){
print((i + 1).toString() + " " + days[i]);
}
print("\n\n\tDays in a week (using for While)");
int i = days.length-1;
while(i > 0){
print((i + 1).toString() + " " + days[i]);
i--;
}
print("\n\n\tDays in a week (using for Do-While)");
i = days.length-1;
do{
print((i + 1).toString() + " " + days[i]);
i--;
}while(i >= 0);
}
Comments
Post a Comment