CJCoding With Joseph
15free / daySign inCreate account

Mastering C Textbook

Using if Statements for Conditional Logic

Conditional statements let you control the flow of your program based on different conditions. Here’s how to use if statements to evaluate and act on various conditions in C.

 

Equals To

To check if two values are equal, use the double equals sign ==

if (x == 50) {
    printf("x is equal to 50");
}

 

Greater Than

Use > to check if a value is greater than another.

if (x > 50) {
    printf("x is greater than 50");
}

 

Less than

Use < to check if a value is less than another.

if (x < 50) {
    printf("x is less than 50");
}

 

Greater Than Or Equals To

Use >= to check if a value is either greater than or equal to another.

if (x >= 50) {
    printf("x is greater than or equal to 50");
}

 

Less Than Or Equals To

Use <= to check if a value is less than or equal to another.

if (x <= 50) {
    printf("x is less than or equal to 50");
}

 

Not Equal To

Use != to check if a value is not equal to another:

if (x != 50) {
    printf("x is not equal to 50");
}


If, Else If, Else

With if-else if blocks, you can handle multiple conditions. Once a condition is met, the program stops checking others.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 11 more lessons in Mastering C Textbook.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.