Learning GO : 07 – Loops

Hey! I am Currently learning Go Lang, and I am taking some basic Notes on my Notion and though I’d also just publish them here. They are not well thought out or well written but it’s just me taking notes from time to time for my reference.

I am taking…


This content originally appeared on DEV Community and was authored by Gaurav Vala

Hey! I am Currently learning Go Lang, and I am taking some basic Notes on my Notion and though I'd also just publish them here. They are not well thought out or well written but it's just me taking notes from time to time for my reference.

I am taking the Udemy Course by Maximilian Schwarzmüller,

Notes

Loops

  • In Go lang there is only for loop , other than this there are no loops

  • So, same as conditionals, loops does not have round brackets to wrap around the condition

    for i := 0; i < 200; i++ {}
  • they are directly declared with the := syntax for the variable declaration

Infinite for Loop

  • if we define a for loop without any condition then that loop can work as an infinite loop
    for {
        fmt.Println("===============")
        fmt.Println("What do you want to do?")
    }
  • one way to get out of the infinite loop in this case would be to use return statement but with that whole program will be stopped and anything added after that will not be executed

  • So to get out of the loops we can use the break statement, which will stop the loop and start executing next line of the loop

  • We can use continue statement to break out of any particular condition and get back to the main loops, this helps when working with conditions inside a loop, so that way we can stop any particular condition and start the loops again

  • continue statement will stop the current iteration of the loop and start again that loop

Switch statement

  • It is same as the other languages, nothing specific to GO in this
switch choice {
        case 1:
            fmt.Println("Your Balance is", accountBalance)
        case 2:
            fmt.Print("Your Deposit: ")
        case 3:
            fmt.Print("Withdrawal Amount:")
        default:
            fmt.Print("Goodbye!")
        }


This content originally appeared on DEV Community and was authored by Gaurav Vala


Print Share Comment Cite Upload Translate Updates
APA

Gaurav Vala | Sciencx (2024-10-15T03:08:49+00:00) Learning GO : 07 – Loops. Retrieved from https://www.scien.cx/2024/10/15/learning-go-07-loops/

MLA
" » Learning GO : 07 – Loops." Gaurav Vala | Sciencx - Tuesday October 15, 2024, https://www.scien.cx/2024/10/15/learning-go-07-loops/
HARVARD
Gaurav Vala | Sciencx Tuesday October 15, 2024 » Learning GO : 07 – Loops., viewed ,<https://www.scien.cx/2024/10/15/learning-go-07-loops/>
VANCOUVER
Gaurav Vala | Sciencx - » Learning GO : 07 – Loops. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/15/learning-go-07-loops/
CHICAGO
" » Learning GO : 07 – Loops." Gaurav Vala | Sciencx - Accessed . https://www.scien.cx/2024/10/15/learning-go-07-loops/
IEEE
" » Learning GO : 07 – Loops." Gaurav Vala | Sciencx [Online]. Available: https://www.scien.cx/2024/10/15/learning-go-07-loops/. [Accessed: ]
rf:citation
» Learning GO : 07 – Loops | Gaurav Vala | Sciencx | https://www.scien.cx/2024/10/15/learning-go-07-loops/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.