×

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 Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

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

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes 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 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 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 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 Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

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

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

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 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 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 Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.