If Statements
Practice decision-making with if statements and conditional logic.Age Seasons
Age Seasons
Objective: Use if-else statements to categorize agesPrint different messages based on age ranges.Expected Output:
- Age > 60: “Getting older”
- Age > 30: “Getting wiser”
- Age > 20: “Adulthood”
- Age > 10: “Young blood”
- Otherwise: “Booting up”
- If-else chains
- Comparison operators
- Order of conditions matters
Simplify It
Simplify It
Objective: Refactor complex if statementsSimplify nested or complex if statements for better readability.Key Concepts:
- Code simplification
- Logical operators
- Guard clauses
- Combine conditions with
&&or|| - Use early returns
- Eliminate unnecessary nesting
Arg Count
Arg Count
Objective: Validate command-line argumentsCheck if the correct number of arguments are provided.Key Concepts:
- Length checking
- Input validation
- Error messages
Vowel or Consonant
Vowel or Consonant
Objective: Classify charactersDetermine if a given character is a vowel or consonant.Key Concepts:
- Character comparison
- Multiple conditions
- String indexing
- Get first character from input
- Compare against vowels (a, e, i, o, u)
- Use
||for multiple vowel checks
Error Handling with If
Learn to handle errors gracefully using if statements.Movie Ratings
Movie Ratings
Objective: Parse and validate ratingsConvert string ratings to numbers and validate them.Key Concepts:
strconvpackage- Error checking
- Input validation
Odd or Even
Odd or Even
Objective: Check number parity with error handlingDetermine if a number is odd or even, handling invalid input.Key Concepts:
- Modulo operator
% - Error handling pattern
- Type conversion
Leap Year
Leap Year
Objective: Determine if a year is a leap yearImplement leap year logic with proper input validation.Rules:
- Divisible by 4: leap year
- EXCEPT divisible by 100: not a leap year
- EXCEPT divisible by 400: leap year
Simplify Leap Year
Simplify Leap Year
Objective: Refactor leap year logicMake the leap year checker more concise and readable.Key Concepts:
- Boolean expressions
- Operator precedence
- Code clarity
Days in a Month
Days in a Month
Objective: Calculate days for any monthReturn the number of days in a given month, considering leap years.Key Concepts:
- Multiple conditions
- Combining if statements
- Edge cases
- Check for 30-day months
- Check for 31-day months
- Handle February with leap year logic
Switch Statements
Use switch statements for cleaner multi-way branching.Richter Scale
Richter Scale
Objective: Classify earthquake magnitudesDescribe earthquake effects based on Richter scale values.Key Concepts:
- Switch with expressions
- Case ranges
- Fallthrough behavior
Richter Scale #2
Richter Scale #2
Objective: Enhanced Richter scale classifierAdd more granular classifications and descriptions.Approach:
Expand the previous exercise with more detailed magnitude ranges and descriptions.
Convert
Convert
Objective: Unit conversion toolCreate a unit converter using switch statements.Key Concepts:
- Switch on strings
- Case matching
- Conversion formulas
String Manipulator
String Manipulator
Objective: String operations based on commandsPerform different string operations based on input commands.Operations:
- Uppercase
- Lowercase
- Reverse
- Length
Days in Month (Switch)
Days in Month (Switch)
Objective: Reimplement days-in-month using switchUse a switch statement instead of if-else for cleaner code.Approach:
Loops
Master iteration with Go’s versatile for loop.Basic Loops
Sum the Numbers
Sum the Numbers
Objective: Calculate sum using a basic loopSum numbers from 1 to 10 using a for loop.Approach:
Sum the Numbers (Verbose)
Sum the Numbers (Verbose)
Objective: Add detailed output to loopPrint each step of the summation process.Approach:
Print the current number and running total in each iteration.
Sum Up to N
Sum Up to N
Objective: Sum with user-provided limitSum numbers from 1 to N where N comes from input.Approach:
- Parse N from command-line argument
- Loop from 1 to N
- Calculate and print sum
Only Evens
Only Evens
Objective: Process only even numbersPrint or sum only even numbers in a range.Approach:
Loop Control
Break Up
Break Up
Objective: Exit loops early with breakUse
break to stop a loop when a condition is met.Key Concepts:- Break statement
- Loop termination
- Conditional exit
Infinite Kill
Infinite Kill
Objective: Control infinite loopsCreate and properly exit an infinite loop.Approach:
Multiplication Table
Dynamic Multiplication Table
Dynamic Multiplication Table
Objective: Generate multiplication tablesCreate a multiplication table for any given number.Approach:
Math Tables
Math Tables
Objective: Generate multiple operation tablesCreate tables for addition, subtraction, multiplication, and division.Approach:
Use nested loops or multiple loops for different operations.
Advanced Loop Exercises
Lucky Number Game
Lucky Number Game
Objective: Build a number guessing gameCreate a game where the player guesses a random number.Key Concepts:
- Random numbers
- User input in loops
- Win conditions
- First Turn Winner: Check if player wins on first try
- Random Messages: Show different messages for guesses
- Double Guesses: Track guess attempts
- Verbose Mode: Add detailed output
- Enough Picks: Limit number of guesses
- Dynamic Difficulty: Adjust range based on performance
Word Finder
Word Finder
Objective: Search for words in textFind and count occurrences of words in a string.Key Concepts:
- String iteration
- Pattern matching
- Substring comparison
Crunch the Primes
Crunch the Primes
Objective: Find prime numbersGenerate prime numbers using nested loops.Key Concepts:
- Prime number algorithm
- Nested loops
- Optimization techniques
- Loop through numbers
- For each number, check divisibility
- Print if prime
Loop Best Practices
Performance Tips:
- Use
breakto exit early when possible - Avoid unnecessary iterations
- Consider loop scope for variable declarations
- Use range loops for slices when appropriate (covered in Data Structures)
Next Steps
Data Structures
Work with arrays, slices, maps, and structs
Functions
Learn to write reusable functions