0% found this document useful (0 votes)
8 views31 pages

Mop App2

The document provides an overview of mobile app development, focusing on Dart programming language concepts such as variables, data types, conditions, loops, and operators. It explains how to declare variables, use different data types like strings and lists, and implement control structures like if statements and loops. Additionally, it covers the use of keywords and the importance of variable scope in Dart.

Uploaded by

anas almamary
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)
8 views31 pages

Mop App2

The document provides an overview of mobile app development, focusing on Dart programming language concepts such as variables, data types, conditions, loops, and operators. It explains how to declare variables, use different data types like strings and lists, and implement control structures like if statements and loops. Additionally, it covers the use of keywords and the importance of variable scope in Dart.

Uploaded by

anas almamary
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
You are on page 1/ 31

Mobile App Development

Variables,
Dart( Conditions
& loops
)
Lec 2

Mohammed
1
Mobile App Development
Mobile app development is the act or process by which a mobile app is
developed for mobile devices, such as personal digital assistants,
enterprise digital assistants or mobile phones. These software
applications are designed to run on mobile devices, such as a
smartphone or tablet computer.

Mark Zukerburg
Facebook

2
What is a variable in Dart?
• Variables are used to store the value at the memory location. It
contains a namespace referring to memory location.
• Dart is case-sensitive. This means that Dart differentiates between
uppercase and lowercase characters.

• Declaration
– Variables can be declared in multiple ways
• You can use using var keyword or directly use the type without the
var keyword Syntax:
– type variableName;
– var variable=value;
• The first one is a type of variable that must be declared with a
variable name. Type can be primitive or custom types declared in
Dart.

All variables in dart store a reference to the value rather than containing the value.
3
// this is single line comment /* This is a Multi-line comment */
Dart Data Types
• Number
• String
• Boolean
• List
• map

4
Built primitive types
• Number types:
– Numerical values such as num, int, double
– The num type is an inherited data type of the int and double types.
void main() {
void main() {
// declare an integer
var a1 = num.parse("1");
int num1 = 2;
var b1 = num.parse("2.34");
// declare a double value
double num2 = 1.5;
var c1 = a1 + b1;
// print the values
print("Product = ${c1}");
print(num1);
}
print(num2);
}
The parse() function is used parsing a string containing numeric literal and
convert to the number.

5
Number
• Properties:
•isEven: If the given number is an even then this property will return true.
•isOdd: If the given number is odd then this property will return true.

• Methods
– abs(): This method gives the absolute value of the given number.
– ceil(): This method gives the ceiling value of the given number.
– floor(): This method gives the floor value of the given number.
– compareTo(): This method compares the value with other numbers.
– remainder(): This method gives the truncated remainder after dividing the two numbers.
– round(): This method returns the round of the number.
– toDouble(): This method gives the double equivalent representation of the number.
– toInt(): This method returns the integer equivalent representation of the number.
– toString(): This method returns the String equivalent representation of the number
– truncate(): This method returns the integer after discarding fraction digits.

6
String
• String :
– Strings represent a sequence of characters.
– String values are embedded in either single or double quotes.
– String values in Dart can be represented using either single or double or triple
quotes.
– Single line strings are represented using single or double quotes. Triple quotes are
used to represent multi-line strings.
String variable_name= ''''''line1
line2''''''
• Concatenation :
• The operator plus (+) is a commonly used mechanism to concatenate string
• String conct = str1+str2;
• Using "${}“ used to interpolate the value of a Dart expression within strings.
• String str2 = "The sum of 2 and 2 is ${2+2}";

• String Properties:
• isEmpty Returns true if this string is empty.
• Length Returns the length of the string including space, tab and newline characters.

7
String
void main() {
String str = 'Coding is ';
String str1 = 'Fun';
print(str + str1);
}

8
Methods to Manipulate Strings
• toLowerCase()
– Converts all characters in this string to lower case.
• toUpperCase()
– Converts all characters in this string to upper case.
• toString()
– Returns a string representation of this object.
• split()
– Splits the string at matches of the specified delimiter and returns a list of
substrings.
• replaceAll()
– Replaces all substrings that match the specified pattern with a given value.

9
Data types
• Bool:
– The Boolean data type represents Boolean values
true and false.
• List and map:
– The data types list and map are used to represent
a collection of objects.

10
List
• List data type is similar to arrays in other programming
languages. A list is used to represent a collection of objects. It
is an ordered group of objects.

• is one of the data structures in Dart Collections used to store


the collection of values or objects. A list is an ordered list and
allows duplicate elements.

• The list stores the elements in order of the way.

• The list index always starts with zero and ends index with the
list.length -1.
11
List
•Fixed size List:
•Dart has no support for Array and it is equal to Array in Dart. List length does not allow you
to change at runtime.

•Growable List:
•There is no fixed length and the list is growable and length is changeable.

•Syntax:
•List List.filled(int length, String fill, { bool growable : false, })

void main() {
var listvariable = new List<String>.filled(3, "", growable: false);

listvariable[0] = "one";
listvariable[1] = "two";
listvariable[2] = "three";
print(listvariable); 12
}
List
void main() {
var listvariable = [];
var listvariable1 = [1, 2, 3];

listvariable = ["one", "two", "three"];


listvariable1.add(4);
print(listvariable);
print(listvariable1);
}

13
List
• Using square brackets
– List<int> numbers = [1,2,3,4];
• Using list constructor
– List<String> frt = List.empty(growable, true);
– Frt.addAll([‘apple’, ’banana’]);

14
Map
• The Map object is a key and value pair. Keys
and values on a map may be of any type. It is a
dynamic collection.

void main() {
Map lst = new Map();
lst['First'] = 'Al-janad';
lst['Second'] = 'is';
lst['Third'] = 'University';
print(lst);
}

