6 - ICS103 Programming in C Lecture 6 Selection Structures 1 Outline Control Structures Conditions Relational Operators Logical Operators if statements

# 6 - ICS103 Programming in C Lecture 6 Selection Structures...

This preview shows page 1 - 13 out of 42 pages.

1 ICS103 Programming in C Lecture 6: Selection Structures 2 Outline Control Structures Conditions Relational Operators Logical Operators if statements Two-Alternatives One-Alternative Nested If Statements switch Statement 3 Control Structures Control structures –control the flow of execution in a program or function. Three basic control structures: Sequential Flow - this is written as a group of statements bracketed by { and }where one statement follows another. Selection control structure - this chooses between multiple statements to execute based on some condition. Repetition – this structure executes a block of code multiple times. C Y N C Y N 4 Compound Statements A Compound statement or a Code Block is written as a group of statements bracketed by { and } and is used to specify sequential flow. { Statement_1; Statement_2; Statement_3; } Example: the main function is surrounded by {}, and its statements are executed sequentially. Function body also uses compound statement. 5 Conditions A program chooses among alternative statements by testing the values of variables. 0 means false Any non-zero integer means true, Usually, we’ll use 1 as true. if (a>=b) printf(“a is larger”); else printf(“b is larger”); Condition - an expression that establishes a criterion for either executing or skipping a group of statements a>=b is a condition that determines which printf statement we execute. 6 Relational and Equality Operators Most conditions that we use to perform comparisons will have one of these forms: variable relational-operator variable e.g. a < b variable relational-operator constant e.g. a > 3 variable equality-operator variable e.g. a == b variable equality-operator constant e.g. a != 10 7 Relational and Equality Operators Operator Meaning Type < less than relational > greater than relational <= less than or equal to relational >= greater than or equal to relational == equal to equality != not equal to equality 8 Logical Operators logical expressions - expressions that use conditional statements and logical operators. && (and) A && B is true if and only if both A and B are true || (or) A || B is true if either A or B are true ! (not) !(condition) is true if condition is false, and false if condition is true This is called the logical complement or negation Example (salary < 10000) || (dependents > 5) (temperature > 90.0) && (humidity > 90) !(temperature > 90.0) 9 Truth Table && Operator A B A && B False (zero) False (zero) False (zero) False (zero) True (non-zero) False (zero) True (non-zero) False (zero) False (zero) True (non-zero) True (non-zero) True (non-zero) 10 Truth Table || Operator A B A || B False (zero) False (zero) False (zero) False (zero) True (non-zero) True (non-zero) True (non-zero) False (zero) True (non-zero) True (non-zero) True (non-zero) True (non-zero) 11 Operator Table ! Operator A !A False (zero) True (non-zero) True (non-zero) False (zero) 12 Remember!  #### You've reached the end of your free preview.

Want to read all 42 pages?

• Spring '10
• baleh
• Equals sign, relational operator, Switch statement
• • •  