Operators And Flow Statements

Photo by Henry & Co. on Unsplash

Operators And Flow Statements

On day 6 of 100 days of flutter, I learned about operators and flow statements. This blog covers my understanding of some of what I learned.

Operators

Operators in Dart are functions all on their own. There are many of them. Dart operators are all under the num class. Dart solves expressions based on the priority of the operators involved. Surrounding specific expressions with () tells Dart to evaluate those first.

Arithmetic Operators

Arithmetic operators are the regular operators used in math habitually, plus a few others. Here are some of them +, -, *, /, ~/, %, -expr . Dart operators return a num except for the division operator, which will return a double. Use the truncation division ~/ operator to return an int value.

 const age1 = 31;
 const age2 = 19;
 print(age1 + age2); // a plus/add operator between two integers

 /// Dart will resolve this operation by subtracting the first
 /// two variables first before subtracting the value 17
 print(age1 - age2 - 17);

 print(age1 * age2); // multiplication operator

 const divided = age1 / age2; //division operator
 print(divided); // will return a double

  /// to return an int during a division operation  use the the
  /// truncating division operator ~/ instead.
  /// the ~/ operator converts a fraction by rounding it
  /// towards 0
  const intDivided = age1 ~/ age2;
  print(intDivided);

Unary Prefix And Postfix Operators

Unary operators involve the increment or decrement operators, i.e., they increase or decrease a value by a number. And they are termed prefixes or postfixes if they come before or after the variable they operate on.

 // Unary Prefix Operators
 var age = 21;
 int age2 = --age; // subtracts 1 from 21 and assign 20 age & age2

 print(++age); // mutate the value of age back to 21
 print(age2);
 print(--age); // subtracts 1 from age

 /// this unary prefix reverses the sign of the expression
 /// or value to a negative or positive
  print(-age); // age will be -21

 // Unary postfix operators
  var age = 50;
  print(age++); // will print value of age(50) before adding 1.
  print(age); // prints the current value of age 51

Binary Infix Operators

Binary infix operators sit and operate between two values

 /// The value of age remains the same no matter the operation 
/// beign performed
  const age = 20;
  print(age + 20);
  print(age - 2);
  print(age * 5);
  print(age / 4);
  print(age ~/ 4);

///remainder operator returns the remaining value after division |      ///check how many 3s are in 10 and return the remainder
  print(age % 3); 
  print(age); // age remains 20

/// equality or rational operators compare values with each other

  print(age == 2); // checks if age is equal to 2 and  returns true/false

/// not equal operator switches the actual result to it's opposite
  print(age != 20); // return false instead of true

  print(age > 10); // true
  print(age < 10); // false
  print(age >= 10); // true
  print(age <= 10); // false

Control Statements

A Dart app starts its execution from the main function, and it's where the flow of the app starts and advances line by line.

If Else Statements

If and else statements are the basic constructs for allowing the flow of the app to be changed based on some conditions. An if statement usually looks for something that evaluates to a boolean ( true or false). if (2 == 2) An else statement takes the application flow to a block of code if the if statement preceding it doesn't evaluate to true. Else statements are usually optional.

const yourName = 'Sam';
const myName = 'Mak';

if (yourName == myName) {
   print('We have the same name'); // moves app flow to this line if     yourName = myName
 } else {
    print('Hello Sam!'); // if not it prints this line
 }

For Loop Statements

A "for loop" is a way to repeat a block of code multiple times. It's like having a set of instructions that you want to execute over and over again, but with different inputs or conditions.

  const names = ['Yeee', 'Food', 'Bar', 'Zar'];

  /// for loop creates var i
  /// set condition that stops the for loop(increase value of i by 1(i++) everytime loop is exec)
  /// stop loop if i > name length
  /// Dart  jumps out of for loop since i is not < name.length
  for (var i = 0; i < names.length; i++) {
    print(names[i]);
  }

While Loop Statement

While Loop is a type of indefinite(Loops in which several iterations are not constant) loop which checks the test expression or is based on a boolean condition(True or False). That means if the test expression is true it will execute the statement block and it will evaluate again and again as long as the test expression is true. When the test expression is false it will exit from the loop.

const names = ['John', 'Sam', 'Miles', 'Ken'];
var counter = 0;

while (counter < names.length) {
    // creates an endless loop
    print(names[counter++]); // prints name and increases counter till expression is false
  }

Do-While Loop Statements

Do-While like While Loop is a type of Indefinite Loop(Number of iterations are not constant).

In Do-While Loop is similar to a While Loop. The difference is Do-While loop executes the statement block once even if the test expression or condition is false, i.e. it executes the statement block before checking the condition.

do {
    print(names[counter++]);
  } while (counter < names.length);

Switch Statements

Switch Case Statement is used when we have to evaluate a test condition against multiple cases. The statement block of the particular case which is evaluated as true is executed. If all the case statements are evaluated as False then the statement block of the default case will be executed.

var number = 3;
    switch(number){ // test condition
    // cases
    case 1:
    {
    print("the number is 1.");
    }
    break;
    case 2:
    {
    print("the number is 2.");
    }
    break;
    // executes case 3 since case value = 3
    case 3:
    {
    print("the number is 3.");
    }
    default :
    {
    print("The number is other than 3");
    }