{First: Al-janad, Second: is, Third: University}


15
Map
• There is no restriction on the type of data that goes in a map data type.
• Maps are very flexible and can mutate their size based on the requirements.
• All keys need to be unique inside a map data type.
• Syntax:
– var map_name = { key1 : value1, key2 : value2, ..., key n : value n }

void main() {
var mp = {'position1': 'Al-janad', 'position2': 'is', 'position3': 'Univ'};
// Printing Its content
print(mp);
// Printing Specific Content , Key is defined
print(mp['position1']);
// Key is not defined
print(mp[0]); //return null
}

16
Map
In new dart use the following:
Map<String, int> frt = {
‘apple’:1,
‘Banana’:2,
};

17
Variable scopes
• Each Variable declared in dart has scope and
scope is lifetime based on the place of
declaration.
• There are two types of variable scopes.
– Global Variable
– Local Variable

18
Final and Const
• The final and const keyword are used to declare constants. Dart prevents
modifying the values of a variable declared using the final or const keyword.
• The const keyword is used to represent a compile-time constant. Variables
declared using the const keyword are implicitly final.

19
Final and Const
• Note − Only const variables can be used to compute a compile time constant.
Compile-time constants are constants whose values will be determined at
compile time.
void main() {
const pi = 3.14;
const area = pi*12*12;
print("The output is ${area}");
}

Error:
void main() {
final pi = 3.14;
const area = pi*12*12; //error in pi, pi must be constant
print("The output is ${area}");
}

20
Operators
• An expression is a special kind of statement
that evaluates to a value.

• There are many kinds of operators:


– Arithmetic Operators (+, -,* …….)
– Equality and Relational Operators (==, !=, <, > ……)
– Type test Operators
– Assignment Operators (=, ??=)
– Logical Operators (&&, ||, !)

21
Operators
• Type test Operators
– These operators are handy for checking types at runtime.
Gives boolean value true
is is as output if the object has
specific type
Gives boolean value false
is! is not as output if the object has
specific type
void main() {
String a = 'ABC';
double b = 3.3;

// Using is to compare
print(a is String);

// Using is! to compare The output:


print(b is! int); true
22
} true
Conditional Operators
• Dart has two operators that let you evaluate expressions that might
otherwise require if else statements
– condition ? expr1 : expr2
• If condition is true, then the expression evaluates expr1 (and
returns its value); otherwise, it evaluates and returns the value of
expr2.
• expr1 ?? expr2
• If expr1 is non-null, returns its value; otherwise, evaluates and
returns the value of expr2
void main() {
var a = 10;
var res =
a > 12 ? "value greater than 10" : "value lesser than or equal to 10";
print(res);
} Output: value lesser than or equal to 10 23
Decision Making
• A conditional/decision-making construct evaluates a condition before the
instructions are executed.
• if statement An if statement consists of a Boolean expression followed by one or
more statements.
• If...Else Statement An if can be followed by an optional else block. The else block
will execute if the Boolean expression tested by the if block evaluates to false.
• Switch case Statement The switch statement evaluates an expression, matches the
expressions value to a case clause and executes the statements associated with
that case.

24
Loops
• A loop represents a set of instructions that must be repeated.
• A loop whose number of iterations are definite/fixed is termed as a definite
loop.

• for loop The for loop is an implementation of a definite loop. The for loop
executes the code block for a specified number of times. It can be used to
iterate over a fixed set of values, such as an array.
• The for...in loop is used to loop through an object's properties.

void main() {
for (int i = 0; i < 5; i++) { void main() {
print('Hello Programmers'); var ar = [1, 2, 3, 4, 5];
} for (int i in ar) {
} print(i);
}
}
25
While
• An indefinite loop is used when the number of iterations in a loop is indeterminate
or unknown. Indefinite loops can be implemented using:
• While Loop The while loop executes the instructions each time the condition
specified evaluates to true. In other words, the loop evaluates the condition before
the block of code is executed.

void main()
{
var x = 4;
int i = 1;
while (i <= x) {
print('Hello Programmers');
i++;
}
}

26
do...while
• The do while loop is similar to the while loop except that the do...while
loop does not evaluate the condition for the first time the loop executes.

void main()
{
var x = 4;
int i = 1;
do {
print('Hello Programmers');
i++;
} while (i <= x);
}

27
Break & Continue
• break Statement The break statement is used to take the control out of a
construct. Using break in a loop causes the program to exit the loop. Following is
an example of the break statement.
• continue Statement The continue statement skips the subsequent statements in
the current iteration and takes the control back to the beginning of the loop.

void main() {
void main() { int count = 0;
int count = 1;
while (count <= 10) { do {
print("You in loop $count"); count++;
count++; if (count == 4) {
if (count == 4) { print("Number 4 is skipped");
break; continue;
} }
} print("you in loop $count");
print("Now, you are out"); } while (count <= 10);
} print("you are out of loop");
28
}
Using Labels to Control the Flow
• A label is simply an identifier followed by a colon (:) that is applied to a statement
or a block of code. A label can be used with break and continue to control the flow
more precisely.

void main() {

// Defining the label


Label1:for(int i=0; i<3; i++)
{
if(i < 2)
{
print("You are inside the loop");

// Continue or break to Label1


continue Label1;
}
print("You are still inside the loop");
}
29
}
Dart - keywords
• keywords in dart are reserved words that are defined by language and have a special meaning
defined by the compiler.
• They can not be used as variables and method names.
• The following are keywords.

Abstract modifier return


assert patch
as export
async and await finally
break on
case interface
const
where
final
yield
new
with
const
get external
set is
library var
package implements
required

30
Questions?

31
Andrew Ng

You might also like