In dart, we found two type of decision controlling instructions – if-else statements and switch statements. Here we will discuss about if-else part of dart. We put conditions into if/else-if statements. Condition gets Boolean expression which returns either true or false.
if statement
In dart, for decisions control we can use if statements.
Syntax –
if (condition){ statements; statements; } |
Let’s see some coding examples…
Write a code, which will check integer variable ‘a’ value is 10 or not.
Code 1:
void main(){ int a=10; // simple if if (a==10) { print(‘a is 10’); } } |

Write a code, which will check String variable ‘name’ value “microcodes” or not.
Code 2:
void main(){ String name; name=”microcodes”; // check both string equal or not. if (name==”microcodes”) { print(‘string matched!’); } } |

Nested if
It’s simply means if statement inside another If statement. When we have multiple conditions then sometime we can use nested if block. Let’s understand through an example…
Syntax –
If(condition1){ If(condition2){ Statements; } } |
Make a code using nested-if statements, which will response on those persons which age between 18-30.
Code 3:
void main(){ int age=22; if (age>=18) { // nested if if (age<=30) { print(‘age between 18-30’); print(‘age is’); print(‘$age’); } } } |

Else statement
When if block’s condition not satisfies then else block get executed.
Syntax –
if (condition){ statements; } else{ statements; } |
Let’s understand through a coding example…
Code 4:
void main(){ int a=23; if (a==90) { print(‘a is 90’); } else { // else block print(‘a is 23’); } } |

Write a code, which checks if name will ‘bill’ then print message ‘bill invented windows’; else name will ‘linus’ then print message ‘linus invented linux kernel’.
Code 5:
import ‘dart:io’; void main(){ String? name; print(‘Enter Name: ‘); name=stdin.readLineSync(); if (name==”bill”) { print(‘bill invented windows’); } else{ print(‘linus invented linux kernel’); } } |

Write a code, which checks a double variable ‘per’, value is 80.5 or not.
Code 6:
import ‘dart:io’; void main(){ String? str; late double per; // getting string input print(‘Enter value: ‘); str=stdin.readLineSync(); if (str!=null) { // converts string->double per=double.parse(str); } // condition checks if (per==80.4) { print(‘percentage is 80.4’); } else { print(‘percentage is 80.5’); } } |

Else if statement
When we want to handle multiple conditions, then we can use else-if statements. Let’s understand use case of else-if statements.
Syntax –
if (condition){ statements; } else if (condition){ statements; } |
Make a code, where it takes user’s phone number country code. There have three countries – India (91), Pakistan (92), and Bangladesh (93). According to user’s input it will print user’s country.
Code 7:
import ‘dart:io’; void main(){ String? str; late int a; // taking input print(‘Enter Country code: ‘); str=stdin.readLineSync(); // converts String->int if (str!=null) { a=int.parse(str); } // country code condition checks if (a==91) { print(‘india’); } else if(a==92){ print(‘Pakistan’); } else if(a==93){ print(‘bangladesh’); }} |

Nested if-else statement
When we want to handle multiple conditions then we can use nested if-else statements.
Let’s understand through below code example…
Code 8:
import ‘dart:io’; // A code, which return message ‘got it!’, if value between -5 to +5 void main(){ String? str; late int a; print(‘Enter value: ‘); str=stdin.readLineSync(); if (str!=null) { a=int.parse(str); } if (a>=-5) { // nested if-else block if (a<=5) { print(‘got it!’); } else{ print(‘2nd condition not match!’); } } else{ print(‘enter valid number!’); } } |

Work with Logical Operators
Till now, we have seen if, else if, else statements using relational operators like > (greater than) <(less than) >=(greater than equal) !=(not equal) etc.
Now we will perform logical operations over if-else statements.
Logical AND [ && ] – if both inputs same then output same.
Code 9:
import ‘dart:io’; void main(){ String? str; late int a; print(‘Enter value: ‘); str=stdin.readLineSync(); if (str!=null) { a=int.parse(str); } // checks integer(a) between 20 – 30 if (a>=20 && a<=30) { print(‘a between 20 and 30’); } else{ print(‘condition not match!’); } } |

Write a code, which will check a string name start with ‘linus’ and end with ‘torvalds’.
Code 10:
import ‘dart:io’; void main(){ String? fname,lname; print(‘Enter first name: ‘); fname=stdin.readLineSync(); print(‘Enter last name: ‘); lname=stdin.readLineSync(); if (fname==”linus” && lname==”torvalds”) { print(‘name is’); print(‘$fname $lname’); print(‘invented linux’); } else{ print(‘oops!’); } } |

Logical OR – if at-least one input gets true then output same. This can be represented as ||
Let’s see logical OR operation, with integers of it…
Code 11:
import ‘dart:io’; void main(){ String? str; int a=10; print(‘Enter value: ‘); str=stdin.readLineSync(); if (str!=null) { a=int.parse(str); } // first condition a==20(false) but a==10(true) if (a==20 || a==10) { print(‘a either 20 or 10’); } else{ print(‘condition not match!’); } } |

Let’s see logical OR operation on String
Code 12:
import ‘dart:io’; void main(){ String? str; var name; print(‘Enter String: ‘); str=stdin.readLineSync(); name=str; // first condition name==’java'(false) but name==’false'(true) if (name==”java” || name==”dart”) { print(‘name either java or dart lang.’); } else{ print(‘condition not match!’); } } |
