This is the third video of this section flow control in the previous video we started with the program
to display hello world in this video.
We will use the if else statement the switch statement and the full range statement flow control is
referred to as the ability to decide which portion of code or how many times you execute some code on
a condition in go.
It is implemented using familiar imperative clauses like if else switch and or the syntax is easy to
grasp.
Let us review major flow control statements in go.
So we start with the first one that is the if else statement.
Go language like most programming languages has if else conditional statement for flow control the syntax
is similar to other languages but you don't need to encapsulate the condition between parentheses.
Here's how we use it as you can see the main function starts with the variable value followed by the
if statement with the condition and the print statement when the condition is true and the print within
else will be printed when the condition is false.
Let's run this and see the output so you can see the print within elses given as output because 10 is
not equal to 20.
Now the else if condition works in a similar fashion you don't need parentheses either and they are
declared as programmers would expect.
Let me add the lines of code for this so it contains if else F and else conditions let's execute this
program so the output is 10 is equal to 10.
Cool we got this output based on the condition.
A equal to be 10 equal to 10 and true equal to False.
Else if eleven.
Equal to eleven and go equal to go.
We print the second statement or else we print the last statement that is in case no condition is satisfied.
Print this go does not have ternary conditions like condition with question mark sign and true colon
pulse.
Next let's touch the switch statement.
So the switch statement is also similar to most imperative languages.
You take a variable and check possible values for it.
Let's take an example to understand this.
So the switch condition here is no should be three.
We have added three conditions so we can be sure that we should get the third statement as the output.
Let's run and check the output.
The third case is satisfied and we get the output as three.
Moving on to the last one.
That is the full range statement.
The for loop is also similar than in common programming languages but you don't use parentheses either.
We use the logic for IE is equal to zero ie less than equal to Xen ie plus plus
and we print all the values of IE for this condition as you have probably imagined.
If you have a computer science background we infer an entire variable defined as 0 and execute the code
between the brackets while the condition ie less than or equal to zero is satisfied.
Finally for each execution we add one to the value of ie.
This code prints the numbers from zero to 10 you have a special syntax to iterate over arrays or slices
which is range which I add here.
So for index value range my array we print index is Ampersand D and value is am present day with the
values of index and value for D.
First the F empty format is a very common go package that we will use extensively to give shape to the
message that we will print in the console.
Regarding 4 you can use the range keyword to retrieve every item in a collection like my array and assign
them to the value temporal variable.
It will also give you an index variable to know the position of the value you're retrieving.
It's equivalent to write this code.
This is the basic syntax for loop.
The Len method is used to know the length of a collection.
If you execute this code you'll see that the result is the same in this video.
We've learned about different statement to use of flow control in go in the next video.
We will dive into the functions with go.