0% found this document useful (0 votes)
43 views13 pages

Dart Programming Basics and Lab Guide

The document discusses the content of a mobile computing lab including programming language basics, OOP, APIs, design patterns, and sockets. It covers Dart data types like int, double, num, String, bool, lists, and maps. It also discusses control flow, comments, operations, loops, and functions.

Uploaded by

Mai gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Dart Programming Basics and Lab Guide

The document discusses the content of a mobile computing lab including programming language basics, OOP, APIs, design patterns, and sockets. It covers Dart data types like int, double, num, String, bool, lists, and maps. It also discusses control flow, comments, operations, loops, and functions.

Uploaded by

Mai gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mobile computing lab1

Eng : hamada mostafa Eng : fatma mohammed


Course content

u Programming language ( Dart ) basics


u OOP in Dart
u Design layout
u APIs
u Design pattern
u Socket
Lab1 content

u Data type

u Control Flow

u Comment

u Operation

u Loops

u Function
Data type
u int: Represents integer values
int myInt = 10;
u double: Represents floating-point numbers
double myDouble = 3.14;
u num: Represents either an integer or a floating-point number
num myNum = 10; // Integer
num myNum2 = 3.14; // Double
u String: Represents a sequence of characters
String myString = 'Hello, Dart!’;
u bool: Represents boolean value
bool isDartFun = true;
u List: Represents an ordered collection of objects
List<int> numbers = [1, 2, 3, 4, 5];
List<String> names = ['Alice', 'Bob', 'Charlie’];
u Map: Represents a collection of key-value pairs
Map<String, int> ages = {
'Alice': 30,
'Bob': 25,
'Charlie': 35,
};
u dynamic: Represents a type that can change at runtime
dynamic dynamicVar = 10;
dynamicVar = 'Hello';
Control Flow
:
if statement switch statement:
u int x = 10; u int number = 3;
If (x > 5) { switch (number) {
print('x is greater than 5'); case 1: print('One’);
} else { break;
print('x is not greater than 5'); case 2: print('Two’);
} break;
case 3: print('Three’);
break;
default: print('Unknown number’);
break;
Comment

Single-line comments: Multi-line comments:

u // This is a single-line comment var u /*


This is a multi-line comment
It can span multiple lines
u number = 10; // This comment explains and is useful for longer planations
the purpose of the variable
u */
Operation

Arithmetic Operators : Logical Operators :


1. * and / 1. NOT (!)
2. + and - 2. AND (&&)
3. OR (||)
u void main() { u void main() {
int result = 10 + 5 * 2; bool result = true && false || true;
print(result); print(result);
// Output: 20 // Output: true
} }
loops
u for loop:
for (int i = 0; i < 5; i++) {
print('Index: $i');
}
u while loop:
int count = 0;
while (count < 5) {
print('Count: $count');
count++;
}
u do-while loop:
int count = 0;
do {
print('Count: $count');
count++;
} while (count < 5);
Functions

u Function without a return value


Syntax :
void functionName(parameters) {
// Function body
}
EX :
void greet(String name) {
print('Hello, $name!');
}
u Function with a return value:
Syntax :
returnType functionName(parameters) {
// Function body
return value;
}

Ex :
int add(int a, int b) {
return a + b;
}
Quick task

u A phrase is a palindrome if, after converting all uppercase letters into lowercase
letters and removing all non-alphanumeric characters, it reads the same forward and
backward. Alphanumeric characters include letters and numbers.

u EX :
Input : “race a car”
Output: false

Input : “a man , a plane , a canal: panama”


Output : true
Thanks !

Common questions

Powered by AI

Functions in Dart can either return a value or not. Functions without a return, like 'void greet(String name) { print('Hello, $name!'); }', perform actions without producing a value. Functions with a return value, such as 'int add(int a, int b) { return a + b; }', provide outputs that can be used later. Functions without returns are beneficial for operations where output isn't needed, while functions with returns are crucial for computations where result propagation is needed, enhancing reusability and modularity .

Arithmetic operations in Dart involve the use of operators like '*', '/', '+', and '-', for instance, 'int result = 10 + 5 * 2' evaluates to '20' due to operator precedence. Logical operations use operators like NOT (!), AND (&&), and OR (||), such as in 'bool result = true && false || true' which evaluates to 'true' due to the precedence of operators. Arithmetic operations primarily handle numerical computations, whereas logical operations are used for Boolean logic and control structures .

Understanding and implementing design patterns in Dart enhances cognitive abilities by providing proven solutions and frameworks for common problems, promoting efficient code structure, and improving code reusability and readability. Design Patterns encourage thinking in patterns rather than specific solutions, facilitating easier problem decomposition and pattern recognition, which is essential for complex software architecture and evolving software with minimal disruption .

The 'dynamic' type in Dart allows variables to change type at runtime, providing flexibility, as shown in 'dynamic dynamicVar = 10; dynamicVar = 'Hello';'. This flexibility supports scenarios where variable types are not known at compile time. However, drawbacks include a lack of compile-time type checking, leading to potential runtime errors and reduced code clarity, which can complicate debugging and maintenance .

Dart checks a palindrome by converting strings to lowercase, removing non-alphanumeric characters, and comparing the processed string to its reverse, as explained with 'Input: "race a car"' and 'Output: false'. This functionality can be applied in data validation, natural language processing, and developing applications where linguistic symmetry is evaluated, like in DNA sequence analysis and cryptography .

In Dart, 'List' is used for ordered collections, useful when items are accessed by index, as in 'List<int> numbers = [1, 2, 3]'. 'Map' is for key-value pairs, helpful for associating meaningful identifiers with values, shown as 'Map<String, int> ages = {'Alice': 30}'. 'List' advantages include efficient indexed access and modifiers like append; 'Map' provides quick lookups by key, making it suitable for associative arrays. Each is optimal based on whether the task requires indexed or associative data handling .

Dart provides 'for', 'while', and 'do-while' loops. The 'for' loop is ideal for iterating with a known number of iterations, as in 'for (int i = 0; i < 5; i++)'. The 'while' loop evaluates a condition before each iteration, suitable for indeterminate iterations: 'while (count < 5) { count++; }'. The 'do-while' loop guarantees at least one iteration by checking the condition afterward, as shown in 'do { count++; } while (count < 5)'. These constructs allow flexibility depending on whether the number of iterations is known or dependent on runtime conditions .

Dart defines several fundamental data types, including 'int', which represents integer values such as 'int myInt = 10'; 'double', used for floating-point numbers like 'double myDouble = 3.14'; 'num', which can be either an integer or a double allowing flexibility, demonstrated as 'num myNum = 3.14'; 'String', representing a sequence of characters such as in 'String myString = 'Hello, Dart!''; 'bool' for Boolean values, seen in 'bool isDartFun = true'; 'List', an ordered collection exemplified by 'List<int> numbers = [1, 2, 3]'; 'Map', a collection of key-value pairs like 'Map<String, int> ages = {'Alice': 30}'; and 'dynamic', a type that can change at runtime, shown as 'dynamic dynamicVar = 10'. These types differ in terms of storage and the operations applicable to them, making them suitable for various programming scenarios .

Dart supports both single-line and multi-line comments. Single-line comments, indicated by '//', are used for brief notes, such as 'var number = 10; // Variable initialization'. Multi-line comments, indicated by '/* */', span across multiple lines for detailed explanations. Comments enhance code readability by providing context and explaining complex logic, which is crucial during collaborative development and maintenance .

Dart uses 'if' and 'switch' statements for control flow. The 'if' statement evaluates a condition and executes a block of code when the condition is true, as shown with 'if (x > 5) { print('x is greater than 5'); }'. The 'switch' statement evaluates an expression and executes code corresponding to the matching case, such as 'switch (number) { case 3: print('Three'); break; }'. These constructs are used for decision-making, with 'if' statements typically for more complex logical evaluations and 'switch' statements for fixed comparison values .

You might also like