dart programming language
Learn from examples
1. Program to declare and output integer, double,
and string
exda1.dart source code
// exda1.dart first example declare and output integer, double, string
void main()
{
print("exda1.dart running");
var i = 1;
var x = 1.2;
var y = 9.9999999e+99;
var s = "string";
var ss = 'string2';
int j = 2;
num k = 3;
double z = 7.5; // no float
final int n = 10; // can not change value
const double q = 3.14; // compile time constant
const double Q = 3.1415; // compile time constant
var very_long_name_for_variable = 99;
var vec1 = [1, 2, 3];
var vec2 = [1.2, 2.3, 3.4];
var vec3 = [ "a", 'b', 'cde'];
print("i= $i");
print("x= $x");
print("y= $y");
print("s= $s");
print("ss= $ss");
print("j= $j");
print("k= $k");
print("z= $z");
print("n= $n");
print("q= $q");
print("Q= $Q");
print("very_long_name_for_variable = $very_long_name_for_variable");
print("vec1= $vec1");
print("vec2= $vec2");
print("vec3= $vec3");
vec3[1] = "aa";
print("vec3= $vec3");
print("vec3[1]= $vec3[1]");
print(" need { } for expression");
print("vec3[1]= ${vec3[1]}");
print(' '); // blank line
print("exda1.dart finished");
}
Output from execution:
exda1.dart running
i= 1
x= 1.2
y= 9.9999999e+99
s= string
ss= string2
j= 2
k= 3
z= 7.5
n= 10
q= 3.14
Q= 3.1415
very_long_name_for_variable = 99
vec1= [1, 2, 3]
vec2= [1.2, 2.3, 3.4]
vec3= [a, b, cde]
vec3= [a, aa, cde]
vec3[1]= [a, aa, cde][1]
need { } for expression
vec3[1]= aa
exda1.dart finished
2. commands to execute the source code
at a minimum, Windows, Linux, MacOSX.
Windows: optional write output to file
dart exda1.dart dart exda1.dart > exda1_dart.out
Linux:
dart exda1.dart
MacOSX
dart exda1.dart
3. You must be able to declare variables and arrays
and matrix of various types.
exda3.dart source code
// exda3.dart declare vectors and matrix of various types
void main()
{
print("exda3.dart running");
int n = 5; // vector of 5 items
var vec = new List.filled(n,0.0); // n,0 integer array
print("vec all 0.0");
print(vec);
for(var i=0; i List.generate(rows + 1, (j) => 0.0,
growable: false), growable: false);
print("cols= $cols rows= $rows");
print("array generated with all elements zero");
for (var i = 0; i < cols; i++)
{
for (var j = 0; j < rows; j++)
{
print("array[ $i ][ $j ]= ${array[i][j]}");
array[i][j] = (i+0.0)*rows+j; // initalize
}
}
print(" ");
print("initialized matrix");
for (var i = 0; i < cols; i++)
{
for (var j = 0; j < rows; j++)
{
print("array[ $i ][ $j ]= ${array[i][j]}");
}
}
print(" ");
print("matrix.dart finished ");
}
Execution output:
exda3.dart running
vec all 0.0
[0.0, 0.0, 0.0, 0.0, 0.0]
vec[ 0 ]= 0.0
vec[ 1 ]= 0.0
vec[ 2 ]= 0.0
vec[ 3 ]= 0.0
vec[ 4 ]= 0.0
exda3.dart finished
matrix.dart running
cols= 4 rows= 3
array generated with all elements zero
array[ 0 ][ 0 ]= 0.0
array[ 0 ][ 1 ]= 0.0
array[ 0 ][ 2 ]= 0.0
array[ 1 ][ 0 ]= 0.0
array[ 1 ][ 1 ]= 0.0
array[ 1 ][ 2 ]= 0.0
array[ 2 ][ 0 ]= 0.0
array[ 2 ][ 1 ]= 0.0
array[ 2 ][ 2 ]= 0.0
array[ 3 ][ 0 ]= 0.0
array[ 3 ][ 1 ]= 0.0
array[ 3 ][ 2 ]= 0.0
initialized matrix
array[ 0 ][ 0 ]= 0.0
array[ 0 ][ 1 ]= 1.0
array[ 0 ][ 2 ]= 2.0
array[ 1 ][ 0 ]= 3.0
array[ 1 ][ 1 ]= 4.0
array[ 1 ][ 2 ]= 5.0
array[ 2 ][ 0 ]= 6.0
array[ 2 ][ 1 ]= 7.0
array[ 2 ][ 2 ]= 8.0
array[ 3 ][ 0 ]= 9.0
array[ 3 ][ 1 ]= 10.0
array[ 3 ][ 2 ]= 11.0
matrix.dart finished
4. You need to be able to have loops, iteration statements
exda4.dart source code
// exda4.dart use loops and interation, for while
void main()
{
print("exda4.dart running");
for(var i=0; i<3; i++)
{
print(i);
}
print(' ');
var vec1 = [3, 4, 5];
for(var k in vec1)
{
print("$k");
}
print(' ');
var year = 2010;
while(year < 2014)
{
print(year);
year += 1;
}
print(' ');
var vec2 = [];
vec2.add(7);
vec2.add(8);
vec2.add(9);
print("vec2=");
print(vec2);
print(' ');
for(var k in vec2)
{
print("$k");
}
print(" ");
for(var i=2; i<10; i++)
{
if(i==4)
{
continue;
}
if(i==8)
{
break;
}
print(i);
}
print(" ");
print("exda4.dart finished");
}
Execution output:
exda4.dart running
0
1
2
3
4
5
2010
2011
2012
2013
vec2=
[7, 8, 9]
7
8
9
2
3
5
6
7
exda4.dart finished
exda4.dart finished
5. You need if then else conditional statements
exda5.dart source code
// exda5.dart
void main()
{
print("exda5.dart running");
var i = 1;
var j = 2;
if(i==j)
{
print("i==j");
}
if(i!=j || j>=1) // or
{
print("i!=j || j>=1");
}
if(i<=j && ii) // and
{
print("i<=j && ii");
}
if(i>j)
{
print("i>j");
}
if(j=1
i<=j && ii
else
exda5.dart finished
6. You need to be able to create functions, procedures,
subroutines.
exda6.dart source code
// exda6.dart functions and prodedures
void proc1()
{
print("proc1 running");
} // end proc1
int factorial(int m)
{
var n = m;
if(n<=1)
{
return 1;
}
var f=1;
while(n>1)
{
f=f*n;
n=n-1;
}
return f;
} // end factorial
void main()
{
print("exda5.dart running");
var result;
proc1();
result = factorial(2);
print("factorial(2)= $result");
result = factorial(3);
print("factorial(3)= $result");
result = factorial(10);
print("factorial(10)= $result");
print("exda6.dart finished");
}
Execution output:
exda5.dart running
proc1 running
factorial(2)= 2
factorial(3)= 6
factorial(10)= 3628800
exda6.dart finished
7. You need to be able to read and write files in various formats.
exda7.dart source code
// exda7.dart read and write file
import 'dart:io';
main() async {
print("exda7.dart running");
print("reads data.txt , prints, writes data-copy.txt");
var file = File('data.txt');
var contents;
if (await file.exists()) {
// Read file
contents = await file.readAsString();
print(contents); // print file
// Write file
var fileCopy = await File('data-copy.txt').writeAsString(contents);
print(await fileCopy.exists());
print(await fileCopy.length());
}
print("exda7.dart finished");
}
data file to read data.txt:
Hello line 1
more text on line 2
// comment on line 3
end line 4
Execution output:
exda7.dart running
reads data.txt , prints, writes data-copy.txt
Hello line 1
more text on line 2
// comment on line 3
end line 4
true
72
exda7.dart finished
8. You need to be able to use a number of files combined to
build a program. This may include packages, libraries,
operating system commands, header files, etc.
exda8.dart source code
Execution output:
Other sample source code and output
sin, cos, ..., exp, log, min, max
import 'dart:math';
trig.dart source code
trig_dart.out output
factorial.dart source code
factorial_dart.out output
test_sort.dart source code
test_sort_dart.out output
Last updated 2/8/2022