Introduction to flow control
Most programming languages run linearly, where code runs from top to bottom, line by line. The following example shows the console application built in the previous chapter, which follows this order:
decimal balance = 75.45M; string balanceMessage = "The current balance is: $"; Console.WriteLine(balanceMessage + balance); balance -= 40; Console.WriteLine(balanceMessage + balance); balance -= 5; Console.WriteLine(balanceMessage + balance); decimal bookPrice = 25; bool canAffordBook = balance > bookPrice;
If you have ever kept a personal budget, you’re likely aware that expenses can be unpredictable at times. Regarding this problem with managing the ending balance, the cost of the groceries can vary, where it may be higher or lower than 40. This example currently has the price of the groceries hardcoded as 40
and the cost of tea as 5
. Let’s change it so that these values are stored in variables of the decimal data type: