Day Five of the #60Days of Flutter Challenge
Learning about switch operators, while loops, for loops and for…in loops
Welcome to Day Five of my sixty-day journey to learn Flutter. Thanks to the incredible work of Mike Dane, I’ve come a long way with Dart in a short time.
Today we learned about switch statements, while loops, for loops, and for…in loops.
A switch statement works in about the same way as an IF statement. It is used to see if one thing is equal to a bunch of other things. However, there are specific circumstances where one works better than the other.
Piggy-backing off of the calculator program we built in a previous lesson, we toss aside our IF/ELSE IF/ELSE statements for a switch statement.
In the below example, we are essentially saying, “if the variable op is (+), print num1 + num2.” When that statement is true, we break out of the loop — ending the program. When it isn’t, we carry on to the next statement (-).
You can use an IF statement, but there are benefits to using a SWITCH statement whenever it makes the most sense. One gigantic benefit is clarity. The final code is less repetitive — and, therefore, easier to read.
It is important to reiterate, though, that an IF statement will do the same thing — it just might not do it as cleanly.
Loops are used to execute the same code multiple times as long as a particular condition remains true. There are three types of loops we learned about today, while loops, for loops, and for…in loops.
Loops need data to work.
A while loop is the most basic of the three. In a while loop, you execute an instruction every time a specific condition is true. Every time the loop is completed, the index increments, and the loop starts over. This continues until the condition is false. At that point, the program ends.
The below example is a very basic guessing game that uses a while loop. A user guesses names until they come up with the answer. Each time you are incorrect, it asks you to enter a guess. Once you answer “Michael Scott,” the program prints, You won!
A for loop works in a similar fashion.
Consider the List <String> friends = [“Joe”,”Bob”,”Mary”];
There are three parts to a traditional for loop, the initialization of the index (i = 0;), a particular condition (i<friends.length;), and incrementing index (i++).
In the above example, a loop will move through a list of friends (Joe, Bob, and Mary) and print each of their names in all-caps.
The for…in loop seems to be the cleanest of the three by far. This loop says, “for each friend’s name in the friends list, print their name.”
Here is an example of a for loop at work.
In a previous lesson, we needed to write the function drawTriangle(); five different times to draw five triangles. Per our unending quest to not repeat ourselves (remember D.R.Y.?), we needed to find a better way.
First, we created a drawTriangle() function. Then we added a parameter requiring the number of triangles to print. The for loop continues until the condition (in this case, i < numTriangles) is false.
We are now able to do what we could only dream a day or two ago.
To make it work, all we have to do is enter the number of triangles we’d like to draw when we call the function.
I know what you are thinking. If i < numTriangles, how are we able to draw five? Don’t forget, we start our index at 0.
That’s it for Day Five. Today’s lesson was a bit shorter than the others but covered some important information. Switch statements and loops are a big deal. Both use data to define logic and therefore help drive the flow of your Dart code.
In case you missed it, here is the post for day four: