![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |
![]() | Section 4: Control Statements |
When a programmer is crafting a program, it is good practice to break the program down into pieces that can be thought of independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other. While one piece has the control, the other pieces are inactive. This is known as the flow of control in a program. If our program had three parts, called Start, Middle, and End, the flow of control could look like:
if (my_money > cost_of_CD) then buy_CD else get_a_job end if;
Note that the pseudocode statement end if means "end the previous if statement." This is to make it clear what statements are inside the if statement and what statements are outside of the if statement.
Depending on a certain condition a certain series of events will be executed. Another type of branching statement is called a switch statement. A switch statement is just a shorter way of writing a lot of if statements. Switch statements will be explained in more detail in the next subsection.
if (my_money > cost_of_CD) then buy_CD else if (see_a_friend) then borrow_money else get_a_job end if; end if;Now there is one control statement that is inside of another control statement. This is known as nesting.
if (my_money > cost_of_house) then buy_house end if;But this means that Julien would only check once if he had enough money to buy the house. What we want to describe is the fact that Julien needs to keep waiting until he has enough money to buy the house.
while (my_money < cost_of_house) work_more end while; buy_house;This is a loop statement. Another loop statement is the for command. Let's say Julien wanted to add up how much money he would make over the next year. Let's say Julien is paid $500 twice each month. The pseudocode to figure this out could be:
int total=0; for x = 1 to 24 total = total + 500 next x; output total;A for statement execute a specified number of times. In this instance it executes 24 times (12 months * 2 pay periods per month). In this example, it would actually be easier to write this code as:
total = 24 * 500; output total;But, what if Julien earns interest on any money that he saves? Now a for statement will be a handy tool. Let's decide that Julien spends $400 a month on rent, $75 a month on food, and $100 a month on other expenses. Let's also assume that Julien earns 2% per month on any money that he saves. Now our pseudocode could look like:
int monthly_expenses= 400 + 75 + 100; int monthly_income = 1000; float interest_rate = .02 // compute the amount Julien will have saved after one year int total = 0; int interest_earned =0; for x = 1 to 12 interest_earned = total * interest_rate; total = total + interest_earned + monthly_income - monthly_expenses next x; // display the value output total;Control statements allow a programmer to craft a program so that certain parts of code execute multiple times, or not at all based on the current state of the program. Control statements are the most basic form of logical control within a program.
![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |