Errors Const Final Conditional Statements in Dart (Class 15)

Errors, Const, Final and Conditional Statements in Dart

Today we are going to learn Errors, Const, Final and Conditional Statements in Dart. Let’s start

Compile time & runtime

There are following two stages when we make and run a code:-

  1. Compile time
  2. Runtime

 

Now let’s understand both of them but one by one.

 

Compile Time

Compile time is a stage where we compile a code.

 

Runtime

After compiling code when we run it is called runtime/ execution.

 

Syntax Error & Logical Error

Similarly, there are two types of errors i.e. Syntax and logical Error.

 

Syntax Error

The error we get during the compile time is called a Syntax error. For example, we type roll no as a variable name with space. In that case, we instantly get an error in an underlined form. The reason behind the error is that we cannot add space in a variable name. Similarly, if we give integer values to a String, an error comes again. These all are examples of syntax errors.

 

Logical Error

The error we get on runtime is called Logical error. Syntax error is not a big issue as we can easily fix it. On the other hand, logical error is rated more dangerous than syntax error. Suppose we made an app without error but didn’t get the desired result then it would be due to logical error.

Constant

Constants are the values that can’t be changed. It is opposite to the variable that we learned earlier. If you missed that catch up from here:

How to make variables with Dart conventions (Class 9)

 

Variables value can be changed as shown under:

int a =10;
a = 20;
a = 30;

 

However, the value of the constant cannot be changed. For example, we can’t do as under:

void main() {
  const int b = 303;
  b =500;
}

 

Const vs final

We use const and final keywords to define a constant. We use the const keyword to make constants. It is a one-time assignment and we have to give value at compile time while making const without any other option. On the other hand, the final is a bit more flexible than const as we can give it value at a later stage. It is also a one-time assignment but called runtime constant. Dart recommends making const instead of final. Almost every widget in Flutter has const. It is the responsibility of a developer to use variables or constants as per requirement.

 

Conditional Statements | if else

We put conditional statements in Dart such as if else. These are also called Control flows. The conditional statements are used to add conditions/ checks in code. These are checkpoints to get requisite results according to our set conditions. We use operators in it such as:-

 

Arithmetic operators

The following arithmetic are used to add, subtract, divide & multiply respectively:-

+

/

*

 

Logical Operators

The && and || operators are used to combine expressions. The && operator returns true only when both conditions return true.

For example:-

var a = 12

var result = (a<12 && a>5)

 

In the above example, a<12 and a>5 are two expressions combined by an && operator. Here, the first expression returns false. However, the && operator requires both expressions to return true. So, the operator skips the second expression.

The || operator returns true if one of the expressions returns true. For example −

var a = 12

var result = ( a>5 || a<12)

 

In the above code, two expressions a>5 and a<12 are combined by a || operator. Here, the first expression returns true. Since the first expression returns true, the || operator skips the subsequent expression and returns true.

 

Comparison Operators
Let’s discuss some comparison Operators which are commonly used:

== (two equals for equal to)

!= (not equal to)

> (greater than)

< (less than)

>= (greater than or equal to)

<= (less than or equal to)

Click here to read all operators on dart.dev

 

Mostly the answers are true & false. For example, this number is greater than this, true or false. We can also compare a variable to another variable and with our conditions get our desired results. Let’s see an example:-

main() {
  int a = 14, b = 18;
  if (a> b) {
    print(“a is smaller than b”);
  } else {
    print(“a is taller than b”);
  }
}

In the above example, if the value of a is greater than b then it will be true. Otherwise, it will be false which is the case so “a is taller than b” will be printed. Practice all topics i.e. Errors, Const, Final and Conditional Statements to become an expert in it. Best of luck!!

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.