If / Else Conditional Logic Statements for Coding

Lesson Topic:

If/Else Conditional Logic Statements

Lesson Description:

Students will practice using if/else statements or conditional logic statements. The lesson starts with students doing a pairing activity to review if/else statements. (Approximately 10-15 minutes). Once they find the partner with the other pair with the "output" to the pseudo code the other partner has, they will stay with that person as their daily coding buddy. Transition to a brief discussion about Boolean Operators and the coding symbols that go with them. Students will then do another unplugged activity (attached) where they will work with their partners to write psuedo code algorithms using nested conditional statements. This activity will take approximately 20-25 minutes. 

Learning Goals/Outcomes:

Students will be able to 

  • Write and test conditional expressions using comparison operations
  • Given an English description write code (if statements) to create desired program logic
  • Use the comparison operators (<, >, <=, >=, ==, !=) to implement decision logic in a program

Nebraska Standards:

NE.CIS.HS.7.8C Interpret the flow of execution of algorithms and predict their outcomes. (Algorithms can be expressed using natural language, flow and control diagrams, comments within code, and pseudo code)

NE.CIS.HS.8.1.F Describe algorithms using foundational principles of sequence, selection, and iteration with non-code techniques (e.g. pseudo code, flow charts, sequence diagrams.)

Teacher Planning:

Equipment/Materials/Software Needed:

Handouts (attached)

Time Required for Lesson:

45 minutes -- lesson or activities could be split or extended

Diagram/Setup:

For the first activity, teacher may choose to give out cards as students come in to class, or wait to explain the activity to pass out cards. Otherwise, there is no set up. 

Technology Use:

Software:

N/A

Hardware:

N/A

Instructional Plan:

Anticipatory Set/Pre-Activity:

Depending on pacing of the course, this activity would follow instruction about conditional statements and nested conditional statements. Teacher can use the example of "Is it the weekend?" needing to be broken in to two conditions. Is it Saturday or is it Sunday? We need to check two conditions, so nesting an if statement is the solution. Teacher can ask the class if they can think of other examples of nested conditions and share as a class or with a shoulder partner. (Other examples: traffic light has to check three colors and what the output would be. Checking ages <16 --"sorry you can't drive yet"; <18 "sorry you can't vote yet"; >25 -- "you can do anything that is legal")

Benefits/Explanation/Real-World Connection:

Students may have a simple common-sense intuition about how if-statements work, but there are several ways you can get your wires crossed when considering how programs actually execute. There are two main issues: 1) how the flow of program execution works, and 2) How complex logical statements are composed and evaluated. These activities lead to and understanding of the logic so that it can be executed when students are programming code. 

Activities (i.e. instructions, warm-up, lesson, cool-down):

Activity #1: Use handout "Partner Cards" resource attachment. On that attachment, there are multiple pages. On each page is pseudo code (top) and the partner match which includes an "input" and the "output" of the psuedo code. There are many that are similar so that students can't solve and find their partner just by looking for key words. Therefore many problems/code have the same topic (ie. test scores, ACT dates). Teacher should print and cut the papers in half. (To save paper, I print multiple pages per sheet in the printer options). Originally I thought this activity would be quick, but when I did it in class it took closer to 10-15 minutes. Optional: have students sit with their new partner for the next activity to mix up who they work with on a daily basis. 

Differentiation of this activity: you could follow up with discussion questions: What made this activity hard? How did you solve that? Did anything make it more efficient when looking for your partner? 

Activity #2: (Handout: Nested If/Else Statements). There are three scenarios and two boxes under each. On the left hand side, have students write psuedo code for each scenario. Ask them to ignore the right hand side for now. Options: have them work independently then compare with their new partner; have them work together on the left side scenarios; or do the first one together, and each partner do one of the remaining each. 

Optional during same lesson or for next day: Show video on Boolean Operators, or teach the symbols of the operators. Most kids will know some that they use in math classes, but a few will be new to them. Then follow up with the second column of the worksheet having students substitute their psuedo code using the operators. If you found that they struggled with the first column, encourage them to be "more efficient" with their code in the second column. 

Closure:

Follow up discussion: It's likely that solutions to the last problem varied considerably. Point this out as a positive.

  • Programming is a creative activity.
  • When you are planning a solution to the problem, you are thinking about algorithms
  • There are multiple correct solutions
  • This is because there are multiple ways to think about the problem
  • There are also multiple algorithms for solving it
  • Even if you used the same algorithm, the code might be different.

Discussion Questions: How many coding solutions were there? Why are different solutions possible? 

Assessment :

At this point in the learning, the assessment would be formative. 

Activity #1 -- Teacher should walk the room and assist where needed finding partner/pair. Try not to give answers, but ask leading questions that could help them problem solve and find the solution. 

Activity #2 -- teacher can pick up and make sure that the logic is there. It doesn't need to be perfect, but that the logic can be followed. Give feedback on any that need assistance.

Supplemental Information:

Modifications:

Each activity could be used stand alone, or as a supplement to something the teacher already does. You can pair the activities for a full 45 minute lesson. 

Teacher could take out the second column on the second handout if you don't want to teach Boolean Operators. I had students go back to this scenario after learning Boolean Operators and write the same pseudo code, but using those operators. Due to time, I had to teacher the operators on another day. 

Videos in the resources part of this lesson could be used in class with students, or just as teacher resources. 

Safety Precautions:

None

Comments (adaptations for various grades/ages, teaching styles, etc.)

See comments above. Other variations you could do is have students write examples of their solutions on a white board. Teachers could go from partners to groups of 4 AFTER filling out their worksheet to compare answers. 

Resources:

Vocabulary needed:

  • Conditionals - Statements that only run under certain conditions.
  • If-Statement - The common programming structure that implements "conditional statements".
  • Nested If-Statement - An if statement inside another if statement. 
  • Boolean - A single value of either TRUE or FALSE
  • Boolean Expression - in programming, an expression that evaluates to True or False.
  • comparison operators (<, >, <=, >=, ==, !=) -- less than, greater than, less than or equal to, greater than or equal to, equals (or has the value of), not equal to)

Video 1: Part 2 If/Else Statements

Video 2: Boolean Expressions

Video 3: "And & Or" Operators

Download: Partner_cards_for_Conditional_Statements_yIl3Eim.pdf


Download: Nested_If_Statement_Review.pdf


Return to top