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
- 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
May be interested
- How to Compile C Programs with GNU (GCC)tipsmake today will teach you how to compile a c program from source code using gnu (full name is gnu compiler collection, abbreviated gcc) - compiler for linux and minimalist gnu (mingw) on windows.
- How to Design and Implement a Field Using C Sharpthe first step in designing a class usually is to define its fields. fields are the backbone that classes rely upon, and so, the process of designing and implementing fields is crucial to the overall process of designing classes. this...
- How to Make Your Windows Forms Application Run at Windows Startup Using Csometimes you need to run your windows forms application when the user log in to windows. many applications that use this behavior are windows live messenger and yahoo messenger. in this article you will learn how to make your application...
- How to Set Up an OpenGL SDL GLEW Template Project in Visual Studiomany programmers prefer opengl for graphics. if you are one of them, you are strongly advised by its producer, to use a window toolkit (such as sdl) and an opengl loading libraries (such as glew). this guide will help you get over the...
- How to Use Function Template Parameter Packs in C++c++ template parameter packs (also known as variadic templates) were introduced in the c++11 standard and are powerful tools that let functions accept an arbitrary number of arguments. without further ado, jump into your favourite ide,...
- How to Create a 20 Questions Game in C++this tutorial will walk you through creating 20 questions in c++ with numbers using visual studio. this tutorial is very 'bare bones' and only uses the basics of c++ programming. obtain a copy of visual studio and open it.