×

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

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute 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 Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

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

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

2 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

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

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.