×

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

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

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

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 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 Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

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

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

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

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

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

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.