Lecture04_control_stmts

Lecture04_control_stmts - 1 EE312 Lecture 4 Announcements...

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

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

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

View Full Document
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
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)

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

View Full Document
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?
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 ﬁx it: if (y != 0) { if (x != 0) result = x / y; } else printf("Error: y is equal to 0\n");

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

View Full Document
6 Lecture 4 11 Conditional Expressions • A conditional expression is like an if statement that produces a value.
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

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
Ask a homework question - tutors are online