Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If Statements | Coding Foundations
Matlab From Beginner to Professional
course content

Course Content

Matlab From Beginner to Professional

Matlab From Beginner to Professional

1. Basic Syntax And Coding With A Text Editor
2. Coding Foundations
3. Learning Through Applications
4. Visualizations
5. Recursion + Matrix Multiplication

bookIf Statements

What happens if we want to program different tasks depending on certain conditions? Do we write separate programs for each case? No! We'll use if statements!

If statements allow portions of code to be executed or skipped based on whether or not certain Boolean statements are true or false. This sort of gatekeeping can also be made more intricate through the incorporation of elseif and else statements. And when combined with for loops (previous chapter), we can really start to analyze data in complex and efficient ways.

Here we'll dip our toes in by learning the general behavior and logical errors that can arise. The syntax isn't super complicated, so you can start off with the set of snippets until you have it down, and in the next section we'll start using if statements to analyze data in real world applications.

Task

Write a program that:

  • Imports all the data from the Excel file "Chapter 4 Task Data Set";
  • Uses an if statement nested in a for loop to identify the indices of all the rows in the data set that represent farms owned by Mary;
  • Reduces the numerical data to only the farms owned by Mary;
  • Calculates the total number of each type of animal that Mary owns;
  • Calculates the average number of each type of animal on Mary's farms;
  • Outputs these two sets of calculations to a well-defined location in the Excel file, along with headings to label the results.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt