We're back to exploring programming basics, and this week: if/else statements!
A few weeks ago, we talked about arithmetic operators in Arduino/C. We're back with another riveting round of programming basics, and now we're talking about conditional statements. They're super important in most programming languages, as they allow you to change how the program flows.
To use conditional statements (and later, loops), we first look at relational operators, which, in their basic form, compare two numbers. For example, does 5 equal 7? Here are the six basic relational operators in C:
Operator | Description |
---|---|
== | Is equals? |
!= | Is not equals? |
> | Greater than? |
< | Less than? |
>= | Greater than or equal to? |
<= | Less than or equal to? |
In C, these operators return an integer: 1 for true and 0 for false. We can feed these into the conditional statements in order to change the flow of the program:
if ( some relational operation ) {
if above is true, execute this
} else if ( another relational operation ) {
if first relational operation is false but second is true, execute this
} else {
otherwise (both are false), execute this
}
While this is old news for many of you, how would you recommend wrapping these programming basics videos up for someone who might be new to it? Would you prefer to see it in a playlist? As part of a class with homework problems and answers? I'm trying to figure out how to package these into something that could be useful for a beginner, so ideas are welcome! Please share in the comments below.
Amazing and cool information. Thanks for sharing.
I think you've hit many of the core ideas. As a software engineer, a few things I find will help reduce bugs in code:
As mentioned elsewhere, putting constants on the left side of your conditional can help catch some errors (for instance,
7 = a
rather thana = 7
). This is more obvious to a human eye and compilers can flag this error.Put the shorter condition block first. Rather than:
Instead do:
I have watched new programmers learn conditionals, exercises are crucial to this process. In other words, definitely yes to the home works.
The constant before the variable is genius! I will definitely have to start using that. I have to admin, I don't readily see what the sorter condition block does? Is that to make it easier to read on a single screen?
I wanted to let you know that I am using your videos as part of the summer classes that I am doing for a High School First Robotics team. So keep churning them out :)
What I am doing with this summer session is using the Arduino as a simple hard to mess up C++ programming environment. So I am going to do doing lessons on Pointer,Classes and, Algorithms and such. I hope to get the students to produced more structured programs, with the thought in mind that from an education point of view a FRC robot could implemented with an Arduinio controlling each of various subsystem (ball pick up, shooter, etc) but to do that we need to get a lot more kids up to speed on programming.
A few things from my experiences:
My biggest issue with the homework of online classes is they do not show you the bigger picture of when to use certain key features of the language. Why use an if-statement, when a unary/ternary statement can do the same thing? The times I learned a language the best was when I was given a practical application in which to code. This helps teach the specific aspects of the syntax you want to teach, but also teaches the students to test their code. Plus, as they get more familiar with the code and application, they can start doing coding optimizations themselves to meet certain specifications of the design.
I think a playlist with a companion set of 'homework' problems would be nice. It's always easier for me to learn by doing. Perhaps a basic set of problems and one 'extra credit' challenge per video would work. You could discuss the solutions on the next video in the series.
A small critique on the video: prior to discussing the operators, there were a few seconds of blank screen, and I had to check to make sure my video was still playing. Perhaps a background image could be used instead of a blank, black screen.
Overall I think the videos are worth continuing. Good job!
You read my mind with the "homework" problems :) I've got about a dozen videos planned for the programming section that I'd like to get through, and then I'm considering making a tutorial (or something similar) on our site with the playlist and homework problems with solutions for people to work through.
Thanks for the heads up on the black screen. I've used it before in presentations, which works well when there's a presenter to attract visual attention, but you're right that in a video it can off putting.
Something to consider for future educational content is how to write an Arduino library for a new part from start to finish. I don't know enough about the topic to know if that's perhaps too broad a subject, but I'd certainly like to understand more about the inner workings of what makes a library tick.
That's definitely on my list of "advanced Arduino programming" topics. However, I've found that it helps to use good object-oriented practices and write a class for each part. That means a whole new set of videos on OOP. Looks like I've got my work cut out for me :)