×

If Statement in C

Decision Control Statements

We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance. A decision control instruction is used in C programs to handle situations of this nature.

By using decision control statements, we can direct the program's flow so that it executes specific statements based on the result of a condition.

We have the following decision control statements in the C programming language.

(a) The If statement

(b) The If-else statement

(c) The Conditional operators

In this article, we will talk about the if statement.

If Statement

When making judgments in C, the if statement is employed. A block of statements will be performed or not depends on whether the if statement has verified the specified condition and evaluated the outcome.

Syntax:

If(condition)
{
Statements;
// If the relational operator's conditions are met, statements will run.
}

An If statement is always followed by a condition enclosed in a pair of parentheses. The assertion is carried out if the condition is true. If the mentioned condition is not true, the statement will not be performed and the compiler skips over it.

Using C's "relational" operators is the norm when expressing a condition. In order to determine whether two values are equal to one another, different from one another, or greater than one another, we can compare them using relational operators.

If Statement Rules:

  • The Boolean expression or Condition is always closed with the parenthesis.
    For an example, (x>=7).
  • The Boolean expression or Condition will not be terminated by the semicolon (;). If this happens, the block of the statement will not execute even if the condition is true.
  • The block of statements should be enclosed between the two curly braces.

Flow Diagram:

If Statement in C

 The block of code enclosed by the if statement will be executed if this condition is satisfied. The if statement's inner block of code will not run if the condition is false.

Program-1:

#include<stdio.h>
int main( )
{
int number ;
printf ( "Enter a number less than 9:\n " ) ;
scanf ( "%d", &number ) ;
if ( number <= 9)
printf ( "What an intelligent student you are !\n" ) ;
}

Explanation:

  • We have one integer-type variable named "number" in the main function.
  • When the program runs, a message stating "Enter a number less than 9" will appear on the computer screen due to the printf function.
  • The scanf function accepts keyboard input from the user.
  • The compiler will arrive at the if condition after receiving input.
  • If the condition is met, the compiler checks to see if the number is less than or equal to 9.
  • If this criterion is met, the printf function will be executed, and the message "What a bright student you are!" will be displayed on the screen.
  • But if the condition is not met, the printf function won't run, and the program will terminate.

Program Output:

If the condition is true then the following result will occur:-

If Statement in C

If the condition is not satisfied then the result will occur:-

If Statement in C

Program-2:

#include <stdio.h>
int main() {
    int negative_number;
    
    printf("When a condition is met, this programme will check which number is negative.\n");
    printf("Enter your any desired value: \n");
    scanf("%d", &negative_number);


    if (negative_number < 0) {
        printf("After condition is satisfied the value is: %d\n", negative_number);
    }


    return 0;
}

Explanation:

  • We have one integer-type variable named " negative_number" in the main function.
  • When the program runs, a message stating " When a condition is met, this programme will check which number is negative." will appear on the computer screen due to the printf function.
  • After that, one more printf function executes, and we get a message “Enter your any desired value” on our computer screen.
  • The scanf function accepts keyboard input from the user.
  • The compiler will arrive at the if condition after receiving input.
  • As if the condition meets, the compiler checks to see if the number is less than 0 or not.
  • If this criterion is met, the printf function will be executed, and the message " After condition is satisfied the value is: " will be displayed on the screen.
  • But if the condition is not met, the printf function won't run, and the program will terminate.

Program Output:

If the condition is true, then following result will occur:

If Statement in C

If the condition is not satisfied, then following result will occur:

If Statement in C

Related Topics

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.