Control Structures - Revisited

Control Structures - Revisited - COP 3223: C Programming...

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

View Full Document Right Arrow Icon
COP 3223: C Programming (Control Structures Revisited ) Page 1 © Dr. Mark J. Llewellyn COP 3223: C Programming Spring 2009 Control Structures - Revisited School of Electrical Engineering and Computer Science University of Central Florida Instructor : Dr. Mark Llewellyn markl@cs.ucf.edu HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3223/spr2009/section1
Background image of page 1

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

View Full DocumentRight Arrow Icon
COP 3223: C Programming (Control Structures Revisited ) Page 2 © Dr. Mark J. Llewellyn An Easy To Make Error With A For Statement What is the output from the following program, assuming the user enters the value 15?
Background image of page 2
COP 3223: C Programming (Control Structures Revisited ) Page 3 © Dr. Mark J. Llewellyn An Easy To Make Error With A For Statement The problem is this semicolon which effectively ends the for statement. Thus, the body of the for loop contains no statements. The for loop executes properly and the value of i after the loop terminates is 16.
Background image of page 3

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

View Full DocumentRight Arrow Icon
COP 3223: C Programming (Control Structures Revisited ) Page 4 © Dr. Mark J. Llewellyn An Easy Error To Make With Conditional Expressions There is one type of error that many beginning C programmers make, including some experienced C programmers, when dealing with conditional expressions. This error is confusing the equality operator ( == ) with the assignment operator ( = ). Often this mistake is made as simply a typing error, but has potentially disastrous effects on your program, because it will ordinarily not generate a syntax error. To illustrate this problem, consider the case where we have a set of employees and their paycodes, where if the employee’s paycode is set to 5, we will print a message that they have been awarded a bonus. Consider the two cases shown on the next page:
Background image of page 4
COP 3223: C Programming (Control Structures Revisited ) Page 5 © Dr. Mark J. Llewellyn An Easy Error To Make With Conditional Expressions If we intended to type the version shown in (a), but mistakenly typed the version in (b), what will happen? Version (b) will evaluate the assignment expression , which simply assigns the constant value 4 to the variable payCode . Since C interprets any nonzero value as “true”, the condition of this if statement is always true and the person (indeed every person) will be awarded the bonus! if (payCode == 4) { printf(“You’ll get a bonus!\n”); } if (payCode = 4) { printf(“You’ll get a bonus!\n”); } (a) (b)
Background image of page 5

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

View Full DocumentRight Arrow Icon
COP 3223: C Programming (Control Structures Revisited ) Page 6 © Dr. Mark J. Llewellyn An Easy Error To Make With Conditional Expressions GOOD PROGRAMMING PRACTICE
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.

This note was uploaded on 10/21/2010 for the course COP 3223 taught by Professor Guha during the Spring '08 term at University of Central Florida.

Page1 / 29

Control Structures - Revisited - COP 3223: C Programming...

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