EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

If Statement in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » If Statement in C

If Statement in C

What is If Statement in C?

If Statement is simply a set of operation which could be used to compare expressions. These generally have two values of LHS and RHS. This operator compares the expression of the left-hand side and right-hand side. In comparison, it simply returns a Boolean value

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The general syntax of If Statement in C is,

if(expression to be evaluated ) {
// sets of instruction which needs to be executed
}

Explanation of Syntax

Now, let us understand the above syntax

A general If Statement compromises in an above-mentioned manner and it contains different parts. Let us explain each part

  • Expression to be evaluated – In this part evaluation of the statement is done. This section generally comprises of the left-hand side and right-hand side. Both left-hand sides which are LHS, as well as a right-hand side which is RHS, is compared and evaluated. If the LHS is equal to RHS or the expression is true then the control enters in the if-section
  • Sets of instruction which needs to be executed – If the expression of the if block is satisfied then the sets of instruction which needs to be executed are executed

A typical example of the first part could be if “1 is less than 10” and a simple example of code which needs to be executed could be to print any number

Different Types of If Statement

These are different types of If Statement. Let us explain in-depth with syntax

  • If-else statement
  • If-elseif-else statement

If-else statement

In this syntax is similar to:

if(expression to be evaluated ) {
// sets of instruction which needs to be executed
} else {
// sets of instruction which needs to be executed
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

If-elseif-else statement

In this syntax is similar to:

if( expression to be evaluated ) {
// sets of instruction which needs to be executed for if-block
} else if{
// sets of instruction which needs to be executed for else-if block
} else {
// sets of instruction which needs to be executed for else block
}

In this section, each block is evaluated and the code is executed as per the evaluation

Now let us see the general flow-chart of If Statement in C

Flow diagram of If Statement

Below given represents a flow diagram of If Statement.

Flow Diagram of If Statement

Examples

Now, let us understand the above-mentioned syntax with examples

If Statement example

Let us look at this with an example

#include <stdio.h>
void main () {
int varNumValue = 1;
if( varNumValue < 10 ) { // checks the condition
printf("if statement instructions");  // sets of instructions which needs to be executed
}
}

Now, copy the above code snippet and run it

It will show the following output

if statement instructions

If-else statement example

Let us look at this with an example

#include <stdio.h>
void main () {
char favoritePlaceToVisit[] = "New York";
if (favoritePlaceToVisit ==  "New York") { // checks the condition
printf(" Your favorite place to visit is New York ");  // sets of instructions which needs to be executed for if block
} else {
printf("Your favorite place is different city");  // sets of instructions which needs to be executed for else block
}
}

Now, copy the above code snippet and run it

It will show the following output:

your favorite place is different city

Now, let us initialize the variable favoritePlaceToVisit with value say “Vegas” so that else block gets executed

your favorite place is different city

Let us look at this with an example

#include <stdio.h>
void main () {
char favoriteFruit[] = "Apple";
if (favoriteFruit ==  "Kiwi") { // checks the condition
printf("You like to eat Apple");  // sets of instructions which needs to be executed for if block
} else {
printf("You don't like to eat Apple");  // sets of instructions which needs to be executed for else block
}
}

Now, copy the above code snippet and run it

It will show the following output:

you dont like to eat apple

Now, it is easy to understand what is If Statement and what is an if-else statement

Example of if-elseif-else statement

#include <stdio.h>
void main () {
int enterNumberOfCarsYouHave = 1;
if( enterNumberOfCarsYouHave == 1 ) { // checks the condition
printf("You have one car");  // sets of instructions which needs to be executed for if block
}
else if( enterNumberOfCarsYouHave == 2 ) { // checks the condition
printf("You have two cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 3 ) { // checks the condition
printf("You have three cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 4 ) { // checks the condition
printf("You have four cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 5 ) { // checks the condition
printf("You have five cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 6 ) { // checks the condition
printf("You have six cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 7 ) { // checks the condition
printf("You have seven cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 8 ) { // checks the condition
printf("You have eight cars");  // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 9 ) { // checks the condition
printf("You have nine cars");  // sets of instructions which needs to be executed for if else block
}
else {
printf("You have more than 10 cars");  // sets of instructions which needs to be executed for else block
}
}

Now, copy the above code snippet and run it

It will show the following output:

you have one car

Conclusion

C is a programming language where there are lots of concepts that one needs to study. If the statement is one of those. These operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation it executes the code. And if the statement is widely used in any programming language to various logical programming expressions

Recommended Articles

This is a guide to If Statement in C. Here we discuss the different types of If Statement with the appropriate explanation of the Syntax along with sample code. You may also have a look at the following articles to learn more –

  1. C# if Statement
  2. If-else Statement in C
  3. If Else Statement in Python
  4. If Statement in Python

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More