Skip to content

Instantly share code, notes, and snippets.

@aijazbinqasim
Last active March 30, 2023 15:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aijazbinqasim/f448d7d4f805502e3c0080942201d59a to your computer and use it in GitHub Desktop.
Save aijazbinqasim/f448d7d4f805502e3c0080942201d59a to your computer and use it in GitHub Desktop.
Dart Programming Language

🎯 Dart Programming Language

📘 Topics:

👉 Data Types

  1. Num
  2. Int
  3. Double
  4. Bool
  5. String
  6. List
  7. Map

👉 Operators

  1. Arithmetic
  2. Arithmetic assignment
  3. Relational
  4. Conditional
  5. Logical

👉 Program Control Statements

  1. If
  2. Switch
  3. For
  4. For in
  5. While
  6. Do while
  7. Break
  8. Continue

👉 Object Oriented Programming

  1. Class
  2. Constructor
  3. Named constructor
  4. Function/method
  5. Named parameters/arguments
  6. Positional parameters/arguments
  7. Optional & default parameters/arguments
  8. Const, final, static, var & dynamic keywords
  9. Enums
  10. Properties

Dart Programming Language

Created with <3 with dartpad.dev.

void main() {
// Data types are used to save/store different kinds of information for our application.
// 1.num
// It is used to store whole & whole number with a decimal point.
// Example: 1 (whole number)
num totalMarks; // Declare,
totalMarks = 600; // Initialize.
print('Total marks: $totalMarks'); // Use
// Example: 2 (whole number with a decimal point)
num gravity;
gravity = 9.8;
print('Gravity: $gravity');
// Example: 3
num obtainMarks, percentage;
obtainMarks = 300;
percentage = 50.0;
print('Obtain marks: $obtainMarks');
print('Percentage: $percentage');
// End of 1.num
// 2.int
// It is used to store whole number only.
// Example:
int userId;
userId = 1;
print('User ID: $userId');
// End of 2. int
// 3.double
// It is used to store whole number with a decimal point only.
// Example:
double pi;
pi = 3.14;
print('Value of pi: $pi');
// End of 3.double
// 4.bool
// It is used to store true/false only.
// Example:
bool isUserAuthenticated;
isUserAuthenticated = true;
print('Is this user authenticated? $isUserAuthenticated');
// End of 4.bool
// 5.String
// It is used to store multiple characters with double quotes.
// Example:
String name;
name = 'Aijaz Bin Qasim';
print('My name is: $name');
// End of 5.String
// 6.List
// It is used to store multiple information as collection in its different locations (indexes).
// Example 1:
List<String> languages = [];
languages.add('Dart');
languages.add('JavaScript');
languages.add('C#');
// Iterate List via for loop.
for(var index = 0; index < languages.length; index ++) {
print("$index: ${languages[index]}");
}
// Iterate List via for in loop.
var index = 0;
for(var lang in languages) {
print('${index++}: $lang');
}
// Access List single value.
print('Index 0 has element: ${languages[0]}');
print('List of languages: $languages');
// Example 2:
List<int> oddNumbers = List.filled(3, 0, growable: true);
oddNumbers[0] = 3;
oddNumbers[1] = 5;
oddNumbers[2] = 7;
oddNumbers.add(9);
print('List of odd numbers: $oddNumbers');
// End of 6.List
// 7.Map
// It is used to store information as key/value pairs.
// Example:
Map<String, String> user = {};
user['name'] = 'Aijaz';
user['profession'] = 'Software Engineer';
user['location'] = 'Hyderabad';
// Iterate Map via for in loop.
for(var usr in user.keys) {
print('$usr: ${user[usr]}');
}
// Iterate Map via Map.forEach loop.
user.forEach((key, value) => {
print('$key: $value')
});
// Access Map single value.
print('User: $user');
print('Name: ${user["name"]}');
// End of 7.Map
// End of Data types.
}
void main() {
// Operators are used to perform different kinds of operations on Data types.
// 1.Arithmetic
// These operators are used to perform calculations b/w 2 or more variables.
// Example: Addition (+)
int dartMarks;
int javaScriptMarks;
int cSharpMarks;
dartMarks = 60;
javaScriptMarks = 70;
cSharpMarks = 85;
print('Obtain marks: ${dartMarks + javaScriptMarks + cSharpMarks}');
// Example: Substraction (-)
int totalBalance;
int totalExpenses;
totalBalance = 500;
totalExpenses = 300;
print('Remaining balance: ${totalBalance - totalExpenses}');
// Example: Multiplication (*)
int square;
square = 4;
print('Square of $square is: ${square * square}');
// Example: Division (/)
double totalWadges;
totalWadges = 5048;
print('There are 5 workers, each worker gets R.s: ${totalWadges / 5}');
// Example: Module (%)
double value;
value = 20;
print('$value devided by 3, reminder is: ${value % 3}');
// End of 1.Arithmetic
// 2.Arithmetic Assignment
// These operators are used to perform calculations & also assign values to variables.
// Example: Addition Assignment (+=)
int a;
a = 10;
a += 5; // This statement same as a = a+5
print('a: $a');
// Example: Substraction Assignment (-=)
int b;
b = 10;
b -= 5;
print('b: $b');
// Example: Multiplication Assignment (*=)
int c;
c = 10;
c *= 5;
print('c: $c');
// Example: Division Assignment (/=)
double d;
d = 10;
d /= 5;
print('d: $d');
// Example: Module Assignment (%=)
int e;
e = 10;
e %= 5;
print('e: $e');
// End of 2.Arithmetic Assignment
// 3.Relational
// These operators are used to perform comparison b/w 2 operands & return result in bool (true/false).
// Examples:
int age;
int otherAge;
age = 17;
otherAge = 18;
// Example: Greater than (>)
bool greaterThan;
greaterThan = (age > otherAge); // This (age > otherAge) is called a condition.
print('Greater than (>): $greaterThan');
// Example: Less than (<)
bool lessThan;
lessThan = (age < otherAge);
print('Less than (<): $lessThan');
// Example: Greater than or equal to (>=)
bool greaterThanOrEqualTo;
greaterThanOrEqualTo = (age >= otherAge);
print('Greater than or equal to (>=): $greaterThanOrEqualTo');
// Example: Less than or equal to: (<=)
bool lessThanOrEqualTo;
lessThanOrEqualTo = (age <= otherAge);
print('Less than or equal to: (<=): $lessThanOrEqualTo');
// Example: Equal to: (==)
bool equalTo;
equalTo = (age == otherAge);
print('Equal to: (==): $equalTo');
// Example: Not equal to: (!=)
bool notEqualTo;
notEqualTo = (age != otherAge);
print('Not equal to: (!=): $notEqualTo');
// End of 3.Relational
// 4.Logical
// These operators are used to perform comparison b/w 2 or more conditions & return result in bool (true/false).
// Examples:
int numOne;
int numTwo;
String color;
numOne = 50;
numTwo = 60;
color = 'red';
// Example: Logical OR (||):
// Returns true if any condition is true.
bool logicalOr;
logicalOr = numOne == numTwo || color == 'red';
print('Logical OR (||): $logicalOr');
// Example: Logical AND (&&):
// Returns true if all conditions are true.
bool logicalAnd;
logicalAnd = numOne == numTwo && color == 'red';
print('Logical AND (&&): $logicalAnd');
// Example: Logical NOT (!) with a condition:
// Returns inverse of the conditions result.
bool logicalNot;
logicalNot = (!(numOne == numTwo) && color == 'red');
print('Logical NOT (!): $logicalNot');
// Example: Logical NOT (!) with Logical OR (||):
// Returns inverse of the Logical OR (||) result.
logicalNot = (!(numOne == numTwo || color == 'red'));
print('Logical NOT (!): $logicalNot');
// Example: Mix Logical OR (||) AND (&&):
bool mixLogicalOrAnd;
mixLogicalOrAnd = (numOne == numTwo || color == 'red') && (numOne == numTwo && color == 'red');
print('(Logical OR (||)) && (Logical AND (&&)): $mixLogicalOrAnd');
// End of 4.Logical
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment