If ... Else in C

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.com!

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!

If ... Else in C Picture 1If ... Else in C Picture 1

Conditions and IF commands in C

You already know that C supports logical conditions from mathematics:

  1. Smaller: a < b
  2. Less than or equal to: a <= b
  3. Greater than: a > b
  4. Greater than or equal to: a >= b
  5. Equals: a == b
  6. Not equal to: a != b

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

  1. Used ifto specify a block of code to be implemented if a specified condition is true.
  2. Used elseto specify a block of code to be implemented if the same condition is false.
  3. Used else ifto define a new condition for the test if the first condition is false.
  4. 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.

4 ★ | 1 Vote