Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then the else – block is executed.

This else-block is always in association with the if block.

Syntax:

if ( condition )
{
      statements ;
}
else
{
     statements ;
}

Here, condition is a test expression that evaluates to Boolean True or False.

Program

Consider the following code in Dart that explains the concept of if-else statement,

import 'dart:io';
void main( ) {
  print( " Enter an integer : " ) ;
  int? a = int.parse( stdin.readLineSync( ) ! ) ;
  print( " Enter an integer : " ) ;
  int? b = int.parse( stdin.readLineSync( ) ! ) ;
  if ( a > b ) {
    print( " A is greater than B " ) ;
  } else {
    print( " B is greater than A " ) ;
  }
}

Output

Enter an integer :
5
Enter an integer :
10
B is greater than A

Program

Consider the following code that accepts the integer value from the user and checks whether it is even or odd.

import 'dart:io';
void main( ) {
  print( " Enter an integer : " ) ;
  int? a = int.parse( stdin.readLineSync( ) ! ) ;
  if (a % 2 == 0) {
    print( " A is an even integer " ) ;
  } else {
    print( " A is an odd integer " ) ;
  }
}

Output

Enter an integer :
5
A is an odd integer
Enter an integer :
10
A is an even number