Lecture04_control_stmts

Lecture04_control_stmts - 1 EE312 - Lecture 4 Announcements...

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

View Full Document Right Arrow Icon
1 Lecture 4 1 EE312 - Lecture 4 Announcements • Topics for today will address: – More Program control statements – C Language Types • Take advantage of your recitation sections – TAs there to help with assignment questions – TAs cover questions at the chapter ends • Read Chapter 7 before Thursday Flowchart Symbology 101 - Primitives - Start/stop Process/ Task/action Decision Control flow Input/output connector
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 Lecture 4 3 Three Proper Logic Constructs sequence iteration selection condition T F condition T F . . . one or a block of statements grouped together Lecture 4 4 Three Proper Logic Constructs - pseudocode style - sequence iteration selection 1. … 2. … 3…. 1. Repeat below steps while condition is true a) … b) … c) … end repeat 2. … 3. … 1.If condition is true then else end if 2. … 3. … Use indentation heavily to see the block structure
Background image of page 2
3 Program Control Statements Decision Making - selecting among alternative paths •Boolean values, variables and expressions (either T or F) •Using if and if. ..else •Nested if Statements •Using switch Statements •Conditional Operators Iteration - bounded repetition of statement(s) •Looping: while , do, and for •Nested loops •Using break and continue Controlling the flow of the program’s logic Lecture 4 6 Conditional Expressions • The relational operators are: < less than > greater than <= less than or equal >= greater than or equal • The value of a relational expression is either 1 (if the condition is true) or 0 (if it is false): 1 < 2 has the value 1 1 > 2 has the value 0 • The relational operators have lower precedence than the arithmetic operators. • For example: i + j < k - 1 means (i + j) < (k - 1)
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 Lecture 4 7 Equality Conditions • The equality operators are: == equality != inequality • The equality operators produce either 1 or 0 as their result. Lecture 4 8 If Statements Examples if (i > j) max = i; if (i < 0) printf("Less than zero"); else if (i == 0) printf("Equal to zero"); else printf("Greater than zero"); 3 way branch if (i > j) max = i; else max = j; 2 way branch if (i > j) if (i > k) max = i; else max = k; else if (j > k) max = j; else max = k; How many branches?
Background image of page 4
5 Lecture 4 9 Blocks • A block (aka compound statement) has the form { statements } • A block of statements are used whenever C requires a single statement, but we want to include more than one statement. • blocks, unlike ordinary statements, are not followed by a semicolon. Example: if (i < j) { temp = i; i = j; j = temp; } Lecture 4 10 The “Dangling else” Problem • Warning: Watch out for the “dangling else” problem: if (y != 0) if (x != 0) result = x / y; else printf("Error: y is equal to 0\n"); Which if does that else belong to ? •Use the block to fix it: if (y != 0) { if (x != 0) result = x / y; } else printf("Error: y is equal to 0\n");
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 Lecture 4 11 Conditional Expressions • A conditional expression is like an if statement that produces a value.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

Lecture04_control_stmts - 1 EE312 - Lecture 4 Announcements...

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

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