Dart programing if-else conditions - TechXplore

Wednesday, 15 February 2023

Dart programing if-else conditions

 In Dart programming, if-else conditions are used for making decisions based on certain conditions. The syntax for if-else condition is as follows:



if condition:

example

void main(){

int a=20;

if(a>4){

  print('The condition is true');

}

}

output:

The condition is true

else -if condition;

example:

void main(){

int login =10;

int data=1;

if(login==data){

print('welcome to app');

}

else{

 print('Please enter correct details');

}

}

output:

Please enter correct details

else-if condition:
example:

void main(){

int log=10;

if(log>10){
  print('condition 1 is correct');
}
  else if(log==10){
    print('condition 2 is correct');

  }
else if(log<=4){
  print('condition 3 is correct');
}
else{
  print('not corrct ans');
}
}
output:
condition 2 is correct

Nested conditions:
example:

void main(){

int log=10;
if(log>=10){
  if(log==10){
    print('The condition is ture');
  }
  else{
    print('The condition is not correct');
  }
}
else{
  print('Please enter correct details');
}

}
output;
The condition is ture

switch case;

In Dart programming, switch is a control statement that allows you to select one of several code blocks to be executed based on the value of an expression. switch statement is a cleaner and more efficient way of writing complex multiple if-else conditions.

examples:

void main(){
  
  int a=1;

  switch(a){
   case 1:{
      print("The number is 1");
      break;
    }
    
    case 2:{
      print("The number 2");
      break;
    }
    case 3:{
      print("The number 3");
      break;
    }
  }

}
output:
The number is 1



No comments:

Post a Comment