This is the third part of a 12 part series of blog posts about programming in the C language. This tutorial will guide you through learning C programming, but since there are already a lot of different tutorials out there on the web to learn the basics of programming I’ll not give you yet another tutorial on the exact same thing. These tutorials will instead give you a small description of the different pieces making up the C language. It will give you some links to learn the absolute basics and then continue with a discussion. My hope is that this discussion will enhance your learning, giving additional tools to understand the content on the other sites. The discussion will also hopefully give you additional value in understanding more about the possibilities, limitations, and usefulness of different concepts in different situations.
Table of Contents
The 12 parts are made up of the following posts:
- Introduction to C programming
- Data Types
- Allocating Memory
- Error Handling
- Other Goodies
In programming as well as in real life, selection is about making choices. Sometimes you only have one choice and sometimes you have a lot to choose between. Sometimes you need to answer with a yes or no and sometimes you need to answer with some additional information.
When using selection in C programming we have all these kinds of situations and to handle them there are several ways to go about. You may use if-statements, if-else-statements, if-elseif-else-statements or switch-statements. The if statement can be seen as one of the most simple types of selection. You give it a condition and depending on if that condition evaluates to true or false the if statement will execute its code or not. If-else-statements work in a similar fashion as if-statements, but with the addition that if the condition of the if-statement is false then the else-statement run its code. Else-if statements work as a chain to the if-statement, if the condition of the if-statement is false then you try with the else-if-statement and then continue through eventual else-if-statements until you reach the end of them or an else-statement. Another way to do chaining in this fashion is to use switch-statements which instead of a condition takes a value and checks cases of that value. If no case covers the input in the variable then a default-case can handle the input.
So what are these conditions? The condition is an expression which evaluates to yes or no, true or false, or as it’s often represented in C, 1 or 0. The condition can be built by joining together different conditional functionalities. There are:
- Functions which results in a boolean value (1 or 0).
- Simple values (positive values evaluates to true (1) and 0 to false).
These values can be combined with different operations, the two most common being the and-operation and the or-operation. If the right and left side of the and-operation is true then this evaluates to true, else it’s false. The or-operation evaluates to false if both sides are false, else it’s true.
To learn more about selection read these two parts on tutorialspoint and watch these two videos:
Now when you know the basics of selection it can be a good idea to think a bit about when to use what. It can also be valuable to see different ways that selection can be used to optimize or simplify the code.
Something that might be interesting to realize early is that the if-statement actually evaluates the condition to a 1 or a 0, a true or a false. This means that you can just write a variable as the condition and this will be enough to fulfill the condition. If you have a function which returns a value (more on that in the section about functions), this value returned from the function can be used to fulfill the condition. If the variable or function is a positive value then C will see this as true and run the if-statement, if it’s 0 it will evaluate to false and skip the if-statement.
A trick that can be used in C is to omit the brackets which show what to run if the if-statement, else-if-statement or else-statement gets a true condition. This is possible to do if there is only one line of code that should be executed and is adviced to start practicing when you start to feel comfortable with selection in C. If you’d forget the brackets by mistake then the code would in worst case seem like it works in most cases but sometimes give you errors which are hard to track. But when used properly this omittment can really help making the code look more simple and clean.
One more thing that can be interesting to note is that selection statements can be nested. This means that an if-statement can reside inside another if statement. This is perhaps not so strange but it means that if you have several if statements, some omitting brackets and some not you’d have to think about which else-statement belongs to which if-statement. You can also write if statements directly after another if statement with omitted brackets. This makes the second if-statement check its condition if the condition of the first if statement is true and then if that is true, run whatever statement it should run. By nestling if-statements in direct succession in this way you can often clear up a lot of brackets and reduce a lot of overhead which would otherwise be there.
If you want to see how this nested if-statements can look you can check out a sample I did on tutorialpoints online C-compiler.
There is, of course, more to be known about selection, much of which can be learned if you conduct further studies on the internet or by reading a book on C programming. If you have any further questions don’t hesitate to write a comment and I’ll try my best to give a good answer. Did you like the post and/or did you think that I left out something important which you want to add or ask about? Like the blog post and leave a comment below, spread the word about it chats or social media, I’ll really appreciate it!