C Menu


Decision Statements




In the C programming language, the program execution flow is in sequential from top to bottom. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code. Decision-making statements are the statements that are used to verify a given condition and decide whether a block of statements gets executed or not based on the condition result.

In the c programming language, there are two decision-making statements they are as follows.

  1. if statement
  2. switch statement

if Statement

The "if" is a keyword, which tells the compiler that what follows is a decision control instruction. The if statement verifies the given condition and decides whether a block of statements are executed or to be ignored. In c, if statement is classified into following types.

  • Simple if statement
  • if-else statement
  • Nested if-elses (if-else ladder)

Simple if statement

The general form of simple if statement looks like this:


Syntax
if ( this condition is true ) 
	execute this statement ;

If the condition,then the statement is executed. If the condition is not true then the statement is ignored; instead the program skips past it.

Following are the test conditions:
(7)          // a non-zero value returns True.
(0)           // zero value returns False.
(i==0)      // True if i=0 otherwise False.
(i = 0)      // False because value of the expression is zero.

Program:
/* Demonstration of if statement */
main( )
{
	int num ;
	printf ( "Enter a number less than 10 " ) ;
	scanf ( "%d", &num ) ;
	if ( num <= 10 )
		printf ( "What an obedient servant you are !" ) ;
}
Output:
Enter a number less than 10
5
What an obedient servant you are !

if-else statement

The if statement by itself will execute a single statement, or a group of statements, when the condition is true. It does nothing when the condition is false.

In if-else statement we execute one group of statements(true block) if condition is true and execute another group(false block) of statements if condition is false.


Syntax
if ( this condition is true ) 
	true block of statements;
else
	false block of statements;

Program:
/* Demonstration of if-else statement */
#include
main()
{
   int n ;
   printf("Enter any integer number: ");
   scanf("%d", &n) ;
   if ( n%2 == 0 )
      printf("Given number is EVEN\n");
   else
      printf("Given number is ODD\n");
}
Output:
Enter any integer number:
5
Given number is ODD

Nested if-elses

We already saw how useful if and else statements are, but what if we need to check for more conditions even when one condition is satisfied?

For example, if we need to analyse if the number is even or odd, and then if it is even, whether it is divisible by 4 or not, and if it is odd, whether it is divisible by 3 or not. In such a case, only one if and else statement wouldn’t suffice.

In the program an if-else occurs within the else block of the first if statement. Similarly, in some other program an if-else may occur in the if block as well. There is no limit on how deeply the ifs and the elses can be nested.


Syntax
if ( condition ) 
	true block of statements;
else if ( condition )
	true block of statements;
else
	false block of statements;


Syntax
if ( condition ) 
	if ( this condition is true ) 
		true block of statements;
	else
		false block of statements;
else 
	if ( this condition is true ) 
		true block of statements;
	else
		false block of statements;


Program:
/*Nested if-elses statement */
/*C program to find the largest  of three numbers*/
#include 
main()
{
	int a, b, c;
	printf("Enter three numbers: \na: ");
	scanf("%d", &a);
	printf("b: ");
	scanf("%d", &b);
	printf("c: ");
	scanf("%d", &c);
	if (a > b && a > c)
		printf("Biggest number is %d", a);
	else if (b > c)
		printf("Biggest number is %d", b);
	else
        printf("Biggest number is %d", c);
}
Output:
Enter three numbers: 
a: 10
b: 5
c: 2
Biggest number is 10

Next Topic :Loop Statements