UNIT II
Decision Making & Branching
C has decision-making statements to support conditional logic. C conditional
statements allow you to make a decision based upon the result of a condition.
These statements are called Decision Making Statements or Conditional
Statements. C has a number of alternatives to add decision-making in the
code. In programming, we come across situations when we need to make some
decisions. Based on these decisions, we decide what should we do next.
Similar situations arise in algorithms too where we need to make some
decisions and based on these decisions, we will execute the next block of code.
This type of structure requires that the programmers indicate several
conditions for evaluation within a program. The statements will be executed
if the condition becomes true, and optionally, if the condition
becomes false, an alternative statement or set of statements will be executed.
The flowchart of the Decision-making technique in C can be expressed as:
Decision making is about deciding the order of execution of statements based
on certain conditions or repeat a group of statements until certain specified
conditions are met. C language handles decision-making by supporting the
following statements,
• if Statement
• if-else Statement
• Nested if Statement
• if-else-if Ladder
• switch Statement
• Conditional Operator
• Jump Statements:
➢ break
, ➢ continue
➢ goto
➢ return
Decision Making with If Statement:
In C programming, the if statement is a fundamental control structure used
for decision making. It allows you to execute a block of code if a specified
condition evaluates to true.
Syntax:
if (condition)
{
// block of code to be executed if the condition is true;
}
, Flowchart of If Statement:
If the condition is fails the control goes outside the if statement. The if
statement in C allows you to control the flow of your program based on
conditions. For example write a program in C to find if the entered number
is positive or negative:
#include <stdio.h>
void main( )
{
int num;
printf ( “ Enter a number”);
scanf ( “%d”, &num);
if (num > 0)
{
printf("%d is a positive number.\n", num);
}
if (num < 0)
{
printf("%d is a negative number.\n", num);
}
if (num = = 0)
{
printf (“Entered number is Zero”);
}
}
In this example:
Decision Making & Branching
C has decision-making statements to support conditional logic. C conditional
statements allow you to make a decision based upon the result of a condition.
These statements are called Decision Making Statements or Conditional
Statements. C has a number of alternatives to add decision-making in the
code. In programming, we come across situations when we need to make some
decisions. Based on these decisions, we decide what should we do next.
Similar situations arise in algorithms too where we need to make some
decisions and based on these decisions, we will execute the next block of code.
This type of structure requires that the programmers indicate several
conditions for evaluation within a program. The statements will be executed
if the condition becomes true, and optionally, if the condition
becomes false, an alternative statement or set of statements will be executed.
The flowchart of the Decision-making technique in C can be expressed as:
Decision making is about deciding the order of execution of statements based
on certain conditions or repeat a group of statements until certain specified
conditions are met. C language handles decision-making by supporting the
following statements,
• if Statement
• if-else Statement
• Nested if Statement
• if-else-if Ladder
• switch Statement
• Conditional Operator
• Jump Statements:
➢ break
, ➢ continue
➢ goto
➢ return
Decision Making with If Statement:
In C programming, the if statement is a fundamental control structure used
for decision making. It allows you to execute a block of code if a specified
condition evaluates to true.
Syntax:
if (condition)
{
// block of code to be executed if the condition is true;
}
, Flowchart of If Statement:
If the condition is fails the control goes outside the if statement. The if
statement in C allows you to control the flow of your program based on
conditions. For example write a program in C to find if the entered number
is positive or negative:
#include <stdio.h>
void main( )
{
int num;
printf ( “ Enter a number”);
scanf ( “%d”, &num);
if (num > 0)
{
printf("%d is a positive number.\n", num);
}
if (num < 0)
{
printf("%d is a negative number.\n", num);
}
if (num = = 0)
{
printf (“Entered number is Zero”);
}
}
In this example: