10_if2.zig 639 B

12345678910111213141516171819202122232425
  1. //
  2. // If statements are also valid expressions:
  3. //
  4. // var foo: u8 = if (a) 2 else 3;
  5. //
  6. // Note: You'll need to declare a variable type when assigning a value
  7. // from a statement like this because the compiler isn't smart enough
  8. // to infer the type for you.
  9. //
  10. // This WON'T work:
  11. //
  12. // var foo = if (a) 2 else 3; // error!
  13. //
  14. const std = @import("std");
  15. pub fn main() void {
  16. var discount = true;
  17. // Please use an if...else expression to set "price".
  18. // If discount is true, the price should be $17, otherwise $20:
  19. var price = if ???;
  20. std.debug.print("With the discount, the price is ${}.\n", .{price});
  21. }