notexOPENOverview
Notex/Languages/Practice: Blocks, shadowing, and if statements
02.04LESSON 04 OF 19 · 1 MIN READ

Practice: Blocks, shadowing, and if statements

Always free to read

The following exercises let you try out code with blocks and if statements.

  1. Create a game where the computer picks a number and then tries to guess it. Define a variable named luckyNumber and set it to rand.IntN(10)+1. Define a second variable myGuess and also set it to rand.IntN(10)+1. Write a series of if statements that print out if myGuess is equal to, less than, or greater than luckyNumber.

  2. Rewrite the following code so err is scoped to only the if/else chain and is not accessible after it:

go err:= doSomething() if err != nil { fmt.Println("failed:", err) } else { fmt.Println("ok") }

  1. Declare a local variable named len and write code that uses both the predeclared identifier and the shadow. Explain what happens if you call len("hi") after the shadow.
Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K