There are several decision making statements supported by C Language. If Statement in C programming has three different forms, if Statement, the if-else statement, else-if statement. Also we’ll cover switch statement. The if keyword tells the compiler that it is decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. Let’s look at syntax of each form of if statement for implementation:
Table of Contents
If Statement in C Programming
The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction.
If Statement Syntax[crayon-5c6ab8f04543f917766708/] Let’s take a look at example code:
[crayon-5c6ab8f04544d212308102/] Here is an output from project:
In the example above the user can input a number. The number is stored in the variable mynumber. Now take a look at the “if statement”: if the number stored in the variable mynumber is equal to ten, then print “Entered Number is equal” on the screen. Simple, isn’t it. If the number is not equal to ten, then nothing gets printed.
Now take another look at the “if statement”: look at the placement of the semi-colon. As you can see there is no semi-colon behind the “if statement”. If there is just one instruction (if the statement is true), you can place it after the “if statement” (with an indentation). When multiple instructions necessary to execute then we will have to use curly brackets, like so (syntax):
[crayon-5c6ab8f045454792695999/] I recommend always putting braces following if statements. If you do this, you never have to remember to put them in when you want more than one statement to be executed, and you make the body of the if statement more visually clear. The modified program will look something like this:
Sometimes when the condition in if statement evaluates to false, it would be nice to execute some code. The “else” statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.
If-Else Syntax[crayon-5c6ab8f045460860880349/] Let’s take a look at an example. We will continue to modify previous example program. Now we like to print something if the “if statement” is not equal. We could do this by adding another “if statement” but there is an easier/better way. Which is using so called “else statement” along with “if statement”. Here is an example:
Example Program: If-Else Statement[crayon-5c6ab8f045466992460326/]
Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement’s body to execute. You can use an “else if” statement following an if statement and its body; that way, if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is to be executed.
Let’s look at a simple program for you to try out on your own.
[crayon-5c6ab8f04546c889327714/] Here is an output from project:
The switch statement is almost the same as an “if statement”. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default. If none of the variable equals the condition the default will be executed. Let’s have a look at example code:
Example Program: Switch Case[crayon-5c6ab8f045473639480833/] Note: “break” is used to exit the switch case.
In the example above the user can input a character. The character is stored in the variable grade. Now take a look at the “switch statement”: if the character stored in the variable grade is equal to ‘a’, then print “Excellent!” on the screen. Simple, isn’t it. If the character is not equal to any ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ then default code get executed and print “Invalid grade” on screen.
We encourage you to make your own programs or to modify existing programs. This is the only way you can learn C Programming. This is all for If Statement in C Programming. In next post we will learn about loops in C Programming Language. I hope you will find this post educational. Feel free to leave a comment.