×

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular block of statements is executed.

Syntax:

if ( condition 1 ) {
            Statements ; }
else if ( condition 2 ) {
            Statements ; }
else {
            Statement ; }

This is also known as the else…if ladder. The statements are executed sequentially and if none of the condition evaluates to true then the else block is executed by default.

Program

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

import 'dart:io';
void main( ) {
  // accepting the value of marks from the user
  print( ' Enter the marks : ' ) ;
  int? marks = int.parse( stdin.readLineSync( ) ! ) ;
// checking the marks with the user entered marks, whichever   condition evaluates to true, the print statement corresponding to that gets printed
  if ( marks > 85 ) {
    print( " A + " ) ; }
    else if ( marks > 75 ) {
    print( " A " ) ;
  } else if ( marks > 65 ) {
    print( " B + " ) ;
  } else if ( marks > 55 ) {
    print( " B " ) ;
  } else if ( marks > 45 ) {
    print( " C + " ) ;
  } else if ( marks > 35 ) {
    print( " C " ) ;
  } else {
    print( " Fail ! " ) ;
  }
}

Output :

Enter the marks :
65
B
Enter the marks :
35
Fail!

Nested if – else statement

Nest if – else statement refers to one if – else inside the another if – else.

Program

Consider the following code in Dart that explains nested if – else statement,

import 'dart:io';
void main( ) {
  // accepting the value of a from the user
  print( ' Enter the value of a : ' ) ;
  int? a = int.parse( stdin.readLineSync( ) ! ) ;
 // accepting the value of b from the user
  print( ' Enter the value of b : ' ) ;
  int? b = int.parse( stdin.readLineSync( ) ! ) ;
 // accepting the value of c from the user
  print(' Enter the value of c : ' ) ;
  int? c = int.parse( stdin.readLineSync( ) ! ) ;
 // checking if a is greater than b
  if ( a > b ) {
 // if true, then check if a is greater than c
    if ( a > c ) {
      print( " A is the greatest of all ! " ) ;
    } else {
      print( " C is the greatest of all ! ") ;
    }
 // if a is not greater than b, then check if b is greater than c
  } else if ( b > c ) {
    print( " B is the greatest of all ! " ) ;
  } else {
    print( " C is the greatest of all ! " ) ;
  }
}

Output:

Enter the value of a :
25
Enter the value of b :
45
Enter the value of c :
23
B is the greatest of all !

Related Topics

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

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...

1 minute read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.