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!

If ... 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

May be interested

  • How to Compile C Programs with GNU (GCC)Photo of 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 SharpPhoto of How to Design and Implement a Field Using C Sharp
    the 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 CPhoto of How to Make Your Windows Forms Application Run at Windows Startup Using C
    sometimes 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 StudioPhoto of How to Set Up an OpenGL SDL GLEW Template Project in Visual Studio
    many 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++Photo of 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++Photo of 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.