Skip to main content

Decision Control Statements in C - FCST

 Decision Control Statement

**NOTE**
PDF version of this post also available. to get the PDF of Decision Control Statements in C click the link given at end of this post.
All statements written in a program are executed from top to bottom one by one. Control statements are used to execute/transfer the control from one part of the program to the another depending on condition. These statements are also called conditional statement. There are two types of decision control statements-
1) If-else statement.
2)Switch statement.

If-else statements has three types. They are 
i) normal if-else statement.
ii)else-if statement.
iii) nested if-else statement.
If-else statement
The if  statement plays a vital role in conditional branching. Its usage is very simple, the test expression is evaluated, if the result is true, the statement(s) followed by the expression is executed else  if the expression is false, the statement is skipped by the compiler.
Syntax of if-else statement 
If(condition)
{
    true block
}
else
{
    false block
}
Example: -
Write a c program to find out biggest of two numbers.
#include<stdio.h>
main()
{
int x,y,big;
printf("enter the value for x = ");
scanf("%d", &x);
printf("enter the value for y = ");
scanf("%d", &y);
if(x>y)
{
big=x;
printf("biggest of given two number is %d",big);
}
else
{
big=y;
printf("biggest of given two number is %d",big);
}
}
Explanation of this example
1. take two number as input.
2. after taking input it started the if block. In If block it checks with y. if isn't bigger than x then the value of x assign into big & print this as output.
3. otherwise in else block the value of y assign into big & print this as output.
Output



Example of else-if


#include<stdio.h>
main()
{
int num1,num2,num3,big;
         printf("enter the value for num1:");
         scanf("%d",&num1);
        printf("enter the value for num2:");
        scanf("%d",&num2);
       printf("enter the value for num3:");
       scanf("%d",&num3);
if(num1>num3)
{
if(num1>num3)
big=num1;
else
big=num3;
}
else
{
if(num2>num3)
big=num2;
else
big=num3;
}
printf("biggest no=%d",big);
}
Program explanation:-
1. Take the three numbers and store it in the variables a,b,c respectively.for example we take num1=3,num2=2, num3= 4

2. Firstly check if the num1 is greater than num2.


3. If it is, then check if it is greater than num3.


4. If it is, then print the output as “num1 is the greatest among three”.


5. Otherwise print the ouput as “num3 is the greatest among three”.


6. If the num1 is not greater than num2, then check if num2 is greater than num3.


7. If it is, then print the output as “num2 is the greatest among three”.



8. Otherwise print the output as “num3 is the greatest among three”.
Output of this program





If-else-if /nested if-else statement
The if-else-if construct works in the same way as a normal if statement If-else-if construct is also known as nested if-else construct. 
Syntax of nested if-else statement
if(condition)
{
  if(condition)
{
   statement block
}
else
{
  statement block
}
}
else
{
  if(condition)
 {
   statement block
  }
  else
  {
    statement block
   } 
}
Example of this statement


#include<stdio.h>

main()

{

int a,b,c,big;

printf("enter any three no");

scanf("%d%d%d",&a,&b,&c);

if(a>b)
{
if(a>c)
big=a;
else
big=c;
}
else
{
if(b>c)
big=b;
else
big=c;
}
printf("biggest no=%d",big);
}
Output of this program
Switch case statement
A switch case statement is a multi way decision statement that is a simplified version of an if -else block that evaluate only one variable. 
Syntax of Switch case statement
switch(expression)
{
  case(label name):
  ..........
  break;
  case(label name):
  .........
  break;
  default:
  break;
}
example of switch case
#include<stdio.h>
main()
{
int m, i;
printf("\n Enter any number: ");
scanf("%d", &m);
i = m%10;
switch(i)
{
case 10:
case 9:
     printf("O");
     break;
case 8:
     printf("E");
     break;
case 7:
     printf("A");
     break;
case 6:
     printf("B");
     break;
case 5:
     printf("C");
     break;
case 4:
     printf("D");
     break;
default:
       printf("F");
}
}

Explanation of this program
Case 1:- If the user is Over smart then there is a condition that if the mark given by the user is greater than 100 then our program display the Message "Don't Be Smart Enter your Marks Between Limit" or else perform the Else part.

Enter the Mark:- 1000

Output:- Don't Be Smart Enter your Marks Between Limit.

Case 2:- If the user is entering the marks between the 0 to 100 and then particular grade portion will be executed and display the output in Console screen.


Enter the Mark:- 95

Output:-Your Grade Is: A or Excellent

This operation performs same for Grade B, C, D.


Case 3:- If Enter marks are not fulfilled the cases requirement than the program will perform the default case.


Enter the Mark:-25

Output:-You Grade Is: F or Fail

Output




To download the PDF of this post click here

PDF of decision control statement in C

Comments

Popular posts from this blog

Solve-write C program to find biggest number from three number by using nested if statement

          C program to find biggest number from three number                                                by using                                                                 nested if statement Problem Description This program takes the 3 numbers and finds the biggest among all. Problem Solution 1. Take the three numbers as input. 2. Check the first number if it greater than other two. 3. Repeat the step 2 for other two numbers. 4. Print the number which is greater among all and exit. Program codes:- here the program of finding biggest of three number run this in your Linux, DEV C++,turbo c++ #include<stdio.h> main() { int num1,num2,num3,big;   ...

Important MCQ of RDBMS( Relational database management system)-FCST

Important MCQ of RDBMS  1. A RDBMS consists a collection of ? a. Tables b. Fields c. Records d. Keys  ANS/- a. table 2. The term attribute refers to a ___________ of a table a. Record b. Tuple c. Column d. Key   ans/- c. Column 3. In relational model, the row of table is known to be ?  a. Relation b. Entity field c. Tuple d. Attribute  ans/- C. Tuple 4. . Address field of a person should not be part of primary key, since it is likely to ? a. Dependent b. Too long c. Changed d. Not changed  ans/- c. Changed 5. The relational model is concerned with ? a. Data structure and Data integrity b. Data Manipulation c. Both a and b d. None of these  ans/- c. Both a and b 6. Which is the false statement from the following ? a. A veiw is a named derived table b. A name relation is variable c. A veiw is a named reation and is virtual d. None of these  ans/- d. None of these 7. The union of primary key...

Short notes on database schema & database independence- computer science fundamentals tutorial

            Database Schema &  Data Independence PDF version of this post also available. to download the click the link given at the end of this post.           Database Schema : -  A database schema   is the skeleton structure that represents the logical view of the entire database. It defines how the data is organized and how relat ion among them are associated.  Database schema  formulates all the constrains that are to be applied on the data. A database schema defines its entities and the relationship among them. It contains descriptive details of the database which can be described by means of schema diagram. A database schema can be divided into two categories.   I.     Physical database schema : - this schema contain to the actual storage of data and its form of storage like files, indices etc. It defines how the data will be stored in a secondary storage. ...