Skip to content

Commit

Permalink
Go Error
Browse files Browse the repository at this point in the history
  • Loading branch information
teguhteja committed Dec 1, 2024
1 parent 1a7d5a5 commit b1b47b1
Show file tree
Hide file tree
Showing 3 changed files with 762 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,133 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Breaking the Inner Loop in Go"
"## Breaking the Inner Loop in Go\n",
"\n",
"Well done on your journey through loop control! Your task is to enrich your understanding by completing a conditional statement and action within a nested loop in Go.\n",
"\n",
"package main\n",
"\n",
"import \"fmt\"\n",
"\n",
"func main() {\n",
" for i := 1; i <= 4; i++ {\n",
" for j := 1; j <= 4; j++ {\n",
" // TODO: Add a condition and control statement to skip the rest of the loop when 'j' is equal to 'i'.\n",
" fmt.Print(i, \"\", j, \" \")\n",
" }\n",
" fmt.Println() // Move to the next line\n",
" }\n",
"}\n",
"ChatGPT said:\n",
"To enhance your nested loop with a conditional stateme\n",
"\n",
"To enhance your nested loop with a conditional statement that skips the rest of the inner loop when `j` is equal to `i`, you can use the `continue` statement in Go. Here's the updated code:\n",
"\n",
"```go\n",
"package main\n",
"\n",
"import \"fmt\"\n",
"\n",
"func main() {\n",
" for i := 1; i <= 4; i++ {\n",
" for j := 1; j <= 4; j++ {\n",
" if j == i {\n",
" continue // Skip the rest of this iteration of the inner loop\n",
" }\n",
" fmt.Print(i, \"\", j, \" \")\n",
" }\n",
" fmt.Println() // Move to the next line\n",
" }\n",
"}\n",
"```\n",
"\n",
"### Explanation:\n",
"1. **Condition**: The `if j == i` condition checks if the current values of `j` and `i` are equal.\n",
"2. **Control Statement**: The `continue` statement tells the program to skip the rest of the inner loop's current iteration and proceed to the next iteration.\n",
"3. **Output Behavior**: This avoids printing pairs where `j` is equal to `i`.\n",
"\n",
"### Example Output:\n",
"```\n",
"1 2 1 3 1 4 \n",
"2 1 2 3 2 4 \n",
"3 1 3 2 3 4 \n",
"4 1 4 2 4 3 \n",
"``` \n",
"\n",
"Here, you can observe that combinations where `i` equals `j` (e.g., `1 1`, `2 2`, etc.) are excluded from the output."
]
},
{
"cell_type": "markdown",
"id": "16196f0f",
"metadata": {},
"source": [
"Great, Galactic Pioner! Let's get to your last challenge. Write a program in Go simulating the operation of an amusement park. Ensure to skip a particular ride that's currently unavailable due to maintenance, and close the park after reaching a predetermined ride number.\n",
"\n",
"package main\n",
"\n",
"// TODO: Implement your main function below\n",
"\n",
"func main() {\n",
" // TODO: Create a loop for the amusement park rides from 1 to 10\n",
" // TODO: Skip ride #6, it's under maintenance right now\n",
" // TODO: End your day early when reaching ride #9\n",
" // TODO: Print out the ride numbers that visitors enjoy on the way\n",
"}\n",
"\n",
"Here's the completed Go program simulating an amusement park as per your requirements:\n",
"\n",
"```go\n",
"package main\n",
"\n",
"import \"fmt\"\n",
"\n",
"func main() {\n",
" // Loop through the rides from 1 to 10\n",
" for ride := 1; ride <= 10; ride++ {\n",
" if ride == 6 {\n",
" fmt.Println(\"Ride #6 is under maintenance. Skipping...\")\n",
" continue // Skip ride #6\n",
" }\n",
" \n",
" if ride == 9 {\n",
" fmt.Println(\"Ride #9 reached. Park is closing early for the day.\")\n",
" break // Close the park after reaching ride #9\n",
" }\n",
" \n",
" // Print the rides that visitors can enjoy\n",
" fmt.Printf(\"Visitors are enjoying ride #%d!\\n\", ride)\n",
" }\n",
"}\n",
"```\n",
"\n",
"### Explanation:\n",
"1. **Loop through rides (1 to 10)**:\n",
" - The loop iterates over ride numbers from 1 to 10 using the `for` loop.\n",
"\n",
"2. **Skip ride #6**:\n",
" - The `if ride == 6` condition checks if the current ride is #6, and the `continue` statement skips this ride.\n",
"\n",
"3. **Close the park after ride #9**:\n",
" - The `if ride == 9` condition checks if the ride is #9, and the `break` statement ends the loop early to simulate the park closing.\n",
"\n",
"4. **Print ride activity**:\n",
" - For all other rides, a message is printed to show that visitors are enjoying them.\n",
"\n",
"### Example Output:\n",
"```\n",
"Visitors are enjoying ride #1!\n",
"Visitors are enjoying ride #2!\n",
"Visitors are enjoying ride #3!\n",
"Visitors are enjoying ride #4!\n",
"Visitors are enjoying ride #5!\n",
"Ride #6 is under maintenance. Skipping...\n",
"Visitors are enjoying ride #7!\n",
"Visitors are enjoying ride #8!\n",
"Ride #9 reached. Park is closing early for the day.\n",
"``` \n",
"\n",
"This program ensures smooth operation while handling maintenance and early closure scenarios gracefully!"
]
}
],
Expand Down
Loading

0 comments on commit b1b47b1

Please sign in to comment.