[go: up one dir, main page]

0% found this document useful (0 votes)
53 views8 pages

Unit 3 Decision Making and Branching

This document discusses various conditional control statements in C programming for decision making and branching. It describes IF, IF-ELSE, nested IF, IF-ELSE ladder, SWITCH statements. IF statement executes code conditionally based on a boolean expression. IF-ELSE provides two-way branching based on a condition. Nested IF allows multiple conditional checks. IF-ELSE ladder tests conditions sequentially. SWITCH checks a variable against case values and executes the corresponding code block.

Uploaded by

shubhamverma2055
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)
53 views8 pages

Unit 3 Decision Making and Branching

This document discusses various conditional control statements in C programming for decision making and branching. It describes IF, IF-ELSE, nested IF, IF-ELSE ladder, SWITCH statements. IF statement executes code conditionally based on a boolean expression. IF-ELSE provides two-way branching based on a condition. Nested IF allows multiple conditional checks. IF-ELSE ladder tests conditions sequentially. SWITCH checks a variable against case values and executes the corresponding code block.

Uploaded by

shubhamverma2055
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/ 8

UNIT 3: DECISION MAKING, BRANCHING

Unit 3: Decision Making and Branching


Topic Contents:

Decision Making and Branching

• Decision Making with IF Statement • IF..ELSE Ladder


• IF… ELSE Statement • Switch Statement
• Nested IF Statement • Goto Statement

Normally the statements in a program are execute in the order in which they appear in the program.
This type of execution is called a sequential execution.

In some situations, the order of execution of instructions may have to be changed depending on
certain conditions. This involves a kind of decision making (i.e., logical test to be conducted) to see
whether a particular condition has occurred or not and then direct the computer to execute certain
instructions accordingly. This is called conditional execution. The result of the conditional execution
is a Boolean value (i.e., either true or false). Conditional execution involves both decision making and
branching.

C provides a variety of conditional control statements such as

1. if statement
2. if-else statement
3. nested-if statement
4. if-else ladder
5. switch statement

if statement
This is used to execute statement(s) conditionally. It is a simple if statement and is also known as one-
way branching. Here the logical condition is tested to either true or false. The syntax is
if(boolean_expression) {

/* statement(s) will execute if the boolean expression is true */

If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be
executed. If the Boolean expression evaluates to false, then the first set of code after the end of the
'if' statement (after the closing curly brace) will be executed.

PROF. SUSHANT SUNDIKAR 1


UNIT 3: DECISION MAKING, BRANCHING

Flow chart:

Example :
if(category == “sports”)
{
marks = marks + bonus marks;
}
printf(“%d”,marks);

Explanation: If the student belongs to the sports category, then additional bonus marks are added to
his marks before they are printed. For other bonus marks are not added.

If-else statement
The if statement is used to execute only on action. If there are two statements to be executed
alternatively, then if-else statement is used. The if-else statement is a two way branching. The syntax
of if-else statement is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}

If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else
block will be executed.

C programming language assumes any non-zero and non-null values as true, and if it is either zero or
null, then it is assumed as false value.

PROF. SUSHANT SUNDIKAR 2


UNIT 3: DECISION MAKING, BRANCHING

Flow chart:

Example :
if (code == 1)
{
boy = boy + 1;
}
else
{
girl = girl + 1;
}
printf (“no of boys is %d and girls is %d”, boy, girl);

Here if the code is equal to ‘1’ the statement boy=boy+1; is executed and the control is transferred
to the printf statement, after skipping the else part. If code is not equal to ‘1’ the statement boy
=boy+1; is skipped and the statement in the else part girl =girl+1; is executed before the
control reaches the printf statement.

Nested If statement
When a series of decisions are involved we may have to use more than one if-else statement in nested
form of follows. Nesting means you can use one if or else if statement inside another if or else if
statement(s). The syntax for a nested if statement is
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else
{
/* Executes when the boolean expression 2 is false */

}
}

