Conditional statement #1

if else statment:-
                                         The if statement is a powerful decision-making statement and is use to control the flow of execution of statement.




  1. Syntax of if stament 

if(condition)
{
  statement
  ................
  ................
} 

Example:-

#include<stdio.h>
#include<conio.h>
void main()
{
  int n;
  clrscr();
  printf("Enter a number: ");
  scanf("%d",&n);
  if(n<0)
  n=-n;
  printf("\nAbsolute value: %d",n);
  getch();
}

Output:-
Enter a number: -10
Absolute value: 10

           2. if else statement 

Syntax of if else statement

if(condition)
{
  statement
  ................
  ................
else
{
  statement
  ................
  ................
} 

Example:-

#include<conio.h>
#include<stdio.h>
void main()
{
  int n, m;
  clrscr();
  printf("Enter two number: ");
  scanf("%d %d",&n, &m);
  if(n>m) /* <, > <=, >=, ==, !=(not equal to) called relational operator*/
    printf("\n%d is greater",n );
  else
    printf("\n%d is greater",m);
  getch();
}

Output:-
Enter two number: 5
2
5 is greater
       3. Nested if else 

Syntax of nested if else
  
if(condition)
{
  statement
  ................
  ................
else if(condition)
{
  statement
  ................
  ................
}
else
{
  statement
  ................
  ................
else if(condition)
{
  statement
  ................
  ................
}

Example:-
 
#include<conio.h>
#include<stdio.h>
void main()
{
  int
  clrscr();
  printf("Enter three number: ");
  scanf("'%d %d %d", &a, &b, &c);
  if(a>b)
     if(a>c)
        printf("%d is largest", a);
     else
        printf("%d is largest", c);
  else if(b>c)
     printf("%d is largest", b);
  else
     printf("%d is largest", c);
  getch();
}
Output:-
Enter the three number: 4
3
8
8 is largest. 


If any doubt or query comment below......
In next blog we will learn switch case is a part of conditional statement. 

Key Note:- After if condition there is only one line program then no need to add curly {} braces

No comments

Powered by Blogger.