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!
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
if
to specify a block of code to be implemented if a specified condition is true. - Used
else
to specify a block of code to be implemented if the same condition is false. - Used
else if
to define a new condition for the test if the first condition is false. - Used
switch
to identify multiple alternative code blocks to be implemented.
if statement in C
Use a command if
to 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 if
writing 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 else
to 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 > 0
will return true, so the statement printf
in the block if
will be executed and print "a is a positive number" to the screen.
else if command
Use the command else if
to 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 if
is also false
, so we will switch to the condition else
because condition1
and condition2
are 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.
You should read it
- Rust - interesting programming language worth learning
- Do you know what programming language is?
- In the end, big universities realized that Java was a lousy language if used for introductory programming
- Overview of R language, install R on Windows and Linux
- Flow control in C programming
- The reason why C programming language is never outdated
- Switch in C
- Why should you learn Python programming language?
- BREAK (Control Interrupt) command in SQL Server
- 16 programming languages will change your luck
- What is the first programming language in the world?
- Who invented the Pascal programming language?
Maybe you are interested
How to create Progress Bar in Excel, conditional progress bar
Air conditioner control application by iPhone
China discovers 'super-survival' moss that can withstand weather conditions 'like on Mars'
Why is the minimum air conditioner copper pipe length 3m?
Google Lens has been updated with skin condition detection features
What is the condition of the computer being fixed D?