PROF. SUSHANT SUNDIKAR 3


UNIT 3: DECISION MAKING, BRANCHING

else
{
/* Executes when the boolean expression 1 is false */
}

St-3

If the condition is false the st-3 will be executed otherwise it continues to perform the nested If –else
structure (inner part ). If the condition 2 is true the st-1 will be executed otherwise the st-2 will be
evaluated and then the control is transferred to the st-x

Example:
/* local variable definition */
int a = 100;
int b = 200;

/* check the boolean condition */


if( a == 100 ) {

/* if condition is true then check the following */


if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
Here if a is equals to 100 then the condition a==100 will return true and the execution will enter the
if block. Now another condition is checked inside the if block. Here if b==200 is true then the Value
of a is 100 and b is 200 will be printed.

if else ladder
The else if ladder is an another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if.

The if else ladder statement in C programming language is used to test set of conditions in sequence.
If any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.

PROF. SUSHANT SUNDIKAR 4


UNIT 3: DECISION MAKING, BRANCHING

Syntax:
if(condition_expression_One) {
statement1;
} else if (condition_expression_Two) {
statement2;
} else if (condition_expression_Three) {
statement3;
} else {
statement4;
}

• First of all condition_expression_One is tested and if it is true then statement1 will be


executed and control comes out out of whole if else ladder.
• If condition_expression_One is false then only condition_expression_Two is tested. Control
will keep on flowing downward, If none of the conditional expression is true.
• The last else is the default block of code which will gets executed if none of the conditional
expression is true.

PROF. SUSHANT SUNDIKAR 5


UNIT 3: DECISION MAKING, BRANCHING

/* Program to check if a entered number is positive, negative or


zero. */

#include<stdio.h>
void main ()
{
int num = 10 ;
if ( num > 0 )
printf ("\n Number is Positive");
else if ( num < 0 )
printf ("\n Number is Negative");
else
printf ("\n Number is Zero");
}

switch Statement
The if..else..if ladder allows you to execute a block code among many alternatives. If you are
checking on the value of a single variable in if...else...if, it is better to use switch statement.
The switch statement is a multiway branching statement that tests the value of a given variable against
a list of case values and when a match is found, a block of statements associated with that case is
executed. The general form of switch statement is:

Syntax:

switch(expression)
{
case value1 :
statements;
break;
case value2 :
statements;
break;
:
:
case valuen :
statements;
break;
default :
statements;
break;
}

PROF. SUSHANT SUNDIKAR 6


UNIT 3: DECISION MAKING, BRANCHING

• The expression is an integer expression or characters.


• You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
• The data type of expression and case value must be same.
• When the expression is matched with any case value, then the statements following that case
will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of control jumps to
the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
• The default is an optional case which must appear at the end of the switch. It will be executed
if the value of the expression does not match with any of the case values.

Flow Diagram

Program:
#include <stdio.h>

int main () {
char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade );

return 0;

PROF. SUSHANT SUNDIKAR 7


UNIT 3: DECISION MAKING, BRANCHING

goto statement

C supports an unconditional control statement, goto that provides an unconditional jump from the
'goto' to a labeled statement in a C program. The syntax of goto statement is as follows:

goto label;
………
label:

Here label can be any plain text except C keyword and it can be set anywhere in the C program above
or below to goto statement. The label can be placed any where in the C program either before or after
the goto statement.

goto label; label:


... .. ... statement;
... .. ... ... .. ...
... .. ... ... .. ...
label: ... .. ...
statement; goto label;
(a) (b)

NOTE − Use of goto statement is highly discouraged in any programming language because it may
create an infinite loop.
Example: The following code checks whether an entered number is even or odd
if (num % 2 == 0)
goto even; // jump to even
else
goto odd; // jump to odd
even:
printf(“%d is an even number”,num); return 0;
odd:
printf(“%d is an odd number”,num); return 0;

PROF. SUSHANT SUNDIKAR 8

You might also like