UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to study the Conditional Statements

C program to study conditional statement

by Krishna viswambharan


Posted on 24 Aug 2017 Category: C Views: 441

Edited on 25 Aug 2017


C program to find largest among two numbers using 'if-else' conditional statement

This article explains various conditional statements in C language  with sample programs. Conditional statements in C language are given as below

  • If statement
  • If –Else statement
  • Nested If-else  statement
  • Switch statement

These are also called branching statements  because it can jump from a particular block of a program to another block based on whether a particular condition is satisfied or not.

Selection with ‘If’ Statement

This is the simplest decision making statement in C language. It allows the machine to compute the expression depending on the value of the expression is ‘true’ or ‘false’.

 The syntax is given as follows :-

      if(test expression)
      {
            Statement(s);
      }
      Statement(s);

Selection with ‘If-Else’ Statement

We can say that ‘if-else’ is an extension of ‘if’ statement. The syntax is given as

if(test condition)
{
	True block statement(s);
}
else
{
    false block statement(s);
}

  If the test condition is true , then the true block of statements will execute and if the condition is false the control goes to else part and execute the false-block of statements.

The program given below is an example of ‘ if-else’ control flow. Let us see how it works.

#include <stdio.h>
#include <conio.h>
void main()
{
	int n1,n2;
	printf(“enter the first number :”);
	scanf(“%d”,&n1);
	printf(“enter the second number :”);
	scanf(“%d”,&n2);
	if(n1>n2)
	{
		printf(“%d is the largest number”,n1);
	}
	else
	{
		printf(“%d is the largest number”,n2);
	}
	getch();
} 



Leave a Comment:


Advertisement