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
Image of page 1
2 Outline Control Structures Conditions Relational Operators Logical Operators if statements Two-Alternatives One-Alternative Nested If Statements switch Statement
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
Image of page 3
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.
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.
Image of page 5
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
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
Image of page 7
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)
Image of page 8
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)
Image of page 9
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)
Image of page 10
11 Operator Table ! Operator A !A False (zero) True (non-zero) True (non-zero) False (zero)
Image of page 11
12 Remember!
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 42 pages?

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

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes