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

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 6: Selection Structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Control Structures Conditions Relational Operators Logical Operators if statements Two-Alternatives One-Alternative Nested If Statements switch Statement
Background image of page 2
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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Logical Operators logical expressions - expressions that use conditional statements and logical operators. || (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)
Background image of page 8
9 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)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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)
Background image of page 10
11 Operator Table ! Operator A !A False (zero) True (non-zero) True (non-zero) False (zero)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/09/2010 for the course ICS 103 taught by Professor Baleh during the Spring '10 term at Abilene Christian University.

Page1 / 42

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

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online