CodesandTutorials
Next generation website theme is here (black, neon and white color) - Reduces eye strain and headache problems.  
You are here - Home > Programming > C > Basics

If Statement


Description

If statement checks whether the test expression satisfies the condition or not. If it satisfies it enters into block of if statements bounded by curly braces.

Syntax

if (test expression is true)
{
statement1;
statement2;
statement3;
}

Source code


  • /* Using of If statement */
  • #include <stdio.h>
  • #include <conio.h>
  • void main()
  • {
  • int stack;
  • clrscr();
  • printf("Enter the value of stack = ");
  • scanf("%d",&stack);
  • if(stack>10)
  • printf("Stack is overflow");
  • getch();
  • }

Program working steps

  • First line of the program is the comment, 2nd & 3rd lines are preprocessor statments which include definition of functions like printf, scanf, clrscr etc.
  • From void main() function the program execution starts.
  • Declaration of variable stack with integer data type.
  • clrscr() function clears the console window.
  • 9th line prints the characters quoted in double quote and 10th line asks for user input and stores the value entered into the variable stack.
  • If condition expression is true then the next statment on line no.12 will be executed printing 'Stack is overflow'. And if expression is false then 12th line statement will be ignored.
  • So user should enter value greater than ten to execute the if block statement.
  • In the above example we have used single statement condition body, you can use multiple statement condition body using opening and closing curly brackets.
  • getch() function takes the single input character from the keyboard and that character is not display on the console screen.

Tip Box

You can use multiple condition expression in if condition statement like:
if (i>1 && j<2)


Advertisement





Terms of Use | Privacy Policy | Contact Us | Advertise
CodesandTutorials © 2014 All Rights Reserved