Development

다트 프로그래밍 기초 : 변수, 데이터 유형 및 제어 흐름

sonpro 2023. 3. 11. 01:04
반응형

Dart Programming Fundamentals

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 integers
  • double: used to represent floating-point numbers
  • bool: used to represent boolean values (true or false)
  • String: used to represent text
  • List: used to represent a collection of objects
  • Map: 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 true
  • else statement: used to execute a block of code if a condition is false
  • else if statement: used to execute a block of code if a condition is false, but another condition is true
  • for loop: used to execute a block of code repeatedly for a specified number of times
  • while loop: used to execute a block of code repeatedly as long as a condition is true
  • do-while loop: used to execute a block of code repeatedly at least once, and then as long as a condition is true
  • switch 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.

반응형