Module-2-Chapter-2-Decision Branching Statemnt
Module-2-Chapter-2-Decision Branching Statemnt
Decision making
and Branching
Introduction
Conditional Statements
1. Simple if statement
2. if-else statement
3. Nested if-else statement
4. else-if ladder
5. switch statement
These statements control the flow of
execution known as control statement.
• Example
int mark = 10;
if (mark < 35)
{
printf("Fail");
}
• Example:
int mark=40;
if (mark > 35)
{
printf(“Pass");
}
else
{
printf(“Fail");
}
Nested of if………else statements
• Example for if else condition
• #include <stdio.h>
• int main()
• {
• int A, B, C;
•
• printf("Enter three numbers: ");
• scanf("%d %d %d", &A, &B, &C);
•
• if (A >= B) {
• if (A >= C)
• printf("%d is the largest number.", A);
• else
• printf("%d is the largest number.", C);
• }
• else {
• if (B >= C)
• printf("%d is the largest number.", B);
• else
• printf("%d is the largest number.", C);
• }
•
• return 0;
• }
• Example
int temp; =====
temp=38;
if(temp>20)
{
if(temp>40)
printf("Very Hot....\n");
else
printf("Hot...\n");
}
else
{
printf("Cool....");
}
The else if ladder
• Example
float per=45;
if (per >= 66)
{
printf("Distinction\n");
}
else if (per <66 && per >=56)
{
printf("First Class\n");
}
else if (per <56 && per >= 44)
{
printf("Second Class\n");
}
else if (per <44 && per >= 35)
{
printf("Pass Class\n");
}
else
{
printf("Sorry Fail");
}
The switch Statement
• To select one of the many alternatives we use “if”
to control the selection
• Complexities increases when many alternatives
• To deal with this C has built-in multiway decision
statement called switch
• Tests the value of given variable against a list of
case values
• When match found block associated with that
case is executed.
• Example for switch statement
• #include <stdio.h>
• int main()
• { char Operator;
• float num1, num2, result = 0;
• printf(" Enter any one operator like +, -, *, / : ");
• scanf("%c", &Operator);
• printf("Enter the values of Operands num1 and num2
: ");
• scanf("%f%f", &num1, &num2);
• switch(Operator)
• { case '+': result = num1 + num2; break;
• case '-': result = num1 - num2; break;
• case '*': result = num1 * num2; break;
• case '/': result = num1 / num2; break;
• default: printf("
Invalid Operator "); }
• printf("The value = %f", result);
• return 0;
• }
• Where expression is an integer expression or
characters
• Value1…..n are constants or constant expression
known as case labels and each value should be unique
• Block1….n are statements lists
• No need of braces
• Case label end with colon(:)
• Break signals the end of a particular case and causes
exit from switch and transferring control to statement-
x
• Default is optional –executed if value does not match
with any of case
Example -1
#include <stdio.h>
int main()
{
int num;
scanf(“%d”,&num)
switch (num)
{
case 1: printf(“Entered Value is 1");
break;
case 2: printf(“Entered Value is 2");
break;
case 3: printf(“Entered Value is 3");
break;
default: printf("Out of range");
break;
}
return 0;
}
=======================================================================================================================================================================================================================================================================================================================================================================================================================================================================
The goto statement
• C supports to branch unconditionally from one
point to another
• Requires a label to identify the place where
the branch is to be made
• Label is variable name and must be followed
by colon
• Placed immediately before statement where
control is to be transferred
Forward jump
• int main()
{
printf(“hello”);
goto label;
printf(“hi”);
label:
printf(“how are you?”);
}
Backward jump
int main()
{
int num,answer;
label:
printf(“etr a number”);
scanf(“%d”,&num);
if (num>0)
answer=sqrt(num);
printf(“squreroot of %d is %d\n”,num,answer);
goto label;
return 0;
}
Decision making and looping
• To execute a segment of a program repeatedly introduced counter
and tested using if
• When we know exact number of repetitions more convenient
method is looping.
• Enable us to concise programs containing repetitive processes
• Sequence of statements are executed until some conditions for
termination of the loop satisfied
• Program loop consists of two segments
-body of the loop
-control statement
• Control statement tests certain conditions then directs the repeated
execution of the statements contained in the body of the loop
• Depending on the position of the control statements in loop,a
control structure can be classified as either entry and exit
controlled loop.