• About
  • Contact
  • Books

Sanjib Sinha

~ On Computer Science, PHP, Python, Ethical Hacking and more…

Sanjib Sinha

Tag Archives: control the flow of code in dart

Control the flow of your code in Dart

08 Wednesday May 2019

Posted by Sanjib Sinha in Dart

≈ Leave a comment

Tags

control the flow of code in dart, Dart, dart if and else, dart programming, programming in dart

laern-dart-the-hard-way

https://leanpub.com/learndartthehardway

Controlling the flow of your code is very important. Every programmer wants to control the logic for many reasons; one of the main reasons is the user of the software should have many options open to them.
You do not know the conditions beforehand. You can only guess and as a developer, you should open as many avenues before the user as possible. There are several techniques adopted for controlling the flow of the code. The ‘if and else’ logic is very popular.
If and Else
Let us see an example where it works to control the flow of the code:

//code 2.37
main(List<String> arguments) {
bool firstButtonTouch = true;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
if(firstButtonTouch) print("The giant starts running.");
else print("To stop the giant please touch the second button.");
if(secondButtonTouch) print("The giant stops.");
else print("You have not touched the second button.");
print("Touch any button to start the game.");
if(thirdButtonTouch) print("The giant goes to sleep.");
else print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}
//output of code 2.38
The giant starts running.
You have not touched the second button.
Touch any button to start the game.
The giant goes to sleep.
You have not touched any button.

Now you can make this small code snippet more complicated.

//code 2.39
main(List<String> arguments) {
bool firstButtonTouch = true;
var firstButtonUntouch;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
firstButtonUntouch ??= firstButtonTouch;
firstButtonUntouch = false;
if (firstButtonUntouch == false || firstButtonTouch == true) print("The giant is sleeping.");
else print("You need to wake up the giant. Touch the first button.");
if(firstButtonTouch == true && firstButtonUntouch == false) print("The giant starts running.");
print("To stop the giant please touch the second button.");
if((secondButtonTouch == true && thirdButtonTouch == true) || fourthButtonTouch == false) print("The giant stops.");
else print("You have not touched the second button.");
print("Touch any button to start the game.");
if(thirdButtonTouch) print("The giant goes to sleep.");
else print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}

According to your complexity of the code, you should arrange your ‘if and else’ logic. And your output varies.

//output of code 2.39
The giant is sleeping.
The giant starts running.
To stop the giant please touch the second button.
The giant stops.
Touch any button to start the game.
The giant goes to sleep.
You have not touched any button.

For ‘if and else’ logic always remember these golden rules.

    1. 1. When both conditions are true, the result is true.

 

    1. 2. When both conditions are false, the result is false.

 

    1. 3. When one condition is true and the other condition is false, the result is false.

 

    4. When one condition is true or one condition is false, the result is true.

In the above code, I just try to give you an idea about how you can use ‘if and else’ logic where you really need it. However, this example is too simple. It can be complex when relational operators get added and the logic may become complex.
Finally before leaving this section, I would like to show you another code snippet where the existing set of rules or principles has been changed.


//code 2.40
main(List<String> arguments) {
bool firstButtonTouch = true;
var firstButtonUntouch;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
firstButtonUntouch ??= firstButtonTouch;
firstButtonUntouch = false;
if (firstButtonUntouch == false || firstButtonTouch == true) print("The giant is sleeping.");
else if (thirdButtonTouch) print("You need to wake up the giant. Touch the first button.");
else if(firstButtonTouch == true && firstButtonUntouch == false) print("The giant starts running.");
else if (secondButtonTouch) print("To stop the giant please touch the second button.");
else if((secondButtonTouch == true && thirdButtonTouch == true) || fourthButtonTouch == false) print("The giant stops.");
else if (thirdButtonTouch) print("You have not touched the second button.");
else if (secondButtonTouch) print("Touch any button to start the game.");
else if(thirdButtonTouch) print("The giant goes to sleep.");
else if (firstButtonUntouch) print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}

And here is the output of code 2.40:

The giant is sleeping.
You have not touched any button.
You can change the pattern and see what happens.

Share this:

  • Facebook
  • WhatsApp
  • Twitter

Like this:

Like Loading...

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016

Categories

  • Android
  • Classes and Object
  • CSS
  • Dart
  • Data Journalism
  • Data-Journalism
  • Ethical hacking
    • kali linux
  • Ethical Hacking for Brginners
  • Exceptions
  • Flutter
  • General
  • git
  • HTML
  • iOS
  • JavaScript
  • Laravel 5
  • Laravel 5.2.4: Be A Laravel Expert
  • laravel 5.7
  • laravel 5.7 all model relations explained
  • Laravel 5.8
  • laravel 5: learn easy
  • life
  • metasploitable
  • Mobile Device
  • MySQL
  • NASA
  • Native iOS
  • Object Oriented Programming
  • operating systems
  • PHP
  • php 7
  • PHP 7 in 7 Days
  • PHP Developers
  • Programming Language
  • Python
    • General Syntax
    • Variables, Objects annd Values
  • Software
  • tehelka
  • wordpress

Meta

  • Register
  • Log in

Blog at WordPress.com.

Cancel
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
%d bloggers like this: