Sei sulla pagina 1di 3

03/10/2017

Basic Constructs of an Algorithm

Programming Fundamentals  Sequence


 Selection
 Iteration / repetition
Lecture 05, 06 Choices and Decisions

Programming Fundamentals- Choices & Decisions 2

Choices and Decisions Comparing Data Values


 To make a choice or decision we have to  We can compare data values using some
compare different values operators which are called “Relational
 For example: Operators ”
 If traffic signal is red then stop the car < less than <= less than or equal to
 Here we are comparing the color of the signal and > greater than >= greater than or equal to
then making the decision of stopping the car == equal to != not equal to

Programming Fundamentals- Choices & Decisions Programming Fundamentals- Choices & Decisions 4
3

Comparing Data Values The if Statement


 Each of these binary operators compare two values  In a basic if statement, a statement is executed and
and in result produce true or false the condition is checked weather the result is true or
 true and false are keywords in C++ false

  If the result is true then the block of statement(s) is


They are Boolean literals and their type is bool (after
George Bool- the father of boolean algebra) executed otherwise it is not executed
 bool decision = i > j ;

Programming Fundamentals- Choices & Decisions 5 Programming Fundamentals- Choices & Decisions 6

1
03/10/2017

The if Statement - Flow Chart Exercise 01


No Condition  Write a program that reads the marks of PF
is true?
and ITC and tests whether marks in PF are
Yes greater than marks in ITC. If so display the
Statement or marks of PF
Block of
Statements

Next Statement

Programming Fundamentals- Choices & Decisions 7 Programming Fundamentals- Choices & Decisions
8

The if Statement The if else Statement

No Condition
 If – else statements is true?

Yes
Statement or
 Nested if statements Block of Statement or
Statements for Block of
false Statements
 Nested if – else statements
Next Statement
The Dangling Else Problem

Programming Fundamentals- Choices & Decisions 9 Programming Fundamentals- Choices & Decisions 10

Exercise 02 Nested if statements



 Write a program that reads the marks of PF Statement that is to be executed when condition in an if

& ITC and displays the greater of two statement is true can itself be an if statement
 The condition in the inner if is only tested if the condition for
the outer if is true
 An if that is nested inside another can also contain a nested
if
 We can continue nesting ifs, one inside another to whatever
level we want

Programming Fundamentals- Choices & Decisions 11 Programming Fundamentals- Choices & Decisions 12

2
03/10/2017

Nested if else statements Character Handling


 In our nested if program we write a lengthy code to check
 Like nested if, we can nest if-else statements a letter case (i.e. Upper or Lower case)
 As discussed earlier standard library provides a wide
within ifs, ifs within if-else statements, and of
range of functions, that we can use in our program
course, if-else statements within other if-else  We won’t write our own functions yet but we will discuss
statement how to use functions
 We call a function with its parameters and it returns a
result
 Generally a call to a function looks like;
 functionName(argument1, argument2,…)

Programming Fundamentals- Choices & Decisions 13 Programming Fundamentals- Choices & Decisions 14

Character Handling List of Functions


 isupper() Tests for an upper case letter, ‘A’ to ‘Z’
 By including the header file cctype in our program
 islower() Tests for an upper case letter, ‘a’ to ‘z’
we can make use of extremely useful functions for
 isalpha() Tests for an upper & lower case letter
testing characters
 isdigit() Tests for a digit, 0 to 9
 Each of these functions return a value of type int. the
 isspace() Tests for a space, a new line, tabs
value will be positive (true) if the character is of type
Why each of the functions don’t return a value of type bool
being tested and 0(false) if it is not (i.e. true or false)?

These functions were part of standard library before type


bool was introduced to C++

Programming Fundamentals- Choices & Decisions 15 Programming Fundamentals- Choices & Decisions 16

Exercise 03 Exercise 04
 Write a program for the following stateme  Write a program for the following stateme
nt nt

Read a character from user and if is an alphab Read a character from user
et or a digit, display the message “you entered if it is an alphabet then check if is in uppercase,
an alphabet” or “you entered a digit” other wis convert it to lowercase
e display “you did not enter an alphabet or digi if it is in lowercase then convert it into uppercas
e.
t”
Finally display the converted alphabet.

Programming Fundamentals- Choices & Decisions


Also, display the message “you did not entered an al
17 Programming Fundamentals- Choices & Decisions 18
phabet” if it is not an alphabet

Potrebbero piacerti anche