0% found this document useful (0 votes)
5 views

Lecture 3 Dart

This document provides an overview of Dart programming, focusing on loops, functions, and classes. It explains the syntax and usage of for, while, and do-while loops, as well as defining and calling functions, and creating classes and objects in Dart. Examples are provided to illustrate each concept, emphasizing Dart's object-oriented features.

Uploaded by

faizanrafique356
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 3 Dart

This document provides an overview of Dart programming, focusing on loops, functions, and classes. It explains the syntax and usage of for, while, and do-while loops, as well as defining and calling functions, and creating classes and objects in Dart. Examples are provided to illustrate each concept, emphasizing Dart's object-oriented features.

Uploaded by

faizanrafique356
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

LECTURE 2: DART

CET-222
SIDRA KHATOON
DART – LOOPS

• A looping statement in Dart or any other programming


language is used to repeat a particular set of commands
until certain conditions are not completed. There are
different ways to do so. They are:
for loop
while loop
do-while loop
FOR LOOP

• For loop in Dart is similar to that in Java or any other language and also
the flow of execution is the same as that in Java or any other language.
• Syntax:
for(initialization; condition; text expression){
// Body of the loop
}
CONTROL FLOW

• Control flow goes as:


 initialization
 Condition
 Body of loop
 Test expression

• The first is executed only once i.e in the beginning while the other
three are executed until the condition turns out to be false.
EXAMPLE

// Printing Hello World! 5 times


void main()
{
for (int i = 0; i < 5; i++) {
print(‘Hello World!');
}
}
WHILE LOOP

• The body of the loop will run until and unless the condition is true.
• Expression:
while(condition){
text expression;
// Body of loop
}
EXAMPLE

void main()
{
var a = 4;
int i = 1;
while (i <= a) {
print('Hello World!');
i++;
}
}
DO-WHILE LOOP

• The body of the loop will be executed first and then the condition is
tested.
• Syntax:
do{
text expression;
// Body of loop
}while(condition);
EXAMPLE

void main()
{
var a = 4;
int i = 1;
do {
print('Hello World!');
i++;
} while (i <= a);
}
DART – FUNCTIONS

• The function is a set of statements that take inputs, do some specific


computation and produces output.
• Functions are created when certain statements are repeatedly
occurring in the program and a function is created to replace them.
• Functions make it easy to divide the complex program into smaller sub-
groups and increase the code reusability of the program.
DEFINING THE FUNCTION IN DART

• Dart provides us with the facility of  return value: defines the value
using functions in its program. to be returned from the function.

• In the below syntax:


 function_name: defines the
name of the function.

 return_type: defines the


datatype in which output is going
to come.
DEFINING THE FUNCTION IN DART(CONT.)

• How to Call Functions in Dart?

• In the above syntax:


• function_name: defines the name of the function.
• argument list: is the list of the parameters that the function requires.
EXAMPLE

int add(int a, int b){ void main(){


// Creating function // Calling the function
int result = a + b; var output = add(10, 20);
// returning value result
return result; // Printing output
} print(output);
}
DART – CLASSES AND OBJECTS

• Dart is an object-oriented programming language, so it supports the


concept of class, object … etc.
• In Dart, we can define classes and objects of our own. We use
the class keyword to do so. Dart supports object-oriented
programming features like classes and interfaces.
CLASSES IN DART

• Class is the blueprint of objects and class is the collection of data


members and data function means which include these fields, getter
and setter, and constructor and functions.

• Declaring class in Dart

class class_name {
// Body of class
}
CLASSES IN DART(CONT.)

• In the above syntax:


 Class is the keyword used to initialize the class.

 class_name is the name of the class.

 The body of the class consists of fields, constructors, getter and setter
methods, etc.

• The body of Constructor includes three things: Class Fields, Class


Methods, and Constructors.
1: CLASS FIELDS IN DART

• Classes Fields are the variables which data for the objects. Let us check
with an Example:
class Student{
// Fields defining the
// Properties of Class
int? roll_no;
String? name;
}
2: CLASS METHODS IN DART

• Class Methods are the int? roll_no;


functions that provide String? name;
behavior for an object. Let us
check with a Example: void print_name(){
class Student{
print("Student Name:
// Fields defining the $name");
// Properties of Class }
}
3: CONSTRUCTORS IN DART

• A Constructor is a block of code that initializes the state and values


during object creation. Constructor is name same as the class name
and doesn’t return any value.
• Syntax:
class_name( [ parameters ] ){
// Constructor Body
}

You might also like