100% found this document useful (1 vote)
502 views34 pages

Decision Making & Branching PDF

The document discusses control constructs in C programming, including conditional and unconditional constructs. It covers selective constructs like if-else statements, and loop constructs like for and while loops. It describes conditional execution as transferring program flow based on a test condition, and unconditional execution as transferring flow without a test. Nested if-else statements are also discussed, allowing multiple conditions to control program flow.

Uploaded by

Yuvaraj S
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
100% found this document useful (1 vote)
502 views34 pages

Decision Making & Branching PDF

The document discusses control constructs in C programming, including conditional and unconditional constructs. It covers selective constructs like if-else statements, and loop constructs like for and while loops. It describes conditional execution as transferring program flow based on a test condition, and unconditional execution as transferring flow without a test. Nested if-else statements are also discussed, allowing multiple conditions to control program flow.

Uploaded by

Yuvaraj S
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/ 34

DECISION MAKING &

BRANCHING
TYPES OF CONTROL CONSTRUCTS
Classification of control constructs
Control Constructs

Conditional Unconditional

Selective Loop goto break continue

for while do-while

Conditional if-else if-else-if switch-case


Expression
CONDITIONAL EXECUTION
 Conditional Execution
 The flow of execution may be transferred from one
part of a program to another part based on the output
of the conditional test carried out.

 Two types
 Selective construct

 Loop construct
CONDITIONAL EXECUTION…
 Conditional execution…

 Selective construct

 The statement is selected for execution based on the


output of the conditional test given by an expression.

 C supports

- if-else

- if-else-if

- Conditional expression

- switch-case
CONDITIONAL EXECUTION…
• Conditional execution…
– Loop construct
• Sometimes the execution of certain statements need to be
repeated until a given condition is satisfied or it may have to be
repeated for a known number of times.
• Such repetitions are carried out by using a loop construct.
• The loop construct is also known as an iterative construct or
a repetitive construct.
• C supports
– for
– while
– do-while
UNCONDITIONAL EXECUTION
 Unconditional execution

 The flow of execution is transferred from one part of a


program to another part without carrying out any
conditional test.

 C supports

 break statement

 continue statement

 goto construct
DECISION MAKING &
BRANCHING
 C program is a set of statements which are executed
sequentially
 We have situations where we may have to change the
order of execution of statements based on certain
conditions, or repeat a group of statements until certain
specified conditions are met
 C language possesses such decision making capabilities
and supports the statements known as control or
decision making statements
 if statement
 switch statement
 conditional operator statement
 goto statement
DECISION MAKING WITH IF
STATEMENT
 The if statement is the powerful decision making
statement
 Control the flow of execution of statements
 Two-way decision statement
If (test_expression)
 The computer evaluate the expression first and then,
depending on whether the value of the expression
(relation or condition) is ‘true’ (non-zero) or ‘false’ (zero),
it transfer the control to a particular statement
 It has two paths to follow one for the true condition and
the other for the false condition
DECISION MAKING WITH IF
STATEMENT
 The if statement is implemented in different
forms depending on the complexity of
conditions to be tested
 Simple if statement

 if…….else statement

 Nested if…..else statement

 else if ladder
SIMPLE IF STATEMENT

 If statement is a conditional branching


statement.
 In conditional branching statement a condition is
evaluated, if it is evaluate true a group of
statement is executed.
 The simple format of an if statement is as
follows:
if(expression)
statement;
 If the expression is evaluated and found to be
true, the single statement following the "if" is
executed.
 If false, the following statement is skipped.
 Here a compound statement composed of several
DECISION MAKING WITH IF
STATEMENT

 Example:
if (bank balance is zero)
Borrow money
if (room is dark)
Put on lights
if (code is 1)
Person is male
if (age is more than 55)
Person is retired
SIMPLE IF STATEMENT

Syntax:
if (test expression)
{
Statement 1;
}
Next_Statement;
EXAMPLE

#include <stdio.h> OUTPUT:


