Category Archives: Dart Tutorial

Dart Tutorial

Dart Break statement

Dart Break Statement

The break statement inside any loop gives you way to break or terminate the execution of loop containing it, and transfers the execution to the next statement following the loop. It is almost always used with if..else construct.

Dart Break Statement Flow Diagram

dart_break_statement_example

Syntax:-

Example:-

In this above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10. Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1). Next, we have an if statement that checks the variable count is equal to 5, if it return TRUE causes loop to break or terminate. Within the loop there is a cout statement that will execute with each iteration of the while loop until the loop breaks. Then, there is a final cout statement outside of the while loop.

When we run this code, our output will be as follows –

Output:-

dart_break_statement_example

Dart do while Loop

Dart do while Loop

The do…while loop executes block of statements inside loop body first and then test the condition for next iteration and executes next only if condition is true. The do…while loop is much similar to Dart while loop with one major difference, in do…while loop block of statements inside loop body executes at least once.

Dart do while Loop Flow Diagram

dart-do-while-loop-flowchart-diagram

Syntax:-

Here, loop executes block of statements inside loop body first and then test the condition provided along with while keyword for next iteration and executes next only if condition is true. Condition is a Boolean expression that results in either True or False, if it results in True then statements inside loop body are executed and condition is evaluated again. This process is repeated until the condition is evaluated to False. If the condition results in False then loop is terminated and control is transferred to next statement.

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have loop body inside the pair of curly brackets {} after do keyword, statements inside loop body are executed first then control is passed to the condition ctr <= maxCtr provided along with while keyword for next iteration. If the test condition returns True, then control is passed again to loop body for the next iteration. If the condition results in False then loop is terminated and control is transferred to next statement. Inside the loop body we have incremented the ctr value by one for each if the iteration. When we run the above Dart program, we see the following output –

Output:-

dart_do_while_loop_example

Dart while Loop

Dart While Loop

The while loop will execute a block of statement as long as a test expression is true. While loop is useful when the number of iterations can not be predicted beforehand. The while loop evaluates test expression at beginning of each pass.

Dart While Loop Flow Diagram

dart-while-loop-flowchart-diagram

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside loop body are executed and condition is evaluated again. This process is repeated until the condition is evaluated to False. If the condition results in False then execution is skipped from loop body and while loop is terminated.

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have while loop, that checks the condition ctr <= maxCtr for each of the iteration. If the test condition returns True, the statements inside loop body are executed and if test condition returns False then the while loop is terminated. Inside the loop body we have incremented the ctr value by one for each if the iteration. When we run the above Dart program, we see the following output –

Output:-

dart_while_loop_example

Dart for…in Loop

Dart for…in Loop

The for loop is used when we want to execute block of code known times. In Dart, the for in loop takes an expression as iterator, and iterate through the elements one at a time in sequence. The value of the element is bound to var, which is valid and available for the loop body. Once the loop statements are executed current iteration, the next element is fetched from the iterator, and we loop another time. When there is no more elements in iterator, the for loop is ended.

Dart For In Loop Flow Diagram

dart-for-in-loop-flowchart-dagram

Syntax:-

Example:-

Output:-

dart_for_in_loop_example

 

Dart for Loop

Dart for Loop

The for loop is used when we want to execute block of code known times. In Dart, basic for loop is similar as it is in C, C++ and Java. The for loop takes a variable as iterator and assign it with an initial value, and iterate through the loop body as long as the test condition is true. Once the loop statements are executed for current iteration, the iterator is updated with new value and if the test condition is still valid, we loop another time. When test condition return a false value, the for loop is ended.

Syntax :-

Above is the general syntax of a for loop. We just initialize iterator, check condition and then increment or decrement iterator value. The for loop has three components separated by semicolons.

initialization :- In this section is used to declare and initialize the loop iterator. This is the first statement to be executed and executed only once at the beginning.

condition :- Next, the test condition is evaluated before every iteration, and if it returns a true value then the loop body is executed for current iteration. If it returns a false value then loop is terminated and control jumps to the next statement just after the for loop.

incr/decr :- Once, the loop body is executed for current iteration then the incr/decr statement is executed to update the iterator value and if the test condition is evaluated again. If it returns a true value then the loop body is executed another time. If test condition returns a false value then loop is terminated and control jumps to the next statement just after the for loop.

Dart For Loop Flow Diagram

dart-for-loop-flow-chart-diagram

Example:-

Here, we have initialized ctr with initial value of 1, in the next we checks the condition ctr <= 5 for each of the iteration. If the test condition returns True, the statements inside loop body are executed and if test condition returns False then the for loop is terminated. In incr/decr section we have incremented the ctr value by one for each if the iteration. When we run the above Dart program, we see the following output –

