Program Control Structures In C - Part 2

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

Info iconThis 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
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 – 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.
Background 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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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”. Always
Background image of page 4
Image of page 5
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 / 38

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

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

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