This section contains the for loop find output programs (set 1) with their output and explanations.
Program 1:
package main
import "fmt"
func main() {
for num := 1; num <= 10; num++
{
fmt.Print(num," ")
}
}
Program 2:
package main
import "fmt"
func main() {
for (num := 1; num <= 10; num++) {
fmt.Print(num," ")
}
}
Program 3:
package main
import "fmt"
func main() {
for num := 2; num <= 5; num++ {
for cnt := 1; cnt <= 10; cnt++ {
fmt.Print(num*cnt, " ")
}
fmt.Println()
}
}
Program 4:
package main
import "fmt"
fun main() {
for num := 2; num <= 5; num++ {
for cnt := 1; cnt <= 10; cnt=cnt+1 {
fmt.Print(num*cnt," ")
}
fmt.Println()
}
}
Program 5:
package main
import "fmt"
func main() {
var num = 10
for num == 10 {
fmt.Println("Hello World")
}
}
Answer Program 1:
Output:
Explanation:
The above program will generate a syntax error due to the position of curly braces in the for loop. The correct program is given below,
Answer Program 2:
Output:
Explanation:
The above program will generate a syntax error due to parenthesis in the for loop. The correct program is given below,
Answer Program 3:
Output:
Explanation:
In the above program, we printed tables from 2 to 5 using the nested for loop on the console screen.
Answer Program 4:
Output:
Explanation:
The above program will generate a syntax error because we used fun instead of func to define the main() function.
Answer Program 5:
Output:
Explanation:
The above program will print "Hello World" infinite times because the num==10 condition is true.