Output:-

dart_for_loop_example

Dart Loops

Dart Loops

Loop statements are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. Loop statement are very useful to iterate over collection/list of items or to perform a task for multiple times. In Dart, we have following loop statements available-

Dart for Loop

The for loop is used when we want to execute block of code known times. In Dart, basic for loop is similar as it is in C. The for loop takes a variable as iterator and assign it with an initial value, and iterate through the loop body as long as the test condition is true.

Syntax :-

Example:-

Output:-

dart_for_loop_example

Dart for..in Loop

The for in loop takes an expression or object as iterator, and iterate through the elements one at a time in sequence. The value of the element is bound to var, which is valid and available for the loop body. Once the loop statements are executed current iteration, the next element is fetched from the iterator, and we loop another time. When there is no more elements in iterator, the for loop is ended.

Syntax:-

Example:-

Output:-

dart_for_in_loop_example

Dart While Loop

The while loop will execute a block of statement as long as a test expression is true.

Syntax:-

Example:-

Output:-

dart_while_loop_example

Dart do…while Loop

The do…while statement executes loop statements and then test the condition for next iteration and executes next only if condition is true.

Syntax:-

Example:-

Output:-

dart_do_while_loop_example

Dart Switch Case Statement

Dart Switch Case Statement

In Dart, switch case statement is simplified form of the Nested if else statement , it helps to avoid long chain of if..else if..else statements. A switch case statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Dart Switch Case Flow Diagram

dart-switch-case-statement-flowchart

Syntax:-

Example:-

In the above program we have an integer constant dayOfWeek with initial value of 5, and we pass this variable as expression to following switch statement, value of dayOfWeek is tested with multiple cases to identify the block of code to be executed.

When we run the above Dart program, will see the following output –

Output:-

dart_switch_case_statement_example

Dart if else if Statement

Dart if else if Statement

In Dart, if..else..if statement allows us add alternative set of test conditions in if..else statement using else-if and single else statements for if condition. In such way if..else..if statement is used to select one among several blocks of code to be executed.

Dart if..else..if Statement Flow Diagram

dart-if-else-if

dart-if-else-if

Syntax:-

Example:-

Output:-

dart_if_else_if_ladder_statement_example

Dart if else Statement

Dart if else Statement

In Dart, when we want to execute a block of code when if condition is true and another block of code when if condition is false, In such a case we use if…else statement.

Dart If…else Statement Flow Diagram

dart-if-else-flowchart

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then statements inside else body are executed.

Example:-

Output:-

dart_if_else_statement_example

Dart if Statement

Dart if Statement

If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be either true or false.

Dart If Statement Flow Diagram

dart-if-statement

dart-if-statement

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then execution is skipped from if body.

Example:-

Output:-

dart_if_statement_example

Dart Decision Making Statements

Dart decision making statements allow you to make a decision, based upon the result of a test condition. The decision making statements are also known as Conditional Statements.

What Is Decision Making Statements

So far, we have learned that all statements in a Dart program are executed sequentially (top to bottom) in the order in which they are written. This occurs until there is no jump statements or loop statements. But sometimes we may need to change the order of program execution depending on some specific conditions. Here comes the need of decision making structures that enable computer to decide set of statements to be executed depending upon some conditional choices. These statements are called as the decision making statements or Conditional Statements.

Dart Decision Making Statements

In Dart, we have rich set of Decision Making Statement that enable computer to decide which block of code to be execute based on some conditional choices. Decision making statement statements is also referred to as selection statements. Decision making statement evaluates single or multiple test expressions which results in “TRUE” or “FALSE”. The outcome of the test expression/condition helps to determine which block of statement(s) to executed if the condition is “TRUE” or “FALSE” otherwise. It is to be noted that Dart language assumes any non-zero or non-null value as true and if zero or null, treated as false.

Types of Decision Making Statements

In Dart, we have following types of decision making statements –

dart-decision-making-statements-types-chart

Dart If Statement

If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be either true or false.

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then execution is skipped from if body.

Dart If Else Statement

In Dart, when we want to execute a block of code when if condition is true and another block of code when if condition is false, In such a case we use if…else statement.

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then statements inside else body are executed.

Dart If Else If Statement

When we want to add multiple condition checks in single if else statement then by using if else-if else statement we can easily add multiple conditions. In Java, if..else..if statement allows us add alternative set of test conditions in if..else statement using else-if and single else statements for if condition. In such way if..else..if statement is used to select one among several blocks of code to be executed.

Syntax:-

Dart Switch Case Statement

In Dart, switch case statement is simplified form of the Nested if else statement , it helps to avoid long chain of if..else if..else statements. A switch case statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Syntax:-