-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Fundamentals for Self-Taught Programmers
By :
Software engineers write conditions based on certain criteria to alter the flow of code. They do this by writing code that performs different tasks based on whether that condition evaluates to true or false. One of the most common ways to create these options, or branches of code, is with the if statement with the following syntax:
Figure 7.1 – Identifying parts of an if statement
The code between the curly braces of an if statement will only run if the condition is true. If it’s false, then the code will be ignored. Let’s apply this if statement to the end of the MyBudget application, where we evaluate if we have enough for the book in the canAffordBook Boolean value. If we do, we’ll subtract the price of the book from the balance:
decimal bookPrice = 25;
bool canAffordBook = balance > bookPrice;
if (canAffordBook)
{
Console.WriteLine("You can afford to buy the...