What Is IF statement In C Language?

What Is IF statement In C Language?


Hello Programmer,

Today we will discuss a powerful decision-making statement which helps you to solve every decision. The (if) statement is very powerful every great programmer is master of if decision statement. This statement solves your condition. The (if) statement is used to control the flow of execution of the statements.


Syntax:

if(condition or test expression)
{
        //Your code
}

Some Important Point:

  • The (if) statement block is executed only when the condition is positive or true.
  • If any case the condition is false the compiler skips the code within the "if block".
  • In c programming language the condition is always enclosed within a pair of parenthesis but in python, parenthesis is not necessary.
  • The code inside the "if" statement is normally enclosed in curly braces {}.
  • The condition statement never terminates with a semi-colon(;). If you do this the code which is written for statement does not count for statement because semi-colon terminates the line and after semi-colon new line start.
  • The curly braces indicate the scope of "if" statement.
  • The "if" statement block may be a single statement or multiple statements.
  • If the condition is true, the code block of "if" statement will be executed and then executes the rest of the program.
  • If the condition is false, the code block of "if" statement will be skipped and the rest of the program executes next. 


Flow Chart:



Program:

we take a simple example to show how to use if statement in the real world.

Q: Write a program to check two number is equal or not.

Ans:

#include<stdio.h>
#include<conio.h>

void main()
{
 //two variable of integer type
int num1,num2; 
clrscr();

//Take two number as input
printf("Enter two numbers: ");
scanf("%d%d",&num1,num2);

//Check two number with if statement
if(num1==num2)
{
printf("\n Two numbers are equal.);
}

getch();
}


Output: Enter two number: 5 5
                    Two numbers are equal.



Sharing Is Caring...

Comments :

Post a Comment