0
0
Goprogramming~15 mins

Loop execution flow in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Loop Execution Flow in Go
๐Ÿ“– Scenario: You are helping a small shop owner who wants to count how many items they have in stock. They have a list of item quantities and want to add them up.
๐ŸŽฏ Goal: Build a Go program that uses a for loop to add up all the item quantities and print the total.
๐Ÿ“‹ What You'll Learn
Create a slice named items with the exact quantities: 3, 7, 2, 9, 4
Create an integer variable named total and set it to 0
Use a for loop with the variable quantity to go through each number in items
Add each quantity to total inside the loop
Print the final value of total
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Counting totals is common in shops, inventories, and anywhere you track quantities.
๐Ÿ’ผ Career
Understanding loop execution flow is essential for processing lists of data in software development.
Progress0 / 4 steps
1
Create the item quantities slice
Create a slice called items with these exact integer values: 3, 7, 2, 9, 4
Go
Need a hint?

Use items := []int{3, 7, 2, 9, 4} to create the slice.

2
Create the total variable
Create an integer variable called total and set it to 0 inside the main function, below the items slice
Go
Need a hint?

Use total := 0 to create the variable.

3
Add a for loop to sum the items
Use a for loop with the variable quantity to iterate over items and add each quantity to total
Go
Need a hint?

Use for _, quantity := range items and inside the loop add quantity to total.

4
Print the total
Print the value of total using fmt.Println(total). Remember to import the fmt package at the top.
Go
Need a hint?

Use fmt.Println(total) to print the total sum.