0% found this document useful (0 votes)
15 views41 pages

Ch-11 (ICS II) - Decision Constructs

The document covers decision constructs in C programming, including relational and logical operators, control structures, and various types of conditional statements such as 'if', 'if-else', 'nested if', 'if-else-if', and 'switch'. It explains how these constructs are used to control the flow of execution in programs based on conditions, providing examples and limitations of each construct. Additionally, it includes assignments for practical application of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views41 pages

Ch-11 (ICS II) - Decision Constructs

The document covers decision constructs in C programming, including relational and logical operators, control structures, and various types of conditional statements such as 'if', 'if-else', 'nested if', 'if-else-if', and 'switch'. It explains how these constructs are used to control the flow of execution in programs based on conditions, providing examples and limitations of each construct. Additionally, it includes assignments for practical application of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

COMPUTER SCIENCE – 12

Chapter 11
Decision Constructs
Contents
• Relational Operator & Relational Expression
• Logical Operator & Logical Expression
• Control Structure
• The ‘if’ statement
• The ‘if-else’ Statement
• The Nested ‘if’ Statement
• The ‘if-else-if’ Statement
• The switch Statement
Relational Operator &
Relational Expression
Q.1Define relational operator. How many relational operators are available in C?
Also describe the relational expression.
Relational Operator
• Relational operator  symbol used to compare two values of same type
• Result of comparison  True (or 1) or False (or 0)
• Used to specify conditions in programs  Comparison operators

• C-language provides six basic


relational operators
• Suppose
• ‘a’ = 10
• ‘b’ = 5
• ‘c’ = 10
Relational Operator
• Compare numeric quantities (integer and floating-point) and single character
values
• Values may be constant values or variables or result of an expression
• Type of values on both sides of operator must be same data type,
• otherwise an error may occur and program execution may terminate
• In C, strings are compared using string function such as strcmp() function
Relational Expression
• An expression that contains two operands and one relational operator
between these operands
• Returns true (or 1) or false (or 0)
• Relational expression is used in conditional statement (or looping
statements) as test condition
• In C, true can be represented by any nonzero value while false can be
represented by zero value
• Suppose
• a=6
• b=9
• c=6
Logical Operator & Logical
Expression
Q.2What are logical operators? Discuss different logical operators available in C
language. Also describe the logical expression with examples.
Logical Operator
• Used to form more complex and useful logical conditions
• Two or more relational expressions are combined using logical operators
• AND Operator (&&)
• Denoted by && (double ampersand signs)
• Used to combine two relational expressions
• Returns True if both expressions are True, and returns False if any one of the expressions is False
• OR Operator (||)
• Denoted by || (double pipe signs)
• Used to combine two relational expressions
• Returns True if either of the expressions is True, and returns False if both relational expressions are False
• NOT Operator (!)
• Denoted by ! Sign
• Used to reverse the result of the relational expression or condition
• Returns True if condition is False and returns False if condition is True
Logical Expression
• A combination of relational expressions (or simple operands) joined together
by logical operators
• Compound expression
• Returns true (or 1) or false (or 0)
• Suppose a = 10, b = 5 and c = 10, then
• Examples
• Integer Variables  ‘total’, ‘math’, ‘eng’ and ‘computer’
total >=700 && computer>90
total > 600 || eng > 70
!(computer > 33)
Control Structure
Q.3What is a control structure? Also describe the basic control structures in C.
Control Structure
• When a computer program is executed, execution control moves from one
statement to next statement to take action on program statements
• C-language provides features to control the flow of execution in a program
• Control structures  Statements that are used to control the flow of execution
in a program
Sequence Structure
• Statements of a program are executed in same order in which they are written
• Statements are executed one after the other
• All statements of program are executed
Control Structure
Selection Structure
• Used for selecting a statement or group of statements for execution on basis of a
given condition
• Decision structure
• Instructions of program are written in a sequence but selection structures are used
for making decisions
• Given condition is tested
• If true statement(s) that follow selection structure, are executed
• Otherwise ignored and alternative path is followed
• Selection structures in C
 The if statement
 The ifelse statement

