0% found this document useful (0 votes)
6 views

Lecture 3 Decision Control Structure

The document discusses decision control structures in C programming, focusing on how to execute different sets of instructions based on conditions using constructs like if statements, if-else statements, and conditional operators. It explains the syntax and usage of these control structures, along with examples and problems to illustrate their application. Additionally, it highlights the limitations of conditional operators compared to traditional if-else statements.

Uploaded by

tybaclothing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 3 Decision Control Structure

The document discusses decision control structures in C programming, focusing on how to execute different sets of instructions based on conditions using constructs like if statements, if-else statements, and conditional operators. It explains the syntax and usage of these control structures, along with examples and problems to illustrate their application. Additionally, it highlights the limitations of conditional operators compared to traditional if-else statements.

Uploaded by

tybaclothing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Decision Control Structure

Dr.Rab Nawaz Jadoon


Department of Computer Science Assistant Professor
COMSATS University, Abbottabad
DCS Pakistan
COMSATS Univeresity, Islamabad
(Abbottabad Campus)

Programming Fundamental
Decision control structure
 Before this we have used sequence control
structure in which,
 Various steps are executed sequentially, i.e. in the
same order in which they appear in the program.
 By default the instructions in a program are executed
sequentially.

Department of Computer Science 2


Decision control structure

 Many a times, we want a set of instructions to


be executed in one situation, and an entirely
different set of instructions to be executed in
another situation.
 This kind of situation is dealt in C programs using a
decision control instruction.

Department of Computer Science 3


Decision control structure

 Decision control instruction can be implemented


in C using:
 The if statement
 The if-else statement
 The conditional operators

Department of Computer Science 4


The If Statement

 Like most languages, C uses the keyword if to


implement the decision control instruction.
 The general form of if statement looks like this:
if ( this condition is true )
execute this statement ;
 The condition following the keyword if is always enclosed
within a pair of parentheses.
 If the condition, whatever it is, is true, then the statement is
executed.
 If the condition is not true then the statement is not
executed; instead the program skips past it.

Department of Computer Science 5


Single and double control structure

Department of Computer Science 6


The If Statement
 We express a condition using C’s ‘relational’
operators.
 The relational operators allow us to compare two
values to see whether they are equal to each other,
unequal, or whether one is greater than the other.

Department of Computer Science 7


Simple Program using If statement

Department of Computer Science 8


If Control Structure

Department of Computer Science 9


Problem
 While purchasing certain items, a discount of 10% is offered if the
quantity purchased is more than 1000. If quantity and price per item are
input through the keyboard, write a program to calculate the total
expenses???

Department of Computer Science 10


Solution (Flow Chart & Program)

Department of Computer Science 11


Multiple statements within If
 More than one statement to be executed if the
expression following if is satisfied.
 If such multiple statements are to be executed then
they must be placed within a pair of braces.
 For example,
If(condition)
{
---------
---------
}

Department of Computer Science 12


Program
Flow chart Program

Department of Computer Science 13


Quiz
#include<stdio.h>
#include<conio.h> What would be the output???
main( )
{ Nothing
int a = 300, b, c ;
if ( a >= 400 ) What would be the output, if a=500???
{
b = 300 ; 300 200
c = 200 ;
printf ( "\n %d %d", b, c ) ;
}
getche ();
}

Department of Computer Science 14


The if-else Structure

 Execute one group of statements if the


expression evaluates to true and another group
of statements if the expression evaluates to
false.
 This is done using the if-else control structure.
 For example,
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”

Department of Computer Science 15


If-else Example

Department of Computer Science 16


Program illustrating if-else

Department of Computer Science 17


Quiz ???
 What would be the output of the following
program???

x and y are equal


Department of Computer Science 18
Quiz ???
 What would be the output of the following
program???

C is WOW

Department of Computer Science 19


Quiz ???
 Identify the error from the following

}
No Error, a and b are equal

No Error

No Error

Department of Computer Science 20


Identify the errors

No Error

Department of Computer Science 21


Forms of if

Department of Computer Science 22


Use of logical operators

 C allows usage of three logical operators,


namely, &&, || and !.
 These are to be read as ‘AND’, ‘OR’ and ‘NOT’
respectively.
 Two of them are composed of double symbols:
 || and &&.
 Don’t use the single symbol | and &. These single symbols
