Decesion Making in C/C++(if, if-else, Nested if, if-else, switch)

Decesion Making in C





Decesion making is a control structure that allows you to select one or more execution paths in a program. It causes a program to execute a block of code after evaluating a given conditional expression. If the decesion making comes out to be true, the accompanying block of code is executed; otherwise, the block of code is bypassed.

Following is the list of the conditional statements
  1. if statement
  2. if-else statement
  3. Nested if statement
  4. if-else ladder
  5. switch statement


if Statement

The if statement executes a simple or compound statement, depending on whether or not an expression is true.

Syntax and flow chart of if statement


if(test_expression)
{
  statementBlock1;
}
statementX;

A statement block1 is executed when the test_expression given in the if statement is ture. Otherwise, the program control passes to the statementX. If the test_expression is false then the C compiler does not do anything. Note that the test_expression given in parentheses, must be evaluated as true the (non-zero value) or false (zero value). Some example of using a test_expression in the if statement are given as follows.

if(7)      /* a non-zero value returns True*/
if(0)      /* zero value returns False*/
if(i==0)   /* True if i=0 otherwise False*/
if(i = 0)  /* False because value of the expression is zero*/

Example show the use of if statement along with a simple statement:
if(a>b)
i = i + 1; /* statement A*/

j = j + 1; /* statement B*/
   In the above example, when the conditional expression a > b is true, the statement A is executed otherwise not.
     The following example shows the uses of the if statement along with a compound statement. If the conditional expression a>b is true, both statements marked as A and B are executed.

if (a>b)
{
  i = i + 1; /* statement A*/
  j = j + 1; /* statement B*/

Some examples to use the if statement :


Using the if statement
using if statement

OUTPUT
Run 1
Enter your age: 17
You are not eligible to give vote.
Run 2
Enter your age: 18
You are eligible to give vote.

Program to find the largest of three number
WAP to find the largest three numbers
OUTPUT
Run 1:
Input a b c : 15 20 55
Largest of the three number = 55

Run 2;
Input a b c : 88 198 102
Largest of the three number = 198

To calculate the total pay of a Employee

#include<stdio.h>
main()
{
int empno, itc;
float bpay, bonous, hra, total=0.0;
char category;
printf("\nGive Category :");
scanf("%c", &category);
printf("\nGive Employee Number :");
scanf("%i", &empno);
printf("\nGive Basic Pay :");
scanf("%f", &bpay);
if(category=='r')
{
bonus=bpay*50.00/100.00;
ltc=5000;
hra=bpay+bonus+ltc+hra;
}
else
{
bonus = bpay*30.0/100.0;
ltc=2000;
hra=bpay*10.0/100.0;
total=bpay+bonus+ltc+hra;
}
printf("\nBasic Salary = %g", bpay);
printf("\nBonus = %g",bonus);
printf("\nLTC = %i",ltc);
printf("\nHouse rent = %g", hra);
Printf("\nTotal salary = %g", total);

}

OUTPUT
Run 1:
Give Category : r
Give Employee Number : 23
Give Basic Pay : 4000
Basic = 2000
Bonus Rent = 1000
Total Salary = 12000

Run 2 :
Give Category : a
Give Employee Number : 13
Give Basic Pay : 10000
Basic Salary = 10000
Bonus = 3000
LTC = 2000
House Rent = 1000
Total Salary = 16000


if-else Statement

The if-else statement executes a simple or compound statement when the test expression provide in the if statement is true. It executes another simple or compound statement, followed by the else statement, when the test expression is false.

if else structure
In the above image syntax, when the test_expression specified in the if clause is true, the statementBlock1 is executed; otherwise, statementBlock2 is executed.

Example

Checking the Age for Vote
WAP to checking age for vote

OUTPUT
Run 1:
Enter your age : 24
You are eligible to give vote

Run 2:
Enter your age : 16
You are not eligible to give vote.

Checking the number for Nonzero
WAP to Checking the number for nonzero

OUTPUT
Run 1:
Input an integer: 12
It is non-zero.

Run 2:
Input an integer : 0
It is zero.


Nested if

The term nested if statement means one if statement conatins another if statement. The control of a program moves into the inner if statement when the outer if statement is evaluated to be true.
Follow syntax is used to create the nested if statement:

if(test_expression1)
{
  statement1;
  if(test_expression2)
  {
    statement2;
  }
}

Example:
Using Nested if statement
WAP Using Nested if statement

OUTPUT
Run 1:
Enter a number greater than 0: 200
The number is greater than 100.
The number is greater thsn 50.
The number is greater than 0.

Run 2:
Enter a number greater than 0: 76
The number is greater than 50.
The number is greater than 0.


if-else Ladder

When more than one if-else statements are used in a sequence, it is called as if-else ladder. The syntax to use the if-else ladder is:

if(test_expression1)
{
  statementBlock1;
}
else if(test_expression2)
  {
    statementBlock2;
  }
    ............
    else
  {
    statementBlockX;
  }
statementY

if-else Ladder
Example:
WAP using if-else ladder

OUTPUT
Run 1:
Enter an alphabet : e
It is a vowel.

Run 2:
Enter an alphabet: h
It is not vowel.

switch Statement

A switch statement is a conditional statement that tests a variable against different values. If the value is matched, the corresponding groups of statement is executed.

Syntax to use a switch statement

switch(variable)
{
case value1:
   statementBlock1;
   break;
...................,
case valueN:
   statementBlockN;
   Break;
 default
   statementBlockX;
}
statementY;

--------------------------------------------------------------------------------------------------------------------------



Recommended Posts:

--------------------------------------------------------------------------------------------------------------------------
If you want to depth knowledge about Programming Buys Follows books
Books links given below



Post a Comment

0 Comments