Program Control Structures In C - Part 2

Program Control Structures In C - Part 2 - COP 3223 C...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3223: C Programming (Control Structures – Part 2) Page 1 © Dr. Mark J. Llewellyn COP 3223: C Programming Spring 2009 Program Control Structures In C – Part 2 School of Electrical Engineering and Computer Science University of Central Florida Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3223/spr2009/section1
Image of page 1

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

View Full Document Right Arrow Icon
COP 3223: C Programming (Control Structures – Part 2) Page 2 © Dr. Mark J. Llewellyn Control Structures In C C provides three types of repetition structures in form of statements. 1. The while repetition statement allows an action to be repeated as long as some condition remains true. This is a “top-tested” repetition statement, which means that the condition is evaluated before the action is executed the first time. If the condition is initially false, the action is not performed even once. 2. The do…while repetition statement allows an action to be repeated as long as some condition remains true. This is a “bottom-tested” repetition statement, which means that the condition is not evaluated until the action is performed the first time. Thus, the action is always performed at least once with this type of repetition statement. 3. The for repetition statement repeats an action a specific number of times based upon a counter value (an integer). This repetition statement is referred to as a “counted loop” statement.
Image of page 2
COP 3223: C Programming (Control Structures – Part 2) Page 3 © Dr. Mark J. Llewellyn The while Repetition Statement The format of the while repetition statement is: while ( condition ) { statements; } statement x; The condition is any expression which evaluates to true or false (i.e., a Boolean expression). When a while statement (also called while loop ) is executed, the condition is evaluated first. If its value is nonzero (true) the statements in the body of the while loop are executed and the expression is evaluated again. Execution of the statements in the body of the loop continue as long as the condition remains true. These statements are executed only if the condition evaluates to true. Statement x is the first statement to be executed when the condition is false.
Image of page 3

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

View Full Document Right Arrow Icon
COP 3223: C Programming (Control Structures – Part 2) Page 4 © Dr. Mark J. Llewellyn The while Repetition Statement The program on the following page uses a while statement to print the sum of the first 10 integers (one integer at a time). COMMON PROGRAMMING ERROR: Assuming that a while statement’s condition is initially true and execution enters the body of the while statement, there must be some statement within the body of statements that changes the value of the condition. Otherwise, the condition will always remain true and execution of the loop will never terminate. This is called an “infinite loop”.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern