Bird
Raised Fist0
Goprogramming~10 mins

For loop as while in Go - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a loop that runs while i is less than 5.

Go
package main

import "fmt"

func main() {
    i := 0
    for [1] {
        fmt.Println(i)
        i++
    }
}
Drag options to blanks, or click blank then click option'
Ai < 5
Bi <= 5
Ci > 5
Di == 5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'i > 5' which never runs the loop initially.
Using 'i == 5' which runs only when i equals 5.
2fill in blank
medium

Complete the code to print numbers from 1 to 3 using a for loop as while.

Go
package main

import "fmt"

func main() {
    i := 1
    for [1] {
        fmt.Println(i)
        i++
    }
}
Drag options to blanks, or click blank then click option'
Ai < 3
Bi <= 3
Ci >= 3
Di == 3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'i < 3' which stops before printing 3.
Using 'i == 3' which runs only once when i equals 3.
3fill in blank
hard

Fix the error in the loop condition to run while count is less than 4.

Go
package main

import "fmt"

func main() {
    count := 0
    for [1] {
        fmt.Println(count)
        count++
    }
}
Drag options to blanks, or click blank then click option'
Acount >= 4
Bcount > 4
Ccount < 4
Dcount == 4
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'count > 4' which never runs the loop initially.
Using 'count == 4' which runs only once when count equals 4.
4fill in blank
hard

Fill both blanks to create a loop that prints numbers from 2 to 6.

Go
package main

import "fmt"

func main() {
    num := 2
    for [1] {
        fmt.Println(num)
        num [2]
    }
}
Drag options to blanks, or click blank then click option'
Anum <= 6
Bnum < 6
C++
D++num
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'num < 6' which stops before printing 6.
Using '++num' which is invalid syntax in Go.
5fill in blank
hard

Fill all three blanks to create a loop that prints even numbers from 0 to 4.

Go
package main

import "fmt"

func main() {
    i := 0
    for [1] {
        fmt.Println(i)
        i [2] 2
    }
}
Drag options to blanks, or click blank then click option'
Ai <= 4
B+=
C-=
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' as an operator in the increment step.
Using '-=' which decreases i instead of increasing.

Practice

(1/5)
1. Which of the following is the correct way to write a while loop in Go using a for loop?
easy
A. for i < 5 { i++ }
B. while i < 5 { i++ }
C. for (i < 5) { i++ }
D. loop i < 5 { i++ }

Solution

  1. Step 1: Understand Go's while loop syntax

    Go uses for condition { } to act like a while loop.
  2. Step 2: Identify the correct syntax

    for i < 5 { i++ } uses for i < 5 { i++ }, which is valid Go syntax for a while loop.
  3. Final Answer:

    for i < 5 { i++ } -> Option A
  4. Quick Check:

    Go while loop = for condition { } [OK]
Hint: Remember: Go uses 'for' with condition as while loop [OK]
Common Mistakes:
  • Using 'while' keyword which Go does not support
  • Adding parentheses around the condition unnecessarily
  • Using 'loop' keyword which is invalid in Go
2. Which of the following Go code snippets correctly uses a for loop as a while loop to print numbers from 1 to 3?
easy
A. i := 1 for i <= 3 { fmt.Println(i) i++ }
B. i := 1 for (i <= 3) { fmt.Println(i) i++ }
C. i := 1 for i < 3 { fmt.Println(i) i++ }
D. i := 1 while i <= 3 { fmt.Println(i) i++ }

Solution

  1. Step 1: Check syntax for Go while loop

    Go uses for condition { } without parentheses around condition.
  2. Step 2: Verify loop condition and output

    i := 1 for i <= 3 { fmt.Println(i) i++ } uses i <= 3 and increments i, printing 1, 2, 3 correctly.
  3. Final Answer:

    i := 1 for i <= 3 { fmt.Println(i) i++ } -> Option A
  4. Quick Check:

    Correct Go while loop syntax = i := 1 for i <= 3 { fmt.Println(i) i++ } [OK]
Hint: No parentheses needed around condition in Go for loop [OK]
Common Mistakes:
  • Using 'while' keyword which is invalid in Go
  • Using '<' instead of '<=' causing missing last number
  • Adding parentheses around condition causing syntax error
3. What will be the output of the following Go code?
i := 0
for i < 3 {
    fmt.Print(i, " ")
    i++
}
medium
A. 1 2 3
B. 0 1 2
C. 0 1 2 3
D. 1 2

Solution

  1. Step 1: Analyze loop condition and initial value

    Variable i starts at 0 and loop runs while i < 3.
  2. Step 2: Trace loop iterations and output

    i prints 0, then increments to 1; prints 1, increments to 2; prints 2, increments to 3; loop stops.
  3. Final Answer:

    0 1 2 -> Option B
  4. Quick Check:

    Loop prints 0 to 2 with spaces [OK]
Hint: Loop runs while condition true, stops when false [OK]
Common Mistakes:
  • Assuming loop includes i=3 output
  • Starting i from 1 instead of 0
  • Forgetting to increment i causing infinite loop
4. Identify the error in the following Go code that tries to use a for loop as a while loop:
i := 0
for i < 3 {
    fmt.Println(i)
}
medium
A. Variable i should be declared inside the loop
B. Incorrect loop condition syntax
C. Using fmt.Println instead of fmt.Print
D. Missing increment of i inside the loop causing infinite loop

Solution

  1. Step 1: Check loop condition and body

    The loop condition is valid, but i is never changed inside the loop.
  2. Step 2: Understand consequences of missing increment

    Without incrementing i, the condition i < 3 remains true forever, causing an infinite loop.
  3. Final Answer:

    Missing increment of i inside the loop causing infinite loop -> Option D
  4. Quick Check:

    Change loop variable inside loop to avoid infinite loop [OK]
Hint: Always update loop variable inside for-as-while loop [OK]
Common Mistakes:
  • Forgetting to increment or change loop variable
  • Thinking fmt.Println causes error
  • Declaring variable inside loop unnecessarily
5. You want to use a for loop as a while loop in Go to read user input until the user types "exit". Which code snippet correctly implements this?
hard
A. var input string for input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) }
B. var input string for input == "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) }
C. var input string for { fmt.Print("Enter text: ") fmt.Scanln(&input) if input == "exit" { break } }
D. var input string while input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) }

Solution

  1. Step 1: Understand the loop condition for reading input

    We want to keep reading until input equals "exit".
  2. Step 2: Analyze options for correct loop behavior

    var input string for { fmt.Print("Enter text: ") fmt.Scanln(&input) if input == "exit" { break } } uses an infinite loop with a break when input is "exit", which is safe and common in Go.
  3. Step 3: Why other options fail

    var input string for input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } uses input before reading it, so loop condition is false initially (empty string != "exit" is true, but input is empty initially). var input string for input == "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } loops only if input == "exit" which is opposite. var input string while input != "exit" { fmt.Print("Enter text: ") fmt.Scanln(&input) } uses invalid 'while' keyword.
  4. Final Answer:

    Use infinite for loop with break on "exit" input -> Option C
  5. Quick Check:

    Use for { ... if condition { break } } for while loops with input [OK]
Hint: Use infinite loop with break for input-based while loops [OK]
Common Mistakes:
  • Using 'while' keyword which is invalid in Go
  • Checking condition before first input read
  • Using wrong loop condition causing no input read
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard