void main(List<String> args) {
String firstName = 'John';
String lastName = 'Jones';
int age = 36;
double height = 1.84;
print(firstName);
print(lastName);
print(age);
print(height);
double temperature = 20;
int value = 2;
String pizza = 'pizza';
String pasta = 'pasta';
print('The temperature is ${temperature.toStringAsFixed(0)}C');
print('$value plus $value makes ${value + value}');
print('I like $pizza and $pasta');
print("Today I'm feeling great");
print('Today I\'m feeling great');
print('\\');
print('\$');
print(r'C:\Windows\system32');
// Should you prefer 'single' or "double" quotes?
// Most existing Dart code uses 'single' quotes.
// Be consistent.
print("""
This is an even longer sentence, with some things in
it.
""");
// Use multi-line strings to hardcode long chunks of text in your programs.
String name = 'John';
print(name.toUpperCase());
// print -> this is a function
// .toUpperCase() -> this is a method
String lovePizza = 'I love pizza.';
print(lovePizza.contains(pizza));
// Choose meaningful variable names, based on the values they contain.
// This makes your code easier to understand.
int myAge = 36;
String ageString = myAge.toString();
print(ageString);
String ratingString = '4.5';
double rating = double.parse(ratingString);
print(rating);
print(5 ~/ 2); // division operator
double tempFarenheit = 86;
double tempCelsius = (tempFarenheit - 32) / 1.8;
print(
'${tempFarenheit.toStringAsFixed(0)}F = ${tempCelsius.toStringAsFixed(0)}C');
int x = 1;
int y = x++; // Postfix increment operator
// First: assign x to y
// Then: increment x
print('x: $x, y: $y'); // x: 2, y: 1
int a = 1;
int b = ++a; // Prefix increment operator
// First: increment x
// Then: assign x to y
print('a: $a, b: $b'); // a: 2, b: 2
print('');
int q = 1;
int w = q++;
int e = --w;
print('q: $q, w: $w, e: $e'); // q = 2, w = 1, e = 0
/*
bool result = expr1 == expr2
!=
>=
>
<=
<
Relational operator are well suited for conditional logic.
(execute some code only when a condition is true/false)
*/
print(5 < 2 || 5 < 7); // true
print(5 < 2 && 5 < 7); // false
print('');
String email = 'test@example.com';
print(email.isNotEmpty && email.contains('@')); // true
/*
variable = expr1 ? expr2 : expr3
true -> [X]
false -> [X]
*/
int someValue = 40;
String type = someValue > 30 ? 'over 30' : 'under 30';
print(type);
}
// 결과
John
Jones
36
1.84
The temperature is 20C
2 plus 2 makes 4
I like pizza and pasta
Today I'm feeling great
Today I'm feeling great
\
$
C:\Windows\system32
This is an even longer sentence, with some things in
it.
JOHN
true
36
4.5
2
86F = 30C
x: 2, y: 1
a: 2, b: 2
q: 2, w: 0, e: 0
true
false
true
over 30
void main(List<String> args) {
const person = {
'name': 'John',
'age': 36,
};
if (person['address'] == null) {
print('address is missing');
} else {
print(person['address']);
}
int? a;
int b = 3;
if (a == null) {
print(' a is null');
} else {
print(a + b);
}
print('');
int x = 10;
int? maybeValue;
if (x > 0) {
maybeValue = x;
}
int value = maybeValue!;
print(value);
// If we're sure that a nullable variable will wlways have a non-nullable value,
// we can use the assertion operator.
// if-null operator
int? anyValue;
var somevalue = anyValue == null ? 0 : anyValue;
print(somevalue);
int? nullValue;
print('-----' * 3);
var thatValue = nullValue ?? 0;
print(thatValue);
print('-----' * 3);
int? nullValue2;
nullValue2 ??= 0;
print(nullValue2);
print('-----' * 3);
const cities = <String?>['London', 'Paris', null];
for (var city in cities) {
if (city != null) {
print(city);
}
}
print('-----' * 3);
for (var city in cities) {
print(city?.toUpperCase());
}
// Calling methods on null variables is a very common mistake
// (easy to forget null checks)
}
/*
null: no value
(can be very useful)
You're likely to work on code written before Null Safety.
Dart without Null Safety -> Runtime error
Dart with Null Safety -> Compile-time error
When to use ! vs ??
-> use ?? if you have a default (or fallback) value
-> use ! if you're sure that your expression/value won't be null at runtime
Should we use type inference or declare types explicitly?
-> initialize variables when you declare them
Type inference and Null Safety work well with each other
Every time you declare a variable, think about whether
it should be nullable or not.
*/
// 결과
address is missing
a is null
10
0
---------------
0
---------------
0
---------------
London
Paris
---------------
LONDON
PARIS
null
/*
Exercise 1
*/
void main(List<String> args) {
String name = 'Paul';
int age = 35;
int price = 400;
print("$name, a $age year old, paid \$$price to repair his father's car.");
print('');
double tempCityEarly = 18.5;
double tempCityLate = 25.7;
print(
'The average temperature for the day is ${(tempCityEarly + tempCityLate) / 2} degrees celcius.');
print('');
String lastName = 'Peters';
double salary = 1555.35;
print('$lastName (age $age), Salary: \$$salary pm');
String carInfo = 'Honda, Ballade, 2006 model, Automatic';
print(carInfo.toUpperCase());
String message = 'Hi my friend. You are the bomb!';
print(message.contains('bomb'));
print(message.replaceAll(' ', '_'));
print('');
int nrOfPeople = 20;
String nrOfPeopleText = nrOfPeople.toString();
print(nrOfPeopleText);
print('');
double temp = 38.547;
String tempText = temp.toStringAsFixed(2);
print(tempText);
print('');
String ageText = '20';
int ageInt = int.parse(ageText);
print(ageInt);
print('');
double nightTemp = 38.67;
int tempInt = nightTemp.truncate();
print(tempInt);
print('');
int peopleAtShop = 20;
peopleAtShop--;
print(peopleAtShop);
print('');
int y = 10;
print(++y); // 11
print(y); // 11
int z = 10;
print(z++); // 10
print(z); // 11
}
// 결과
Paul, a 35 year old, paid $400 to repair his father's car.
The average temperature for the day is 22.1 degrees celcius.
Peters (age 35), Salary: $1555.35 pm
HONDA, BALLADE, 2006 MODEL, AUTOMATIC
true
Hi_my_friend._You_are_the_bomb!
20
38.55
20
38
19
11
11
10
11
void main(List<String> args) {
int age = 15;
bool id = true;
print('Eligibale to rent movies? ${age > 20 && id == true}');
String type = 'bike';
print(
'You will pay ${type == 'bike' ? '\$10' : type == 'Car' ? '\$20' : '\$30'} to enter the Wild Life Park.');
String email = 'peter@gmail.com';
print('Valid email address? ${email.contains('@') && email.contains('.')}');
const firstName = 'Peter';
const lastName = 'Johnson';
var fullName = '$firstName $lastName';
final fullNameLength = fullName.length.toString();
fullName = 'Peter Pollock'; // no error for assign.
}
// 결과
Eligibale to rent movies? false
You will pay $10 to enter the Wild Life Park.
Valid email address? true
void main(List<String> args) {
const finalMark = 87;
if (finalMark <= 100 && finalMark >= 80) {
print('Grade: A');
} else if (finalMark < 80 && finalMark >= 70) {
print('Grade: B');
} else if (finalMark < 70 && finalMark >= 60) {
print('Grade: C');
} else if (finalMark < 60 && finalMark >= 50) {
print('Grade: D');
} else if (finalMark < 50 && finalMark >= 40) {
print('Grade: E');
} else if (finalMark < 40 && finalMark >= 0) {
print('Grade: F');
} else {
print('Invalid mark');
}
}
// 결과
Grade: A
void main(List<String> args) {
// while loop: pre-test loop (runs 0 or more times)
print('\n******************************\n');
// 1. Declare and initialize your loop control variable
int x = 0;
// 2. Test the loop control variable
while (x < 3) {
print('hello world');
x++; // 3. Change the loop control variable
}
print('\n******************************\n');
// do-while loop: post-test loop (runs 1 or more times)
print('\n******************************\n');
int y = 0;
do {
print('hello world');
y++;
} while (y < 3);
print('\n******************************\n');
// for loop: pre-test loop (runs 0 or more times)
print('\n******************************\n');
for (int i = 0; i < 3; i++) {
print('hello world');
}
print('\n******************************\n');
}
// 결과
******************************
hello world
hello world
hello world
******************************
******************************
hello world
hello world
hello world
******************************
******************************
hello world
hello world
hello world
******************************
import 'dart:io';
void main(List<String> args) {
String name;
do {
stdout.writeln('Please enter your name: ');
stdout.writeln('[info] Only enter to exit.');
name = stdin.readLineSync()!; // sound null safety
print('Your name is $name');
} while (name != '');
}
import 'dart:io';
// input validation
void main(List<String> args) {
print('Please enter a number in the range of 1 through 100');
int number = int.parse(stdin.readLineSync()!);
while (number < 1 || number > 100) {
print('The number is not valid!');
print('Please enter a number in the range of 1 through 100');
number = int.parse(stdin.readLineSync()!);
}
print('$number is valid!');
}
// 결과
Please enter a number in the range of 1 through 100
0
The number is not valid!
Please enter a number in the range of 1 through 100
50
50 is valid!
import 'dart:io';
// user controlled
void main(List<String> args) {
print('How high should I go to square numbers?');
int nrOfTimes = int.parse(stdin.readLineSync()!);
print('Value Value Squared');
print('--------------------------');
for (int i = 1; i <= nrOfTimes; i++) {
print('$i ${i * i}');
}
}
// 결과
How high should I go to square numbers?
3
Value Value Squared
--------------------------
1 1
2 4
3 9
import 'dart:io';
void main(List<String> args) {
int days;
double sales;
double totalSales = 0.0;
print('For how many do you have sales figures?');
days = int.parse(stdin.readLineSync()!);
for (int count = 1; count <= days; count++) {
print('Enter the sales for day $count');
sales = double.parse(stdin.readLineSync()!);
totalSales += sales;
}
print('the total sales for $days are R${totalSales.toStringAsFixed(2)}');
}
// 결과
Enter the sales for day 8
1
Enter the sales for day 9
2
Enter the sales for day 10
23
the total sales for 10 are R51.00
// sentinel values
import 'dart:io';
void main(List<String> args) {
int value;
int doubleValue;
print('Please enter a value to double or 0 to stop');
value = int.parse(stdin.readLineSync()!);
while (value != 0) {
doubleValue = value * 2;
print('$value doubles is $doubleValue');
value = int.parse(stdin.readLineSync()!);
}
}
// 결과
Please enter a value to double or 0 to stop
10
10 doubles is 20
2
2 doubles is 4
0
import 'dart:io';
void main(List<String> args) {
print('Please enter your email address');
String email = stdin.readLineSync()!;
while (!email.contains('@') || !email.contains('.')) {
print('Email not valid!');
print('Please re-enter your email address...');
email = stdin.readLineSync()!;
}
print('$email is valid email address.');
}
// 결과
Please enter your email address
my@example
Email not valid!
Please re-enter your email address...
myemail.com
Email not valid!
Please re-enter your email address...
myemail address
Email not valid!
Please re-enter your email address...
my@example.com
my@example.com is valid email address.
import 'dart:io';
void main(List<String> args) {
int choice;
do {
print('\n');
print('Make your choice:');
print('1. McDonald\'s Fries');
print('2. McDonald\'s Big Mac');
print('3. McDonald\'s Breakfast Muffin');
print('4. Exit');
print('\n');
choice = int.parse(stdin.readLineSync()!);
switch (choice) {
case 1:
print('You chose fries');
break;
case 2:
print('You chose big mac');
break;
case 3:
print('You chose breakfast muffin');
break;
case 4:
print('Good bye');
break;
default:
print('Invalid choice! Please try again!');
}
} while (choice != 4);
}
// 결과
Make your choice:
1. McDonald's Fries
2. McDonald's Big Mac
3. McDonald's Breakfast Muffin
4. Exit
1
You chose fries
Make your choice:
1. McDonald's Fries
2. McDonald's Big Mac
3. McDonald's Breakfast Muffin
4. Exit
6
Invalid choice! Please try again!
Make your choice:
1. McDonald's Fries
2. McDonald's Big Mac
3. McDonald's Breakfast Muffin
4. Exit
4
Good bye
댓글 영역