![]() |
| If-else Statement |
The if-else statement is a simple conditional statement that allows you to choose the appropriate course of action based on a Boolean condition. The if-else statement is found in any programming language, including C. In this article, you will learn how to use the if-else statement in C.
1. What is the if-else statement?
An if-else statement is a programming statement that checks the value of a condition and executes one of two or more statements depending on the value, The if-else statement in C is used to evaluate a condition and return a value based on that condition. The if-else statement takes two parameters: the condition and the consequent. The if-else statement is used to make decisions about what code to run.
2. How to use the if-else statement in C.
The if-else statement is a conditional statement that is used to evaluate and then execute one of two statements depending on the outcome of the evaluation. The syntax for the if-else statement is: if (condition) { statement1; } else { statement2; } The condition evaluates to true or false and then executes the statement or the statement2. The condition can be any expression, such as the following: if (x == 2) The if statement evaluates to true if x is equal to 2. If the condition evaluates to false, the if statement will execute the statement2.
3. Example of if else statement
The following example illustrates how to use an if-else statement in C.
int main( )
{
if (x > 0)
{ printf("The value of x is greater than zero "); }
else
{ printf("The value of x is less than zero "); }
return 0;
}
The following example illustrates how to use an if-else statement in C. if (x > 0) { printf("The value of x is greater than zero "); } else { printf("The value of x is less than zero "); }
4. conclusion
Before you can use an if-else statement in C, you need to understand how a C program works. The first line of a C program is the main function. It is followed by any external functions that you have written. If the program is a function, it is followed by a block of statements that define the function. This block of statements is followed by a return statement that ends the function. After the return statement, the program continues with the next function.
------------------------------
