If-Else Construct – A C Programming Selection Structure

Posted by & filed under Blackboard, Chart, Education, infographic, Information Technology, Technology, Training, Video.

I teach information technology (IT) courses including programming courses. One of the programming languages I teach is C, and one of the first constructs the students need learn is the if-else construct, which is a selection-type programming structure. Selections allow the program to branch in different directions based on a condition, which usually has to evaluate to either true or false, e.g., like a fork in a road. I created this lesson to help the students understand the construct, i.e., how it works, how you might use one, etc.

Announcement

At the beginning of the week and prior to the class, I would display an Announcement in Blackboard concerning what we will be covering and what the students need to complete before coming to class; I will also discuss this in the previous weeks’ classes. What they will need to do is to participate in the if-else discussion.

If-Else Discussion

For this discussion, the student’s original post will involve them doing research, or alternatively making something of their own creation, e.g., a video. This part of the lesson will provide the students a way to find or create something unique, they found useful for understanding the if-else construct and share it with their fellow students. It will also involve the creation of their own small program to demonstration a simple example. Here is what they will need to provide in their original post:

  • A summary of what they found or created
  • A link to something, e.g., video, tutorial, etc., or embedded video, etc., to a unique resource
  • An attached Word document with their example code demonstrating the use of the if-else construct

The discussion will be available at least a week prior to the class, and the students will be expected to reply to at least three other students’ original posts with their thoughtful feedback. In addition, as part of the instructions, I will provide an example of the kind of original post I am expecting along with questions and ideas, about which they can think and use, when replying to someone else’s post. BTW, any videos, etc. used as part of the course material will be off-limits; see http://faculty.valenciacollege.edu/carchibald/ComputerProgrammingConcepts.htm or http://faculty.valenciacollege.edu/carchibald/CProgramming.htm. That brings us to the day of class.

In-Class Sharing

During the first few minutes of class, we will discuss what they found, what they liked, what they learned, etc., and display one or two of the most interesting based on their feedback during the discussion. I will also use this opportunity to correct any misunderstandings and an audio and visual demonstration to explain further an if-else construct in comparison to just an if construct.

Demonstration

Using infographics similar to the following, I will demonstrate the basic idea of an if versus an if-else construct:

If-Else Construct FlowchartIf-Else Flowchart

I normally use analogies concerning taking a trip to the mall, where

  • An if construct is like taking a side trip to pick up a friend before proceeding to the mall
  • An if-else construct is like coming to a fork in the road, i.e., you can take one or the other path, but both will end up at the mall

Now, it is time to get down to coding.

Coding

At this point, we will open Visual Studio, if they do not have it open already, because by now, they should be doing this as soon as they get into class and setting up a project. BTW, my plan is to create short videos concerning how to set up Visual Studioand create a C project. Together we will create one or more programs demonstrating the if-else construct, starting with something simple like displaying one message on the screen if the condition is true or a different message if it is false. I will use this opportunity to show them and discuss the following items:

  • How to create a single statement construct versus a multiple statement construct, and the pitfalls related to the single statement version
  • The pitfalls of using the assignment operator (=) instead of the equality operator (==)
  • The pitfalls of misplacing a semicolon immediately after the if condition
  • How an else construct is optional, but if one is used, it has to be associated with an if construct, and that will be the first previous one the else finds
  • How the else keyword means exactly the opposite of the if condition, e.g., if the if condition is x > 1, then, the else means x <= 1, but else does not have a condition of its own, so all they need to write is the keyword else
  • The use of code conventions, specifically the ones we are following for the class, and how they help to make their code readable and avoid the above pitfalls, and their else constructs associate with the correct if

While they compile their code, I walk around the classroom and assist with any syntax and code convention issues. This brings us to the summary.

Summary

At this point, I will use a video to summarize and reiterate the basic concepts of the if versus if-else constructs such as:

Then, I will discuss their homework and answer any of their questions.

Leave a Reply