Today I improved my Go Conference Booking App by adding conditional logic and better loop control. Instead of allowing any booking, I now validate user input using if, else if, and else statements.
Hereβs what I learned:
Boolean Expressions in Go
In Go, conditions inside if statements must evaluate to a boolean (true or false).
Example from my program:
if userTickets < remainingTickets
This expression returns:
-
true- booking is allowed -
false- booking is denied
Comparison operators I used:
< less than
== equal to
> greater than
If Statement (Main Booking Logic)
if userTickets < remainingTickets {
remainingTickets = remainingTickets - userTickets
bookings = append(bookings, fName+" "+lName)
}
What This Does:
- Checks if enough tickets are available
- Deducts tickets
- Saves the booking
- Displays confirmation
This made my app smarter and prevented overbooking.
Else If Statement
else if userTickets == remainingTickets {
// do something else
}
This condition handles the case where:
- The user books exactly the remaining tickets It allows handling a special edge case separately.
Else Statement (Invalid Booking)
else {
fmt.Printf("We only have %v tickets, so you can't book %v tickets.\n", remainingTickets, userTickets)
}
This runs when:
- The user tries to book more tickets than available
Now my program protects against invalid input.
Infinite Loop with Condition-Based Exit
The booking system runs inside:
for {
This creates an infinite loop.
But I added a stopping condition:
if remainingTickets == 0 {
fmt.Println("Our Conference is fully booked. Come back next year!")
break
}
break Statement
The break keyword:
- Immediately exits the loop
- Stops the program once tickets are sold out
Without break, the loop would continue forever.
Today I learned how to use boolean expressions and if, else if, and else statements to control the flow of a Go program. I practiced validating user input and handling different booking scenarios to prevent overbooking. I also learned how to use break to stop an infinite loop when tickets are sold out. Combining conditionals with loops and slices made my program smarter and more dynamic.
Top comments (0)