Week 4 Lec 1 PDF
Document Details
Uploaded by BestKnownHeather
Tags
Related
- Week 4 Control Structures PDF
- M5-CONDITIONALS PDF - If and If-Else Statements
- OOPR Midterms Reviewer Module 5 Conditional Statements PDF
- Programming Operators and Control Statements (PDF)
- SYSC 2006: Imperative Programming Lecture 3 - Flow of Control (Carleton University)
- Chapter 4 Selection Control Structure PDF
Summary
This document covers fundamental concepts in programming, focusing on selection control structures, relational and logical operators. It explains the various types of control structures, including sequential, selection, and repetition, with practical examples and diagrams. It also introduces logical and relational operators and discusses their application in different contexts.
Full Transcript
Selection 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure 2. Use relational and logical operators 3. Use if, if..else, if..else if, switch..case, and nested if statements and condit...
Selection 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure 2. Use relational and logical operators 3. Use if, if..else, if..else if, switch..case, and nested if statements and conditional operators in program. 2 Control Structures Program is written using simple control structures to determine the flow of program execution. Three types of control structure: sequential, selection, repetition. SEQUENTIAL – is a series of statements that execute one after another. statement 1 statement 2 statement 3 …. 3 Control Structures SELECTION (branch) - is used to execute one set of statements if a given condition is TRUE, a different set of statements, or no statements at all, is executed if the condition is FALSE. statement set 1 …. condition …. statement set 2 4 3. SYSTEM DESIGN continued… Control Structures REPETITION (loop) - is used to execute a set of statements repeatedly as long as a given condition is TRUE. …. Condition …. statement set 5 The logic flow of Control Structures Sequential false ? true ? : boolean Selection expression false ? ? : boolean Repetition true expression 6 Why Selection? Selection structure allows the program to select an action (statements to execute), based upon a condition. For example, by using an if statement to check the password entered, your program can decide whether or not a user is allowed access to the program. The condition in this example is a valid password. Condition is evaluated (checked and determined) to be either true or false. 7 Understand TRUE and FALSE The operator will assign 1 if the evaluation is true, or 0 if the evaluation is false. true or false is known as boolean value (bool). An expression (program code that has a value) that has a boolean value is called a boolean expression. Thus, a condition is a boolean expression. 8 Boolean Expression There are a number of operators that allow checks on condition. There are two types of operators that could be used in boolean expression: relational operators and logical operators. Expression with a relational operator is called relational expression, and with logical operator is called logical expression. Relational and logical expressions are both boolean expressions, and can be used as condition. 9 Relational Operators Used to compare between two values. Operator Meaning Relational Result Expression < less than 1< 2 true 2 false >= greater than or equal to 1>=2 false == equal to 1==2 false != not equal to 1!=2 true 10 Relational Operators More examples: Assume int y=6, x=5; Relational Sample Relational Value Operators Expression > y>x T < y= x>=3 T = y ) && ( x ! = 3) 14 Logical Operator || (OR) Example : Assume int x = 4, y = 5, z = 8; expression1 expression2 expression1 | | Sample Expression expression2 F F F ( y > 10) | | ( z < = x ) F T T ( z < = y) | | ( x = = 4) T F T ( y ! = z) | | ( z < x ) T T T (z>=y)||(x!=3) 15 Logical Operator ! (NOT) Example : Assume int x = 50 expression ! expression Sample Expression F T ! (x = = 60) T F ! (x ! = 60) 16 Precedence of Logical Operator Highest ! && Lowest || Example: (2 < 3) || (5 > 6) && (7 > 8) 1 2 is true because AND is evaluated before OR 17 More on Operator Precedence Highest Arithmetic operators Relational operators Lowest Logical operators Example: 8 < 2 + 7 || 5 == 6 is true 1 2 3 4 16 Boolean Expressions Can be assigned to a variable Example : bool result = (x = 0 && grade