# CS Variables and Expressions

To introduce students to variables and expressions, I used a flipped lesson on using formulas to calculate area, and then a pairs programming activity on divisibility.

Relevant CSTA and CCSS standards. I decided to have the kids work with the area of an ellipse because they enjoyed that connection with the area of a circle – it’s a challenging formula to write in a program, as well.

CSTA:L2:CPP:5 Implement problem solutions using a programming language, including: looping behavior, conditional statements, logic, expressions, variables, and functions.

CCSS.MATH.CONTENT.7.G.B.4
Know the formulas for the area and circumference of a circle and use them to solve problems; give an informal derivation of the relationship between the circumference and area of a circle.

CCSS.MATH.CONTENT.7.NS.A.2.C
Apply properties of operations as strategies to multiply and divide rational numbers.

To give the students vocabulary and build knowledge of syntax, I made a little video lesson for them to create shapes and calculate their area. This is their first introduction to assignment statements.

VIDEO LESSON: VARIABLES AND EXPRESSIONS

STARTER PROGRAM: Variables and Expressions

One solution to drawing and calculating the area of an ellipse.

Their individual assignment was to finish the program, drawing an ellipse and calculating its area using a formula they found in a search engine. Many students didn’t process that the height and the width of the ellipse weren’t the same as the two radii, so they calculated pi * height * width to calculate area.  I mentioned that I had remembered a circle had just about the same area as 3/4 of the area of the square enclosing it, so could it make sense that the ellipse had a greater area than a rectangle with the same width and height? The students partnered up to troubleshoot and write the expression correctly – some form of ea = 3.14159 * eh / 2 * ew / 2;  We had a good discussion about whether or not parentheses were necessary in the formula.

Next, we did a quick whole-class lesson on floor division and modulo operators. Divisibility is a really important concept in mathematics and in computer science. In JavaScript, the two operations look like this.

` a = floor (b / c);    // divides b / c.  truncates to a whole number with floor().`

`a = floor (6/3);    // a is assigned to 2`

`a = floor (10/3);   // a is assigned to 3`

`a = floor (104 / 10);  // a is assigned to 10`

`a = b % c; // divides b / c and assigns a to the remainder.`

`a = 6 % 3; // a is assigned to 0`

`a = 10 % 3; // a is assigned to 1`

`a = 104 % 10; // a is assigned to 4`

The next activity was a pairs-programming activity. I asked the students to find a partner. One would type, the other would look over their shoulder, read the prompt, make suggestions, and help troubleshoot. I set a timer so the roles would switch after 10 minutes. Students have a hard time switching roles! The one using the keyboard gets very comfortable in that position and the one not typing often is self-conscious about it. It will take some coaching for the kids to get more comfortable with pairs programming, but it will be worth it.

I gave the students a scenario. Let’s say I walk into a bank with 2778 pennies. I want to come out with the smallest number of coins and bills possible, so what will the bank give me and how did you come up with it?

We had a whole-class discussion about using division and modulo to come up with a twenty, a five, two ones, three quarters, and three leftover pennies. The students then got a starter program and started pairs programming. The starter program was about a fictional money system and making change.

PLANET ZORG STARTER

This was a really challenging task and it would need quite a few catch-and-release times. I should have used more! During the summary, two different approaches to solving the money problem came out.

Solution 1: Convert to the biggest currency unit first, then divide up the leftovers.  Some students related the problem to how we convert money. If I have 2778 pennies, I’ll look for how many hundreds, fifties, or twenties I can make first and work down. These kids had to calculate that there are 105 zinks in a zab and then work from there.

Solution 2: Convert to the second-smallest currency unit first, then make groups of the next-smallest, and so on. Each time the leftovers get assigned to the small currency units.

Great intro to variables and expressions – next we would tackle conditionals.