# 老火棍

::  ::  ::  ::  ::
 4 随笔 :: 0 文章 :: 0 评论

# 变量与常量：

## 变量：

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
View Code

# 数据类型：

## 内置类型：

### 数值型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);
}
View Code

}
View Code

Map常用操作方法

# 运算符：

## 算术运算符：

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);
}
View Code

12
8
20
5.0
5
0
11
11
12
View Code

## 关系运算符：

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);
}
View Code

false
true
false
true
true
false
false
View Code

## 赋值运算符：

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

posted on 2019-10-07 10:01  老火棍  阅读(567)  评论(0编辑  收藏