变量与常量:
变量:
使用var声明变量,可以赋予不同数据类型的值,
未初始化时默认值是null
使用final声明的变量只能被赋值一次
void main(){ var a; print(a); a = 10; print(a); a = 'Hello World'; print(a); var b = 20; print(b);
final c = 20 ;
//c = 1;
const d = 50;
//d = 1;
}
控制台打印结果:
null 10 Hello World 20
常量:
使用const声明常量,使用const声明的必须是编译期的常量。
注意const和final的区别。
数据类型:
内置类型:
数值型num:整型int,浮点型double
void main(){ num a = 10; a = 12.5; int b = 20; //b = 20.1; double c = 30.5; //c = 40; print(c); }
数值类型常见操作:+,-,*,/,~/(取整),%(取余),
数值类型常见属性:isNaN,isEven,isOdd等,
数值类型常见方法:abs(),round(),floor(),ceil(),toInt(),toDouble()
字符串:
使用单引号或者双引号创建字符串,使用三引号或者双引号创建多行字符串,使用r创建原始raw字符串
字符串运算:+,*,==,[]
插值表达式:${expression}
字符串常见属性:length,isEmpty,isNotEmpty
字符串常见方法:
void main() { String str1 = 'Hello'; str1 = '''Hello Dart'''; print(str1); String str4 = 'This is My favorite language'; print(str4+'New'); print(str4*4); print(str4[5]); int a = 1; int b = 3; print('a*b = ${a*b}'); }
布尔型:
列表List(数组):
创建List :var list = [1,2,3];
创建不可变的List: var list = const [1,2,3];
构造创建List: var list = new List();
列表常见操作:
Map:(键值对)
Map常用操作方法
dynamic:动态类型(泛型)可以赋予不同类型的值
运算符:
算术运算符:
void main(){ int a = 10; int b = 2; print(a + b); print(a - b); print(a * b); print(a / b); print(a ~/ b); print(a % b); print(++a);//先加加在打印 print(a++);//先打印再加加 print(a); }
打印结果:
12 8 20 5.0 5 0 11 11 12
关系运算符:
void main (){ int a = 5; int b = 3; print(a == b); print(a > b); print(a < b); print(a != b); print(a >= b); print(a <= b); String strA = '123'; String strB = '321'; print(strA == strB); }
打印结果:
false true false true true false false
逻辑运算符:
赋值运算符:
复合运算,先用左边变量和右边做运算再把值赋给左边变量
void main(){ int a = 10; int b = 5; b ??= a; print(b); a += b; print(a); }
打印结果:
5 15
条件表达式:
void main(){ int gender = 0; String str = gender ==0 ? 'Male' : 'Female'; print(str); String a = 'Php' ; String b = 'Dart'; String c = a ?? b; print(c); }
打印结果:
Male
Php
控制语句:
if语句:
void main(){ int score = 100; if(score >= 90){ if(score == 100){ print('完美'); } else{ print('优秀'); } } else if(score > 60 && score < 90){ print('良好'); } else if (score == 60){ print('及格'); } else { print('不及格'); } }
for语句:
void main(){ var list = [1,2,3,4,5]; for(var i = 0;i < list.length;i++){ print(list[i]); } print('---------------'); for(var item in list){ print(item); } }
打印结果:
1 2 3 4 5 --------------- 1 2 3 4 5
while语句:
void main(){ int count = 0; while(count < 5){ print(count); count++; } print('-----$count------'); do{ print(count); count--; }while(count>0 && count<5); }
打印结果:
0 1 2 3 4 -----5------ 5 4 3 2 1
continue和break:
void main(){ var list = [1,2,3]; for (var item in list){ if(item == 2){ //break; continue; } print(item); } print('--------------'); var list2 = [4,5,6]; for(var item1 in list){ for(var item2 in list2){ if(item2 == 5){ //continue; break; } print(item2); } } }
打印结果:
1 3 -------------- 4 4 4
switch..case:
void main(){ String language = 'php'; switch(language){ case 'Dart': print('Dart is My fav'); break; case 'php': print('php is My fav'); break; case 'java': print('java is My fav'); break; default: print('None'); } print('-------------'); switch(language){ D: case 'Dart': print('Dart is My fav'); break; case 'php': print('php is My fav'); continue D; //break; case 'java': print('java is My fav'); break; default: print('None'); } }
打印结果:
php is My fav ------------- php is My fav Dart is My fav
请发表评论