IF, ELSE, & IF-ELSE Statements in C Programming

IF, ELSE, & IF-ELSE Statements in C Programming
Coming up next: Switch Statements in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Decision Making in C…
  • 0:43 If Statements
  • 0:58 If-Else Statements
  • 1:55 If-Else-If Statements
  • 2:48 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami
This article demonstrates and explains if, if-else and if-else-if statements in C programming language. It also explains how to use a combination of these statements to best fit a problem scenario.

Decision Making in C Programming

The IF-ELSE statement is used to follow a certain set of instructions based on the result of a decision. Consider a situation in real life when you would want to make a decision based on a condition. Let's take a very basic example that we face in everyday life.

  • If it rains, you will use an umbrella; otherwise you will not.

In this statement, we unconsciously make a decision of using the umbrella based on the condition that it's raining. In any programming language, these decisions and conditions need to be explicitly mentioned, which is exactly why we need an IF-ELSE statement.

Here is an IF-ELSE statement flowchart:

IF-ELSE statement flowchart
IF-ELSE statement flowchart

If Statements

The condition inside the parentheses of the IF statement is evaluated. If the result of the test condition is true, then the instructions inside the IF body are executed. If the result of the condition is false, then the statements are skipped.

if (test condition) {
 
//statements executed if the test condition is true
}

If-else Statements

Notice that the difference between IF statement and IF-ELSE statement is that there's an additional ELSE attached to it. The IF statement is executed if the statement inside the test condition evaluates to true; otherwise the statements inside the ELSE block is executed.

if (test condition) {
 //statments executed if test condition is true
} else {
 //statements executed if the test condition is false
}

Now let's consider an example. The test condition for this piece of code is inside the parentheses of the IF statement. In this case, we will evaluate whether or not a is lesser than 40. If we notice, a is initialized to 100, which is clearly not less than 20; so, what do you think will be the output of the following code?

#include <stdio.h>
int main () {
 /* variable declaration */
 int a = 100;
 /* check the boolean condition and evaluating this condition */
 if( a < 40 ) {
  /* if condition is true then print the following */
  printf("a is less than 20\n");
 }
 return 0;
}

The output of the code we just looked at doesn't yield any result because the test condition isn't satisfied and the conditions inside the IF statement isn't executed. Let's add an ELSE statement to this piece of code.

#include <stdio.h>
int main () {
 /* variable declaration */
 int a = 100;
 /* check the boolean condition and evaluating this condition */
 if( a < 40 ) {
  /* if condition is true then print the following */
  printf("a is less than 20\n");
 } else {
  /* if condition is false then print the following */
  printf("a is not less than 20\n");
 }
 return 0;
}

In this case the output is:

a is not less than 20

IF-ELSE-IF Statements

In this case, the IF statement is followed by a series of ELSE-IF statements. This can be very useful if you want to test various types of scenarios and situations. Let's reconsider the aforementioned piece of code with IF-ELSE-IF statement.

if (test condition 1) {
 //statments exeute if the test condition 1 is true
} else if(test condition 2) {
 //statements executed if test condition 2 is true
} else {
 //statements executed if all the test conditions above result in a false value
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support