Tags

, , , ,

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.

Advertisements