You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository provides examples of Dart programming language focusing on input handling and collections.
Dart Libraries
dart:io: This library is used to handle input and output operations.
Examples
Example 1: Basic Input and String Handling
import'dart:io';
voidmain() {
print('Hello World, test input');
String name;
name = stdin.readLineSync()!; // Input handlingprint("Welcome $name");
print('Skip char\'k'); // Using escape charactersprint('-----------------------\n');
print("Enter your number");
Stringnum= stdin.readLineSync()!;
int n =int.parse(num); // Converting string to integer
n++;
print(n); // Example: 5// ShortCut Exampleprint('-----------------------\n');
print("Enter your number");
int x =int.parse(stdin.readLineSync()!);
x +=10;
print("X : $x");
}
Key Points
String Handling: You can use either " or ' to define strings.
Input: stdin.readLineSync() is used for taking input.
Type Conversion: int.parse() and double.parse() are used to convert strings to integers or doubles.
Escape Characters: Use \ to escape characters.
Example 2: Simple Conditional Logic with Input
import'dart:io';
voidmain() {
print("Enter your random number between 1-10");
intnum=int.parse(stdin.readLineSync()!);
if (num==5)
print('Win');
elseif (num>0&&num<=10)
print('Lose, try again');
elseprint('Not in range');
}
Key Points
Conditional Logic: Basic if-else conditions to check the value of input.
List: Ordered collection that allows duplicates.
Set: Unordered collection that does not allow duplicates.
Map: Collection of key-value pairs, where each key is unique.
Example 4: Switch Case for GPA Calculation
import'dart:io';
voidmain() {
print('Enter your grade in char');
switch (stdin.readLineSync()!) {
case'A+':case'A':print("Your GPA is: 4.0");
break;
case'A-':print('Your GPA is: 3.7');
break;
case'B+':print('Your GPA is: 3.3');
break;
case'B':print('Your GPA is: 3.0');
break;
case'B-':print('Your GPA is: 2.7');
break;
case'C+':print('Your GPA is: 2.3');
break;
case'C':print('Your GPA is: 2.0');
break;
case'C-':print('Your GPA is: 1.7');
break;
case'D+':print('Your GPA is: 1.3');
break;
case'D':print('Your GPA is: 1.0');
break;
default:print("Your GPA is 0.0");
}
}
Key Points
Switch Statement: Used for conditional branching based on specific values.
Example 5: Iterating Over Collections
voidmain() {
List<String> names = ['Ahmed', 'Khalid'];
// Using forEach
names.forEach((name) {
print(name);
});
print('-------------------------\n');
// Using traditional for loopfor (int i =0; i < names.length; i++) {
print(names[i]);
}
print('-------------------------\n');
// Using for-in loopfor (var name in names) {
print(name);
}
}
Key Points
forEach: Function to iterate over all elements in a collection.
for loop: Traditional way of iterating with an index.
for-in loop: Simplified iteration over a collection.
Example 6: Dynamic vs. Var
voidmain() {
dynamic name;
name ="Khalid";
print('String: $name');
name ='C';
print('Char: $name');
name =22;
print("Integer: $name");
name =33.5;
print("Float: $name");
name =true;
print('Boolean: $name');
print('------------------------------------\n');
// var behaves like dynamic but with restrictionsvar x;
x ="Khalid";
print('String: $x');
x ='C';
print('Char: $x');
x =22;
print("Integer: $x");
x =33.5;
print("Float: $x");
x =true;
print('Boolean: $x');
print('------------------------------------\n');
// var with initial value becomes strongly typedvar xy ="Khalid";
print("Here is only string; cannot use xy with different datatype: $xy");
// xy = 12; // This would cause an error!
}
Key Points
Dynamic: Can change types dynamically.
Var: Can behave like dynamic but becomes type-restricted once assigned an initial value.