Site icon TestingDocs.com

C if Statement

Overview

In this tutorial, we will learn about C if statement with some examples. The if statement is a two-way decision statement and is used with conditional expression.

C if Statement

The if statement is a decision-making statement and is used to control
the flow of execution of C program statements.

Syntax

Let’s understand the basic syntax of the statement. The general syntax of the statement is as follows:

if (condition)
{
      // block of program statements executed
     // if the condition is true;
}

The block of statement is usually called the if-block and would be executed only if the condition or the conditional expression evaluates to true. If the condition is false the if-block will be omitted.

Example

A simple C program to illustrate the usage of the if statement.

int main()
{
    //declare a number
    int number = 15;

    //C if statement
    if (number > 10)
    {
       printf(“%d”, number);
      }

return 0;
}

 

In this example, the if block would be executed and the number would be printed on the console.

The variable number is declared and initialized with 15. Since, 15 > 10 the  condition number > 10 would be true. Hence, the if block would be executed.

In the next tutorial, we will learn another variation of the statement. The if-else statement.

C Tutorials

C Tutorials on this website can be found at:

https://www.testingdocs.com/c-language-tutorial/

Exit mobile version