If – else Block in C Language

This C Logical Programs expalins  “if-else block” post provides syntax, flowchart and C logical programs to understand more clearly about if-block and else-block in C language.


 

if-else :if-else :

  • “if-block” executes a block of instructions only if the given condition is true.
  • “else-block” is allowed to provide messages to user in case of failed conditions.

 

 

void main()
{
int n;
printf(“enter one number : “);
scanf(“%d”,&n);
if(n%2 == 0)
printf(“%d is even\n”,n);
else
printf(“%d is odd\n”,n);
}

Note : If we don’t specify the body ( { } ) for if-block or for else block, the statement which is followed by block consider as block implicitly.

void main()
{
if(1)
printf(“Hi\n”);
else
printf(“hello\n”);
printf(“Bye\n”);
}

Check this program output :
void main()
{
if(1)
printf(“Hi\n”);
printf(“Welcome\n”);
else
printf(“hello\n”);
printf(“Bye\n”);
}

What is the output of this code :
#include<stdio.h>
void main()
{
int a=400,b,c;
if(a>=300)
b=300 ;
else
a=b ;
c=a ;
printf(“%d,%d,%d”,a,b,c);
}

Share this