main() Enter an integer between 1 and
{ 10: 7
int number = 0; You entered 7 which is greater
than 5
printf("\nEnter an integer between 1 and 10: ");
or
scanf("%d",&number);
Enter an integer between 1 and
if(number > 5) 10: 3
printf("You entered %d which is greater than You entered 3 which is less than 6
5\n", number);
if(number < 6)
printf("You entered %d which is less than 6\n",
number);
}
IF-ELSE CONSTRUCT

 Used to select a specific statement based on the


specified condition.
Syntax:
if (expression)
{ expression
statement_1; true false
}
else statement_1 statement_2
{
statement_2;
}

Flow chart representation of if-else construct


IF-ELSE CONSTRUCT

• statement_1 and statement_2 may be simple or


compound.
• If there is a simple statement, the braces are
optional.
• But the braces should surround the compound
statements.
• Also, the else part may be omitted, if not necessary.
• It will result in if construct.
• The expression for the conditional test is always
written within parentheses while using it in any
control construct.
IF-ELSE CONSTRUCT…

 The statement_1 will be executed if the


expression returns a true value; otherwise,
statement_2 will be executed.

 If there is no else part and the expression returns


false value, then statement_1 will be skipped and
the subsequent statements, if any, will be executed.
IF-ELSE CONSTRUCT ILLUSTRATION

Code snippet to find the biggest of two


numbers:
Assume: a, b be the two variables holding
two different numbers Logical flow of the sample code
if(a>b) // a>b is the expression
printf(“a is big”); // Statement1
else
printf(“b is big”); // Statement2
tru fals
a>b
e e

printf(“a is printf(“b is
big”); big”);
/* PROGRAM TO FIND WHETHER AN INTEGER IS
ODD OR EVEN */

#include <stdio.h>
main( )
{
int x,y;
printf("Enter an integer\n");
scanf("%d",&x);
printf("Given number is %d\n",x);
SAMPLE INPUT AND
y = x%2;
OUTPUT
if(y==1)
Enter an integer
printf("The given number %d is
5
ODD\n",x);
Given number is 5
else
The given number 5 is ODD
printf("The given number %d is
EVEN\n",x);
Enter an integer
}
8
Given number is 8
The given number 8 is EVEN
EXAMPLE
/* PROGRAM TO CHECK WHETHER A CHAR IS A LETTER OR NOT
*/
#include <stdio.h>
main( )
{
char ch;
printf("Enter any character \n");
ch = getchar( );
printf("Given character is ");
putchar(ch);
if(ch>='A' && ch<= ‘Z’ || ch>='a' && ch <= ‘z’ )
printf("\n and it is an alphabet.\n",ch);
else
printf("\n and it is not an alphabet.\n");
}
SAMPLE INPUT AND OUTPUT
Enter any character
x
Given character is x
and it is an alphabet
NESTED IF-ELSE
• When an if-else construct appears The same example with braces
as a statement in another if-else,
it is known as a nested if-else
for clearer understanding:
if(expression_1)
construct. For example, {
if(expression_1) if(expression_2)
{
if(expression_2) if(expression_3)
if(expression_3) {
statement_1;
{ }
statement_1; else
{
} statement_2;
else }
}
{ else
statement_2; {
statement_3;
} }
else }
{
statement_3;
}
NESTED IF-ELSE…
Nested if-else flow
chart
true expr_ false
1

true expr_ false stmt_4


2

true expr_ false stmt_3


3

stmt_1 stmt_2
SELECTING LARGEST OF THREE
VALUES
#include<stdio.h>
main()
{
float A, B, C; OUTPUT:
printf("Enter three values\n"); Enter three values
scanf("%f %f %f", &A, &B, &C); 23 67 88
printf("\nLargest Value is ");
if(A>B)
{ Largest Value is
if (A>C) 88.000000
printf("%f\n",A);
else
printf("%f\n",C);
}
else
{
if (C>B)
printf("%f\n",C);
else
printf("%f\n",B);
}
}
THE ELSE IF LADDER
 Multi-path decision making
 It is a chain of if ’s in which the statement
associated with each else is an if
if (condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement-3;
---------------
else if(condition n)
statement-n;
else
default-statement;
statement-x;
FLOW CHART – ELSE..IF LADDER
EXAMPLE
#include<stdio.h>
main()
{
int marks;
Char grade[10];
scanf(“%d”,&marks);
if (marks > 80)
grade = "Honours";
else if (marks > 70)
grade = "first Division";
else if (marks > 60)
grade = "Second Division";
else if (marks > 50)
grade = "Third Division";
else
grade = "Fail";
printf("%s\n", grade);
}
/* PROGRAM TO CHECK A CHAR AN ALPHABET OR
DIGIT */
#include <stdio.h>
main( ) SAMPLE INPUT AND OUTPUT
{ Enter any character
C
char ch; C is an uppercase letter
printf("Enter any character \n");
ch = getchar();
if(ch >= 'A' && ch <= 'Z')
printf("%c is an uppercase letter \n",ch);
else if(ch >= 'a' && ch <= 'z')
printf("%c is an lowercase letter \n",ch);
else if(ch >= '0' && ch <= '9')
printf("%c is a digit\n",ch);
else if(ch == '\n' || ch == '\t' || ch == ' ')
printf("It is a whitespace character \n");
else
printf("%c is a special character \n");
}
CONDITIONAL EXPRESSION

 The ternary operator ? : is used to form a


conditional expression.
 It uses three operands and hence it is called as a
ternary operator.
 Format: expression-1 ? expression-2 : expression-3 ;
 Parentheses are optional for expression_1 since the
precedence of ternary operator is very low.
CONDITIONAL EXPRESSION…

• Evaluation is as follows:
– The expression_1 is
evaluated to find the logical true expression_1 false
value true (non-zero) or
false (zero).
– If the expression_1 is true,
expression_2 expression_3
then the expression_2 is
evaluated and that is the
resulting value of the
conditional expression.
– If the expression_1 is false, Flow chart representation of conditional expression
then the expression_3 is
evaluated and that is the
resulting value of the
conditional expression.
THE SWITCH STATEMENT
Format:

 The switch - case construct switch(expression)


{
is an alternative to if - else case constant_1 :
or else if construct. {
statement ;
 The constants following the }
break ;

cases are known as case case constant_2 :


{
labels. statement ;
break ;
 A colon ( : ) must be placed at }
the end of each case label .
.
and default. .
case constant_n :
 The braces following the {
statement ;
labels are optional. break ;
}
default :
{
statement ;
break ;
}
}
SWITCH-CASE CONSTRUCT…
 The expression must be an integer or character
expression yielding an integer value.

 The expression is evaluated first and its value is


then matched against the case labels.

 When a match is found, the statement following


that case will be executed.

 If there is no match with any of the case labels,


the statement following the default will be
executed.
SWITCH-CASE CONSTRUCT…

 The default part is optional.


 If the default part is omitted and the integer value
of the expression does not match with any one of
the case labels, then no statement within the
switch - case construct will be executed.
 If a break statement is not included in each case
statement, whenever a match is found, the program
executes the statements following that case and also
all the subsequent case statements and default
statements.
SWITCH-CASE CONSTRUCT
ILLUSTRATION

switch(expr)

expr==cons true
case_1 block
t1

false

expr==cons true
case_2 block
t1

false

expr==cons true
case_3 block
t1

false

expr==cons true
case_n block
t1

false
default
default block
/*PROGRAM TO PRINT THE DIGITS IN WORDS */

main( )
{ int digit;
printf("Enter a single digit number \n");
scanf("%d",&digit);
switch(digit)
{
case 0: printf("The number entered is ZERO \n");
break;
case 1: printf("The number entered is ONE \n");
break;
case 2: printf("The number entered is TWO \n");
break;
case 3: printf("The number entered is THREE \n");
break;
case 4: printf("The number entered is FOUR \n");
break;
case 5: printf("The number entered is FIVE \n");
break;
case 6: printf("The number entered is SIX \n");
break;
SAMPLE INPUT AND
case 7: printf("The number entered is SEVEN \n");
OUTPUT
break; Enter a single digit number
case 8: printf("The number entered is EIGHT \n");
7
break; The number entered is SEVEN
case 9: printf("The number entered is NINE \n");
break;
}
}
printf("\n\nEnter your
#include<stdio.h>
choice:");
void main()
scanf("%d",&ch);
{
switch(ch)
float a,b,res;
{
int ch;
case 1:res=a+b;
printf("Enter the value of A:");
break;
scanf("%f",&a);
case 2:res=a-b;
printf("Enter the value of B(B<>0):");
break;
scanf("%f",&b);
case 3:res=a*b;
printf("\n\nMathematical
break;
Operations");
case 4:res=a/b;
printf("\n\t1->Add");
break;
printf("\n\t2->Subtract");
}
printf("\n\t3->Multiply");
printf("Answer =%.2f",res);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy