-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics2.dart
215 lines (170 loc) · 5.19 KB
/
basics2.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import 'dart:core'; //automatically imported
import 'dart:io';
// NULL AWARE OPERATORS --> ?. , ?? , ??=
class Num {
int num = 10;
}
main() {
//.............................................................................................................
// NULL AWARE OPERATORS --> ?. , ?? , ??=
// ?. --> used to call a method/getter on an object that is not null.
var n = Num();
var m;
int number;
// if ( n != null)
// number = n.num;
number = n?.num;
print('');
print(number); // o/p --> 10 (coz var n is instance of Num)
number = m?.num;
print(number); // o/p --> null (coz var n is not instantialized)
print('');
// ?? --> returns the value on its right if expression on its left in null.
number = n?.num ?? 0; // o/p --> 10 (returns value on left(i.e. 10) as it is not null)
print(number);
number = m?.num ?? 0; // returns 0 as value of expression on left is null.
print(number);
print('');
//??= (assignment operator) --> used to assign value when the variable is currently null.
number = null;
print(number ??= 100); // o/p --> 100 (coz number is null so 100 is assigned to it)
print(number);
print('.........................................................\n');
//.............................................................................................................
// TERNARY OPERATOR
var x = 100;
var result = x % 2 == 0 ? 'Even' : 'Odd';
print(result);
print('.........................................................\n');
//.............................................................................................................
// TYPE TEST OPERATOR
if ( x is int) // tests the type of variable x.
print('Integer');
print('.........................................................\n');
//.............................................................................................................
// CONDITIONAL STATEMENT
number = 91;
// if else ladder
if (number % 2 == 0)
print('even');
else if (number % 3 == 0)
print('divisible by 3.');
else
print('Confused!');
print('');
// Switch case
number = 0;
switch(number) {
case 0: print('even');
break;
case 1: print('odd');
break;
default: print('confused!');
}
print('.........................................................\n');
//.............................................................................................................
// LOOP
// for loop
print('for loop');
for (var i = 1; i <= 5; i++)
print(i);
print('');
// for-in loop
var numbers = [1, 2, 3];
print('for-in loop');
for (var i in numbers)
print(i);
print('');
// for-each loop
print('for-each loop');
numbers.forEach( (i) => print(i) );
print('');
// while loop
number = 5;
print('while loop');
while(number > 0) {
print(number);
number--;
}
print('');
// do while loop
number = 5;
print('do while loop');
do {
print(number);
number--;
} while(number > 0);
print('');
// break
print('break');
for(var i = 1; i< 10; i++) {
if (i > 5)
break;
print(i);
}
print('');
// continue
print('continue');
for(var i = 0; i < 10; i++) {
if (i % 2 == 0)
continue;
print('Odd: $i');
}
print('');
print('.........................................................\n');
//.............................................................................................................
// COLLECTION (LIST , SET , MAP)
// LIST --> ordered collection of values
List <String> names = ['jack', 'jill'];
// var names = ['jack', 'jill']; // can also use var
print('LIST-----');
print(names[0]);
print(names.length);
print('');
for(var n in names)
print(n);
print('');
var names2 = names; // doesnot create a new list but point to the same list
var names3 = [...names]; // ... --> spread operator (this will ceate a new list instead of pointing to the same list)
for(var n in names2)
print(n);
print('');
names[0] = 'preetha';
for(var n in names2)
print(n);
print('');
List anyThing =['bsbfg', 'vdfsvdf', 1212, 33.43];
anyThing[1] = 'preetha';
for(var n in anyThing)
print(n);
print('');
// SET --> unordered collection of unique values
var halogens = <String>{'flourine', 'chlorine', 'flourine'}; // flourine is stored only once as set stores only unique values
Set <String> halogen_names = {};
print('SET-----');
for(var n in halogens)
print(n);
print('');
print(halogens.runtimeType);
print(halogen_names.runtimeType);
print('');
// MAP
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
print('MAP-----');
print(gifts['fifth']);
print('');
var gifts2 = {
// Key: Value
2: 'partridge', 1: 'turtledoves', 3: 'golden rings' };
print(gifts2[1]);
print('');
var gifts3 = Map();
gifts3['first'] = 'mango';
print(gifts3['first']);
print('');
}