Dart Programming Fundamentals: Variables, Data Types, and Control Flow
Dart is a modern, object-oriented programming language that is used to build web, mobile, and desktop applications. It is a statically typed language that is easy to learn and use. In this article, we will cover the fundamentals of Dart programming, including variables, data types, and control flow.
Variables
Variables are used to store data in a program. In Dart, variables are declared using the var
keyword. For example:
var name = 'John';
var age = 30;
In the above example, we have declared two variables, name
and age
, and assigned them values of 'John'
and 30
, respectively. Dart also supports type annotations, which allow you to specify the data type of a variable. For example:
String name = 'John';
int age = 30;
In this example, we have specified that name
is a String
and age
is an int
.
Data Types
Dart has several built-in data types, including:
int
: used to represent integersdouble
: used to represent floating-point numbersbool
: used to represent boolean values (true
orfalse
)String
: used to represent textList
: used to represent a collection of objectsMap
: used to represent a collection of key-value pairs
Here is an example of using some of these data types:
int age = 30;
double height = 1.75;
bool isStudent = true;
String name = 'John';
List<String> hobbies = ['reading', 'running', 'coding'];
Map<String, dynamic> person = {
'name': 'John',
'age': 30,
'isStudent': true,
'hobbies': ['reading', 'running', 'coding']
};
In this example, we have declared variables of different data types and assigned them values.
Control Flow
Control flow statements are used to control the execution of a program. Dart has several control flow statements, including:
if
statement: used to execute a block of code if a condition is trueelse
statement: used to execute a block of code if a condition is falseelse if
statement: used to execute a block of code if a condition is false, but another condition is truefor
loop: used to execute a block of code repeatedly for a specified number of timeswhile
loop: used to execute a block of code repeatedly as long as a condition is truedo-while
loop: used to execute a block of code repeatedly at least once, and then as long as a condition is trueswitch
statement: used to execute a block of code based on the value of a variable
Here is an example of using some of these control flow statements:
int age = 30;
if (age >= 18) {
print('You are an adult');
} else {
print('You are a minor');
}
for (int i = 0; i < 5; i++) {
print(i);
}
int i = 0;
while (i < 5) {
print(i);
i++;
}
i = 0;
do {
print(i);
i++;
} while (i < 5);
String fruit = 'apple';
switch (fruit) {
case 'apple':
print('You chose an apple');
break;
case 'banana':
print('You chose a banana');
break;
default:
print('You chose something else');
}
In this example, we have used different control flow statements to control the execution of the program.
Conclusion
In this article, we have covered the fundamentals of Dart programming, including variables, data types, and control flow. Dart is a powerful programming language that is easy to learn and use. By mastering these fundamentals, you will be well on your way to becoming a proficient Dart programmer.
'Development' 카테고리의 다른 글
스프링 프레임 워크와 함께 Java의 디자인 패턴 (0) | 2023.03.11 |
---|---|
Flutter Animation : 사용자 정의 애니메이션으로 앱에 생명을 불어 넣는 방법 (0) | 2023.03.11 |
HTTP Method 에 대하여 알아보기 (0) | 2023.03.11 |
성공적이고 효과적인 팀을 구축하는 방법 (0) | 2023.03.11 |
경력의 스트레스 및 소진 관리 가이드 (0) | 2023.03.10 |