Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

If....Else:

The if statement in C is used for decision making. It executes a block of code if a specified condition is true. Optionally, an else statement can be used to execute a block of code if the condition is false. Here's the basic syntax:

if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }

Here's an example:

#include int main() { int x = 10; if (x > 5) { printf("x is greater than 5\n"); } else { printf("x is not greater than 5\n"); } return 0; }

Output:

x is greater than 5

In this example, if the condition x > 5 is true, the statement inside the if block will be executed. Otherwise, the statement inside the else block will be executed.

You can also use nested if-else statements, where an if statement is placed inside another if or else block. This allows for more complex decision-making logic. Additionally, you can use else if to specify additional conditions to check if the initial condition is false. Here's an example:

#include int main() { int x = 10; if (x > 10) { printf("x is greater than 10\n"); } else if (x < 10) { printf("x is less than 10\n"); } else { printf("x is equal to 10\n"); } return 0; }

Output:

x is equal to 10

In this example, x is equal to 10, so the condition in the else if statement is false. Therefore, the code inside the else block is executed.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java