Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vraj6198/e79d62c0acd6bf7797ef6c30ef8a0e10 to your computer and use it in GitHub Desktop.
Save vraj6198/e79d62c0acd6bf7797ef6c30ef8a0e10 to your computer and use it in GitHub Desktop.
Basics of dart language
//BREAK stmt
// break keyword
// using label here we can use outerloop for break stmt
void main(){
myouterloop: for(int i=1; i<=3; i++){
innerloop: for(int j=1; j<=3; j++){
print("$i $j");
if(i == 2 && j == 2)
break myouterloop;
}
}
}
// CONTINUE KEYWORD
// CONTINUE keyword
// using label
void main(){
outer: for(int i=1; i<=3; i++){
innerloop: for(int j=1; j<=3; j++){
if(i == 2 && j == 2){
continue innerloop;
}
print("$i $j");
}
}
}
void main(){
// condition expression
// 1. condition ? exp1 : exp2
//if condition is true then exp1 execute otherwise evaluate and return the value of exp2.
var a =2;
var b= 3;
int smallnumber;
/* if(a>b)
print("$a is greater");
else
print("$b is greater");
a>b ? print("$a is greater") : print("$b is greater");
smallnumber = condition ? exp1 : exp2 */
smallnumber = a<b ? a : b;
print("$smallnumber is smaller");
// exp1 ?? exp2
// if expression is non-null, return it's value; otherwise evaluate and return value of exp2.
String name = "peter";
String toprint = name ?? "guest user";
print(toprint);
}
void main(){
int age=10;
String name="vraj";
double percentage=67.666;
double hexvalue= 0xEEAABBCCDD;
double expo = 1.22e45;
bool isvalid = true;
print(age);
print(name);
print(percentage);
print(hevalue);
print(expo);
print(isvalid);
}
/* Constants: final and const keyword
-> If you never want to change a value then use final and const keyword.
-> final name="peter";
const PI = 3.14;
-> difference between final and const
-> final variable can only be set once and it is initialized when accessed.
-> const variable is implicitly final but it is a compile time constant
-> i.e. it is initialized during compilation time
-> instance variable can be final but cannot be const.
-> if a constant at class level then make it static const
void main(){
// final keyword
final cityname = 'vadodara';
final String nickname = 'Bobby';
// cityname = 'vadodara'; we cannot assign again cityname
final String countryname = 'India';
print(cityname);
print(nickname);
print(countryname);
// const keyword
const PI = 3.14; // const double PI = 3.14;
print(PI);
const double gravity = 9.8;
print(gravity);
}
class circle{
final color = 'red';
static const PI = 3.14; // here in class you have to write static in front of const
}
void main(){
// IF AND ELSE STATEMENTS
var salary = 50000;
if(salary > 5000 )
print("true");
else
print("false");
// if else if ladder statment
var marks = 1000;
if (marks >= 80 && marks <100){
print("grade A+");
}
else if(marks >= 60 && marks < 79)
{
print("grade B"); }
else if(marks<=40)
{
print("fail");
}else
{
print("invalid marks");
}
}
/* Iterator:
Loop: definite-> for loop
Indefinite -> while loop and do while loop */
// FOR LOOP
void main(){
for(int i=0; i<=10; i++)
{
if(i % 2 == 0){
print(i);
}
}
// for...in loop
List planetlist = ["mercury","venus","earth","mars"];
for(String planet in planetlist)
print(planet);
}
// WHILE LOOP
/* WHILE LOOP SYNTAX
initializer counter
while (condition){
put executes code
increment and decrment
} */
void main(){
int i = 0;
while ( i<=10){
if(i % 2 == 0){
print(i); }
i++;
}
}
// DO-WHILE LOOP
void main(){
var i = 0;
do{
if(i % 2 == 0){
print(i); }
i++;
}while(i<=20);
}
void main(){
// literal
var isvalid = true;
var x = 2;
var value = 45.55;
"hello";
//various way to define string literal in dart
String s1 = 'single';
String s2 = "double";
String s3 = 'It\'s easy'; // here we have use escap character so we use backslash now it read full stmt
String s4 = "It's easy"; // here we have use double slash and it read full stmt
// large statment (we don't have to use + sign to add two stmt)
String s5 = 'this is going to be a very long string.'
'this is just a sample string demo in dart programming language.';
// string interpolation : use ["my name is $name"] instead of ["my name is + name"]
String name = "vraj";
/* In dart language using + sign is very bad impression so here string interpolation come into picture
String message = "my name is " + name; */
// String message = "my name is $name";
print("my name is $name");
// print("the number is character in String vraj is " + name.length.toString() );
print("the number is character in String vraj is ${name.length}");
int l = 20;
int b = 10;
print("the sum of l and b is ${l+b}");
print("the sum of $l and $b is ${l+b}");
print("the area of the rectangle is ${l*b}");
}
void main(){
// switch case stmt is applicable for only 'int' and 'String'. here bool and double value is not allowed
var grade = 'A';
switch(grade)
{
case 'A':
print("excellent grade");
break;
case 'B':
print("good work");
break;
case 'C':
print("good work but work hard");
break;
case 'D':
print("you have failed");
break;
default : print("exit");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment