KEMBAR78
Decision Making Using If | PDF | Control Flow | Programming Paradigms
0% found this document useful (0 votes)
9 views12 pages

Decision Making Using If

The document discusses decision-making and branching in C programming, highlighting the use of conditional statements such as if, if-else, nested if-else, else-if ladder, and switch case statements to control program flow based on specific conditions. It provides syntax examples and flow diagrams for each type of statement, along with practical coding examples demonstrating their usage. The document also includes examples related to salary adjustments, voting eligibility, finding the largest number, and a grading system.

Uploaded by

saranya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Decision Making Using If

The document discusses decision-making and branching in C programming, highlighting the use of conditional statements such as if, if-else, nested if-else, else-if ladder, and switch case statements to control program flow based on specific conditions. It provides syntax examples and flow diagrams for each type of statement, along with practical coding examples demonstrating their usage. The document also includes examples related to salary adjustments, voting eligibility, finding the largest number, and a grading system.

Uploaded by

saranya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Structured Programming in C: Decision Making & Branching -I

Decision making & Branching

 The decision-making process in C Programming involves the use of


conditional statements to control the flow of program execution.
 Decision-making statements let the programmers to make decisions based on
specific conditions or criteria.
 Branching allows the program to execute different sets of code based on the
outcome of certain conditions.

The general form of a standard decision making structure

 C language handles decision-making by supporting the following statements:


 if statement
 if-else statement
 nesting if-else statement
 else-if ladder statement
 switch case statement

Simple if statement:

 If statement is used to control the flow of execution of statements.


 It is a two-way decision making statement.
 Simple If statement is used to evaluate the expression or condition first and if
it is true the “if block” will be executed or if it is false, the flow of control
passes to the immediate step next to “if” block.
NITHIYAPRIYA PASAVARAJ Page 1
Structured Programming in C: Decision Making & Branching -I

The syntax for if-statement is:

if(condition)
{
Block of statements;
}

Flow Diagram of Simple if:

Example:

#include <stdio.h>
void main()
{
int salary;
printf("Enter Basic salary of an employee:");
scanf("%ld",&salary);
if(salary < 20000)
{
salary = salary+5000;
}
printf("Employee Salary is:%ld\n",salary);
getch();
}

NITHIYAPRIYA PASAVARAJ Page 2


Structured Programming in C: Decision Making & Branching -I

Output
Enter Basic salary of an employee: 10000
Employee Salary is: 15000
Enter Basic salary of an employee: 30000
Employee Salary is: 30000

If – else Statement:

 The if-else statement is an extension of the simple if statement


 The if-else statement is used to carry out a logical test and then take one of
two possible actions depending on the result of the test.

Syntax:

if (condition)
{
// code to execute if the condition is true
}
else
{
// code to execute if the condition is false
}

Flow Diagram of if-else statement

NITHIYAPRIYA PASAVARAJ Page 3


Structured Programming in C: Decision Making & Branching -I

Example:

#include<stdio.h>
void main()
{
int age;
printf(" Enter your real age:");
scanf("%d",&age);
if(age >= 18)
{
printf("you are eligible for voting.");
}
else
{
printf("you are not eligible for voting.");
}
}
Output:
Enter your real age: 21
You are eligible for voting
Enter your real age: 16
You are not eligible for voting

Nested if-else statement:

 Nested if-else statements are used, when a series of decisions are concerned,
we may have to employ more than one if-else statement.

Syntax of nesting of if-else statement:


if(test expression-1)
{
if(test expression-2)
{
Block of statement-1;
}
else
{
Block of statement-2;
}
else
{
Block of statement-3;
}
statement-n;

NITHIYAPRIYA PASAVARAJ Page 4


Structured Programming in C: Decision Making & Branching -I

Flowchart of nested if-else statements

Example: Write a program to display largest number from three integer numbers
using nested if-else statements.

#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2,num3;
printf("Enter three integer numbers:\n ");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1 > num2) {
if(num1 > num3) {
printf("Largest Number is: %d\n", num1);
}

NITHIYAPRIYA PASAVARAJ Page 5


Structured Programming in C: Decision Making & Branching -I

else {
printf("Largest number is: %d\n", num3);
}
}
else {
if(num3 > num2) {
printf("Largest Number is: %d\n",num3);
}
else {
printf("Largest Number is: %d\n",num2);
}
}
getch();
}
Output
Enter three integer numbers: 20 400 100
Largest Number is: 400

NITHIYAPRIYA PASAVARAJ Page 6


Structured Programming in C: Decision Making & Branching -I

Else-if ladder statements:

 Else-if ladder statements are used, when multipath decisions are concerned.
 If-else ladder statement is used to test set of conditions in sequence that is if
we have different test conditions with different statements then we need else-
if ladder statements.

Syntax:

NITHIYAPRIYA PASAVARAJ Page 7


Structured Programming in C: Decision Making & Branching -I

Flowchart:

Example:

Assume an example of grading system of the students in an institution. The grading


is done according to the following rules:

NITHIYAPRIYA PASAVARAJ Page 8


Structured Programming in C: Decision Making & Branching -I

#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
printf("Enter your Marks:");
scanf("%d",&marks);
if(marks>100)
{
printf("\nInvalid Marks. Marks must be less than 100");
}
else {
if(marks<=100 && marks>=80)
{
printf("Your Grade is A+");
}
else if(marks=75)
{
printf("Your Grade is A-");
}
else if(marks=70)
{
printf("Your Grade is A");
}
else if(marks=65)
{
printf("Your Grade is B+");
}
else if(marks=60)
{
printf("Your Grade is B-");
}
else if(marks=55)
{
printf("Your Grade is B");
}
else if(marks=50) {
printf("Your Grade is C+");
}
else if(marks=45) {
printf("Your Grade is C");
}
else if(marks=40) {
printf("Your Grade is D");

NITHIYAPRIYA PASAVARAJ Page 9


Structured Programming in C: Decision Making & Branching -I

}
else {
printf("Your Grade is F");
}
}
getch();
}
……………………………………………………………………………….
Output
*******
Enter your Marks: 78
Your Grade is A
Enter your Marks: 120
Invalid Marks. Marks must be less than or equal 100

Switch case statement:

 Switch statement checks the value of a given expression against a list of case
values and when a match is found a block of statements associated with the
case is executed.

Syntax:

NITHIYAPRIYA PASAVARAJ Page 10


Structured Programming in C: Decision Making & Branching -I

 The expression is an integer expression or characters.


 Constant-1, Constant-2, Constant-3…. Constant-n are known as case labels.
 Here, each of these values must be unique within switch statement and bock-
code-1, bock-code-2, bock-code-3… bock-code-n are statement lists.
 When switch is executed the variable or value of the expression is successively
compared against the cases.
 If a case is found or matched with the value of the expression, then the
associated block code or statement(s) are executed.
 The break statement at the end of the each block code means the end of
specific case and causes an exit from the switch statement.
 If the break statement is missed from any case, then program executes all
statements until get break statement from another case.
 The default is optional. When variable or value of expression does not match
with any of the case values then default statement(s) are executed.

Flowchart:

NITHIYAPRIYA PASAVARAJ Page 11


Structured Programming in C: Decision Making & Branching -I

Example:

Assume an example of grading system of the students in an institution. The grading


is done according to the following rules:

NITHIYAPRIYA PASAVARAJ Page 12

You might also like