×

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

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

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.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

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.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.