Control Structure
Repetition Structure
• Used to execute a statement or a set of statements repeatedly as long as given
condition remains true
• Interaction structure or loop structure
• Repetition or loop structures provided by C-language
i) while loop
ii) do-while loop
iii) for loop
The ‘if’ statement
Q.4What is if statement? Explain its working with flowchart.
Q.5What is the limitation of simple if statement? Explain with program example.
The ‘if’ statement
• Simplest form of selection structure  decision making statement
• Used to execute or to ignore a statement or a set of statements after testing a
condition
• Evaluates condition
• If true  statement or a set of statements following ‘if’ statement is executed
• If false statement or a set of statements following ‘if’ statement is ignored and control
transfers to statement that comes after ‘if’ structure
• Set of statements following ‘if’
statement are written within curly
braces { }
• Compound statement
The ‘if’ statement
The ‘if’ statement
Limitation of ‘if’ statement
• Its use is limited in programs
• Executes a statement or set of statements if given condition is true
• If given condition is false  no alternate action is performed
• Cannot be used for making two-way decisions
• To execute one set of statements if given condition is true and other set of statements if
given condition is false  cannot handle using simple ‘if’ statement
• Can be handled using sequence of simple ‘if’ statements  not a proper way
• If first ‘if’ statement is omitted and user enters a negative
number  condition will return false
• No message will be displayed on screen
• There should be proper response from program
• Solution  ‘if-else’ structure can be used to handle this
kind of situation
The ‘if-else’ Statement
Q.6 Explain the ‘if-else’ statement with flowchart.
OR Write a comprehensive note on ‘if-else’ statement.
Q.7 What is the conditional operator? Explain with program example.
The ‘if-else’ Statement
• Another form of ‘if’ statement  for making two-way decisions
• One condition and two blocks of statements are given
• If condition true  executes one block of statements
• If condition is false  other block of statements is executed
• In any situation, one block of statements is executed and other is skipped
• May contain a single statement (without curly brackets) on one side and
compound statement on another side
The ‘if-else’ Statement
The ‘if-else’ Statement
The Conditional Operator
• Used for making two-way decisions
• Used in place of simple ‘if-else’ structure
• Denoted by ? and : signs
• Three operands are used with conditional operator  Ternary operator
• General syntax  (condition) ? exp1 : exp2
• Both “exp1” & “exp2” can be arithmetic expressions or constant values or variables
• Other statements of C such as input and output statements can also be used in place of
exp1& exp2
• Example  (n%2==0)? printf(“Even number”): printf(“Odd number”);
The ‘if-else’ Statement
The Conditional Operator
The Nested ‘if’ Statement
Q.8What is nested if statement? Explain its working with example.
Q.9Compare nested ‘if’ with sequence of ‘if’ statements? Explain with example.
The Nested ‘if’ Statement
• An ‘if’ statement within another ‘if’ statement
• Outer ‘if’ statement  ‘if’ statement that contains another ‘if’ statement
• Inner ‘if’ statement  ‘if’ statement used inside outer ‘if’ statement
• Used when multiple conditions have to check
• Nesting can be up to any level
• Increases the complicity of the logic of program
The Nested ‘if’ Statement
The Nested ‘if’ Statement
Nested ‘if’ With Sequence of ‘if’ Statements
• May contain multiple ‘if’ statements that may be nested up to any level
• Some ‘if’ statements are executed while other may be skipped
• All ‘if’ statements are executed in an order  No ‘if’ statement is skipped
• Three ‘if’ statements are given in a sequence
• If user enters a positive number number is tested with
first ‘if’ statement
• True  Message displayed
• Other two ‘if’ statements will also be executed
• Although answers will be false
• No need to execute these ‘if’ statements  should be
skipped
• Otherwise processor has to consume extra time without
any purpose
• Solution  ‘if-else-if’ structure
The ‘if-else-if’ Statement
Q.10 Explain the ‘if-else-if’ statement or multiple if-else statement. Explain its
working.
The ‘if-else-if’ Statement
• Multiple ‘if-else’ statement or nested ‘if-else’ statement
• Execute one block of statements from multiple blocks of statements
• Multiple conditions and multiple blocks of statements are given
• When any given condition is true
• Statements associated with that condition are executed
• All other blocks of statements are ignored
The ‘if-else-if’ Statement
The ‘if-else-if’ Statement
The switch Statement
Q.11 What is switch statement? Explain its working with example. Also
describe ‘break’ statement.
The switch Statement
• Used as an alternative of ‘if-else-if’ structure
• Used when multiple choices are given and one choice is to be selected
• Used for menu selections
• Contains only one expression at its beginning and multiple cases within its body
• Each case contains a set of statements
The switch Statement

