chapter 4

Click here to load reader

Upload: jacob-ortiz

Post on 24-Nov-2015

9 views

Category:

Documents


2 download

TRANSCRIPT

Computer Programming & Application (KJM463)

CHAPTER 4

SELECTION STRUCTURE 1ContentsIntroductionSelection criteria with Boolean expressionThe if statementThe if..else statementThe if..else if statementNested if statementThe switch statement

CSC425 - SEPTEMBER 20132IntroductionA computer can proceed:In sequenceSelectively (branch) - making a choiceRepetitively (iteratively) - loopingSome statements are executed only if certain conditions are met A condition is represented by a logical (Boolean) expression that can be true or falseA condition is met if it evaluates to true

CSC425 - SEPTEMBER 2013IntroductionFlow of Control: the order in which a programs statements are executedNormal flow is sequential Selection and Repetition Statements allow programmer to alter normal flowSelection: selects a particular statement to be executed nextSelection is from a well-defined setRepetition: allows a set of statements to be repeatedCSC425 - SEPTEMBER 2013Introduction

CSC425 - SEPTEMBER 2013Introduction

CSC425 - SEPTEMBER 2013SelectionSelection criteria with Boolean expressionBoolean expression is a sequence of operands and operators that combine to produce one of the Boolean values, true or false.2 types of Boolean expression :-simple Boolean expression-compound Boolean expression

CSC425 - SEPTEMBER 2013Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013Simple Boolean expression :is of the form

expression1 relational-operator expression2

Relational operators: Allow comparisonsRequire two operandsReturn 1 if expression is true, 0 otherwise

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013All computers are able to compare numbersCan be used to create an intelligence-like facilityRelational Expressions: expressions used to compare operandsFormat: a relational operator connecting two variable and/or constant operandsExamples of valid relational expressions:

Age > 40 length 3.3 is always false, therefore the expression has a value of 0

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013Comparing string Types :Relational operators can be applied to stringsStrings are compared character by character, starting with the first characterSuppose we have the following declarations:string str1 = "Hello";string str2 = "Hi";string str3 = "Air";string str4 = "Bill";

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013Compound Boolean expression :is formed by combining simple Boolean expressions with the following logical operator :

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013More complex conditions can be created using logical operations AND, OR, and NOTRepresented by the symbols: &&, ||, !AND Operator, &&: Used with 2 simple expressionsExample: (age > 40) && (term < 10)Compound condition is true (has value of 1) only if age > 40 and term < 10

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013OR Operator, ||: Used with 2 simple expressionsExample: (age > 40) || (term < 10)Compound condition is true if age > 40 or if term < 10 or if both conditions are trueNOT Operator, !:Changes an expression to its opposite stateIf expressionA is true, then !expressionA is false

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013

Selection criteria with Boolean expressionCSC425 - SEPTEMBER 2013For example, the expression such as 5