also have a meaning. They are bitwise operators.
 The first two operators, && and ||, allow two or more
conditions to be combined in an if statement.

Department of Computer Science 23


Working of three logical operators

Department of Computer Science 24


Example

 The marks obtained by a student in 5 different


subjects are input through the keyboard.
 The student gets a division as per the following
rules:
 Percentage above or equal to 60 - First division
 Percentage between 50 and 59 - Second division
 Percentage between 40 and 49 - Third division
 Percentage less than 40 - Fail
 Write a program to calculate the division obtained by
the student.

Department of Computer Science 25


Solution (Method-1)

Department of Computer Science 26


Problems faced in the program
 This is a straight forward program. Observe that
the program uses nested if-elses.
 This leads to three disadvantages:
 As the number of conditions go on increasing the level of
indentation also goes on increasing. As a result the whole
program creeps to the right.
 Care needs to be exercised to match the corresponding if’s
and elses.
 Care needs to be exercised to match the corresponding pair
of braces.

 Note: All these three problems can be eliminated by usage of ‘Logical


operators’. (Method 2)

Department of Computer Science 27


Solution (Method-2)

Department of Computer Science 28


The Else if Clause (Method -3)
 There is one more way in which we can write
the previous program,

Department of Computer Science 29


The Else if Clause (Method -3)
 Note that the else if clause is nothing different.
 It is just a way of rearranging the else with the if that
follows it.
 This would be evident if you look at the following
code:

Department of Computer Science 30


The Else if Clause

 Problem
 A company insures its drivers in the following cases:
 If the driver is married.
 If the driver is unmarried, male & above 30 years of age.
 If the driver is unmarried, female & above 25 years of age.
 In all other cases the driver is not insured. If the
marital status, sex and age of the driver are the
inputs.
 Write a program to determine whether the driver is
to be insured or not?

Department of Computer Science 31


Solution-1

Department of Computer Science 32


Solution-2

Department of Computer Science 33


Problem…

 Write a program to calculate the salary as per


the following table:

Department of Computer Science 34


Solution

Department of Computer Science 35


Cont…

Department of Computer Science 36


The ! Operator

 This operator reverses the result of the


expression it operates on.
 For example, if the expression evaluates to a non-
zero value, then applying ! operator to it results into
a 0 and vice versa.
 If the expression evaluates to zero then on applying !
operator to it makes it 1, a non-zero value. The final
result (after applying !) 0 or 1 is considered to be
false or true respectively.
 For Example ! ( y < 10 ), y is less than 10, the ! Operator
returns false.

Department of Computer Science 37


The ! Operator

 The NOT operator is often used to reverse the


logical value of a single variable, as in the
expression
if ( ! flag )

Department of Computer Science 38


Identify the error from the
following???

Department of Computer Science 39


Conditional Operator
 The conditional operators ? and : are sometimes
called ternary operators since they take three
arguments.
 They form a kind of foreshortened if-then-else.
 Their general form is,
expression 1 ? expression 2 : expression 3
 What this expression says is:
 “If expression 1 is true (that is, if its value is non-zero), then
the value returned will be expression 2, otherwise the value
returned will be expression 3”.

Department of Computer Science 40


Examples
 The following program store 3 in y, if x is
greater than 5, otherwise it will store 4 in y.

 The comparison with if else is as under,

Equivalent to

Department of Computer Science 41


Example

 1 would be assigned to y if a >=65 && a <=90


evaluates to true, otherwise 0 would be assigned

Department of Computer Science 42


Important point
 It’s not necessary that the conditional operators
should be used only in arithmetic statements. This is
illustrated in the following examples:

Department of Computer Science 43


Cont…
 The conditional operators can be nested as
shown below.

 Check out the following conditional expression:


a>b?g=a:g=b;

Department of Computer Science 44


Limitation

 The limitation of the conditional operators is


that after the ? Or after the : only one C
statement can occur.
 In practice rarely is this the requirement.
 Therefore, in serious C programming conditional
operators aren’t as frequently used as the if-else.

Department of Computer Science 45


Limitations

 The limitation of the conditional operators is


that after the ? or after the : only one C
statement can occur.
 Therefore, in serious C programming conditional
operators aren’t as frequently used as the if-
else.

Department of Computer Science 46


Department of Computer Science 47

You might also like