The ‘break’ Statement


• Used to exit from the body of the ‘switch’ structure
(or the loop structure)
• Normally used at the end of statements in each
case
• If all ‘break’ statements are omitted from ‘switch’
structure, then statements of all remaining cases
that come after matched case are also executed
Difference Between ‘switch’ and Nested ‘if-else’
Structures
The ‘switch’ Structure Nested ‘if-else’ Structure
• Easy to use and to understand • Complicated  difficult to use and to
• Uses single expression which understand
returns a single integer value or a • Uses multiple expressions and each
single character returns a true or false value
• Can be used for only simple menu • Can be used for complicated
selections multiple choices
• Fast because computer evaluates • Very slow because computer
only one expression evaluates more than one expression
The switch Statement
The switch Statement
Assignment
• Write a program that inputs three numbers and displays maximum number using simple if statement.
• Write a program that inputs two numbers and finds whether these numbers are equal or different using if-
else structure.
• Write a program that inputs a number and displays the square root of the number if number is positive.
Otherwise program displays a message “Invalid input”.
• Write a program that inputs a character and displays whether it is vowel or consonant using ‘if-else’
statement and OR operator.
• Write a program that inputs a year and finds whether it is a leap year or not using if-else structure.
• A year is a leap year if it is divisible by 4, except that any year divisible by 100 is a leap year only if it is
divisible by 400. Write a program that inputs a year such as 1996, 1800, and 2010, and displays “Leap year” if
it is a leap year, otherwise displays “Not a leap year”.
• Write a program that inputs a number and displays whether the number is divisible by 3 or not using
conditional operator.
• Write a program that inputs three numbers and displays whether all these numbers are equal or different
using ‘nested if statement’.
• Write a program that inputs the radius of a circle. It calculates area of circle if user enters 1 as choice. It
calculates circumference if the user enters 2 as choice.
Assignment
• Write an interactive program that contains an if statement that may be used to compute the area of a square
(area = side2) or a triangle (area = ½ × base × height) after prompting the user to type the first character of
the figure name (S or T).
• Write a program that inputs salary. If the salary is 30000 or more, it deducts 13% of the salary. If the salary is
15000 or more but less than 30000, it deducts 1500 from the salary.
• Write a program that inputs three numbers and finds out the maximum number using logical operators and
‘if-else-if’ structure.
• Write a program that inputs a character and determines whether it is a capital letter, small letter, a digit, or a
special symbol.
• Write a program that calculates the electricity bill. The electricity bill is calculated as follows:
 If the units consumed are <= 300, then the cost is Rs. 5 per unit.
 If the units consumed are > 300 and <= 500, then the cost is Rs.12 per unit.
 If the units consumed exceed 500, then the cost is Rs. 16 per unit.
 A line rent Rs. 250 is also added to the bill.
 If the bill exceeds Rs. 4000, then surcharge of 5% extra is added to the bill.
• Write a program that inputs a digit and displays its value in words using switch statement. For example, if a user enters
digit 3, it displays “Three”.
• Write a program that inputs two numbers and one arithmetic operator. It displays the result of arithmetic operation on
For more details, and solved assignment refers to

PM Series

Computer Science
ICS Part-II

by
CM Aslam, Aqsa Aslam, Abdur Rehman &
Mudassir Saleem

Publisher: Majeed Sons


22- Urdu Bazar, Lahore

You might also like