Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

If. Else in C: Complete Overview

Review If Else Condition in C through the main facts, background, key details, and practical implications in this clear overview for readers.

Table of Contents

What is the If … Else condition in C ? What is the meaning and usage of If Else statement in C ? Let's find out with TipsMake.com!

Key Takeaways About If Else Condition in C

  • What is the meaning and usage of If Else statement in C?
  • The details are explained below. with TipsMake.com!
  • Conditions and IF commands in C You already know that C supports logical conditions from mathematics: Smaller: a Less than or equal to: a Greater than: a > b Greater than or equal to: a >= b

If. Else in C Picture 1 - If Else Condition in C

Conditions and IF Commands in C

You already know that C supports logical conditions from mathematics:

  • Smaller: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equals: a == b
  • Not equal to: a != b

You can use these conditions to implement different actions for other decisions. C has the following conditional statements:

  • Used ifto specify a block of code to be implemented if a specified condition is true.
  • Used elseto specify a block of code to be implemented if the same condition is false.
  • Used else ifto define a new condition for the test if the first condition is false.
  • Used switchto identify multiple alternative code blocks to be implemented.

If Statement in C

Use a command ifto specify a block of code to be deployed if a condition is true.

Recipe:

if (condition) { // kh?i code ???c tri?n khai n?u ?i?u ki?n là true }

Note that ifwriting is in lower case. If you write it in capital letters (If or IF), you will get an error result.

The example below tested two values ??to see if 20 is greater than 18. If that condition is true, you will receive the message:

if (20 > 18) { printf("20 l?n h?n 18"); }

You can also try variables:

int x = 20; int y = 18; if (x > y) { printf("x l?n h?n y"); } 

Example explanation:

The above example uses two variables, x and y, checking whether x is greater than y using the >. Since x is 20, y is 18, it is clear that 20 is greater than 18 and you will see the following message on the screen: x is greater than y .

Else Command

Use the command elseto specify the block of code to be deployed if the condition is false.

Recipe:

if (?i?u ki?n) { // Các câu l?nh ???c th?c thi n?u ?i?u ki?n là ?úng } else { // Các câu l?nh ???c th?c thi n?u ?i?u ki?n là sai }

For example:

int a = 5; if (a > 0) { printf("a là m?t s? d??ngn"); } else if (a < 0) { printf("a là m?t s? âmn"); }

Example explanation:

In the above example, the expression a > 0will return true, so the statement printfin the block if will be executed and print "a is a positive number" to the screen.

Else If Command

Use the command else ifto specify a new condition if the first condition is false.

Recipe:

if (condition1) { // Kh?i code ???c tri?n khai n?u condition1 là true } else if (condition2) { // Kh?i code ???c tri?n khai n?u condition1 là false và condition2 là true } else { // Kh?i code ???c tri?n khai n?u condition1 là false và condition2 là false }

For example:

int time = 22; if (time < 10) { printf("Good morning."); } else if (time < 20) { printf("Good day."); } else { printf("Good evening."); } // Outputs "Good evening."

Example explanation:

In the above example, time (22) is greater than 10, so the first condition is false. The next condition in the command else ifis also false, so we will switch to the condition elsebecause condition1and condition2are both false- and the screen result will display Good evening.

However, if the time is 14, the program will say 'Good day'.

Above are the basic things you need to know about the If…Else condition in C. Hope the article is useful to you.

FAQ

What is the main update about If Else Condition in C?

What is the meaning and usage of If Else statement in C ?

Why does If Else Condition in C matter?

Let's find out with TipsMake.com!

What should readers know about If Else Condition in C?

Conditions and IF commands in C You already know that C supports logical conditions from mathematics: Smaller: a Less than or equal to: a Greater than: a > b Greater than or equal to: a >= b Equals: a == b Not equal to: a != b You can use these conditions to implement different actions for other decisions.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.