Decision-making statements in C Programming

Decision-making statements simply make decisions on the execution of the program that which line has to be executed next. Let’s discuss decision making statements in C programming with sample programs. The sample program is given to make a better understanding and most importantly Syntax is given for learning it and applying it on programs

Decision making statements are
 
  • Simple if
  • If else
  • If else if else
  • Nested if
  • If else ladder
Simple if:
Syntax:
if(expression)
{
     //true block;
}
Example Program:
#include<stdio.h>
void main()
{
int number=10;
clrscr();
if(number%2==0) {
  printf(“n The given number is an Even number”);
}
getch();
}
Aim of this program is to find whether the given number is an even number
Key idea: If the number is even, then it will definitely be divisible by 2. Whenever even number is divided by 2, then it will give the remainder as 0(zero).
To be known: syntax of if statement, modulus operator(gives the remainder)  
Program Explanation:
Include header file <stdio.h> for input output operations.
Declare the variable and initialise it to any number (I consider it as 10)
clrscr() – Clears the previous output from output screen
if(number%2==0)-checks whether the given number is divisible by 2
printf()-prints the statement if and only if the condition is true.
getch()-get character–>used to display output.
 
 
If  – else:
Syntax:
if(expression)
{
//true block;
}
else
{
//false block;
}
Example Program:
#include<stdio.h>
void main()
{
int number=10;
clrscr();
if(number%2==0) {
  printf(“\n The given number is an even number”);
}
else {
  printf(“\n The given number is an odd number”);
}
getch();
}
 
Aim of this program is to find whether the given number is an even number or odd number.
Key idea: If the number is even, then it will definitely be divisible by 2. Whenever even number is divided by 2, then it will give the remainder as 0(zero).Otherwise it must be odd number.
To be known: if – else syntax, modulus operator(give the remainder)  
Program Explanation:
Include header file <stdio.h> for input output operations.
Declare the variable and initialize it to any number (I consider it as 10)
clrscr() – Clears the previous output from output screen
if(number%2==0)-checks whether the given number is divisible by 2
printf()-prints the statement if and only if the condition is true.
else – condition in if() is not true
 printf()-prints the statement if and only if the condition is false
getch()-get character–>used to display output.
 
 
If – else if – else:
Syntax:
if(expression)
{
//true block1;
}
else if(expression)
{
//true block2;
}
else
{
//false block;
}
Example Program:
#include<stdio.h>
void main()
{
int number=10;
clrscr();
if(number>0) {
printf(“\n The given number is Positive”);
}
else if(number<0) {
printf(“\n The given number is Negative”);
}
else {
printf(“\n The given number is Zero”);
}
getch();
}
Aim of this program is to find whether the given number is a positive or negative number or neither of them.
Key idea
  • If the number is positive, then it will be greater than 0(zero).
  • If the number is negative, then it will be less than 0(zero).
  • Otherwise it must be 0(zero).
To be known: if – else if – else syntax, conditional operators 
Program Explanation:
Include header file <stdio.h> for input output operations.
Declare the variable and initialize it to any number (I consider it as 10)
clrscr() – Clears the previous output from output screen
if(number>0) – checks whether the given number is greater than 0(zero)
printf() – prints the statement if and only if the condition is true.
else if(number<0) – checks whether the number is less than 0(zero)–>executes only if(number>0) evaluates false
else – condition in both if() and else if() is false
 printf() – prints the statement if and only if both the expressions evaluates false
getch() – get character–>used to display output.
 
 

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *