Hey wonderful people! How are you doing today? I hope you are healthy in the pandemic. I hope that you are living your life and learning new things every day. Today is Day 12 of #30DaysOfCode challenge and I learnt about conditionals in Go.
Whenever you are writing a program, you don't want to execute from the first line to the last line. You want to have control over your program. You want to run some lines code only when a certain situation is met and the conditionals help you to do so.
The conditionals help you to control the flow of your program. In Golang, we have many conditionals. These conditionals are very similar to other C like languages. Let's talk about them one by one.
If conditional: The
if
conditional executed the code inside its block when a condition is true.If-Else conditional: The if-else conditional executes the code inside the
if
block when a certain condition is true and it executed code inelse
block when the condition is false.If, Else-If and Else conditional: The if, else if, else conditional execute the code in
if
block when theif
condition is true. If the condition is not true, it moves to theelse if
block and executes the code in its block if the condition inelse if
is true. If none of theif
andelse if
conditions are true, it execute the code in theelse
block.Switch Case statement: The switch case statement is used to select one of many blocks of code to be executed. It executes the code of a case when the condition for that case is true.
These conditionals work almost the same in practice in Go as they work in other C-like languages. Here is a simple example of if, else if, else
in Go.
Alright! That's gonna be it for today's post.
Bye.
Connect With Me:
Youtube: ShubhamSauravYT.
Twitter: @hiShubhamSaurav
Instagram: @hiShubhamSaurav
Facebook: @hiShubhamSaurav
Top comments (0)