control4 - Control Structures Chapter 3 and Chapter 5...

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

View Full Document Right Arrow Icon
Introduction All programs are written in terms of three different types of control structures. Recall that control structures allow you to act upon the decisions that are made by the algorithm. The three basic control structures are: 1. Sequence structures Statements are executed one after the other in the order they were written. This is built into the C language. C programs are executed sequentially by default. 2. Selection structures C has three types: if , if-else , and switch . 3. Repetition structures Again, C has three types: while , for , and do-while . We will examine all of the various types of control structures. Repetition Structures Repetition structure: The loop is an iteration construct that provides a way of controlling the repetition. The programmer specifies some action which is to be repeated as long as some specified condition remains true. A loop may appear anywhere within the body of an algorithm. We’ll look at while loops first since it is a basic loop construct and the other loop constructs can be expressed in terms of a while loop. While Loop Repetition Structure A while loop is repeated until the looping condition becomes false. For example, while there are more items on my grocery list – purchase next item and cross it off the list. The general format of the while loop is: Control Structures - 1 Control Structures – Chapter 3 and Chapter 5
Background image of page 1

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

View Full DocumentRight Arrow Icon
while (boolean expression) statement; where statement is any valid statement in C (including an empty statement or a block. It is called the body of the loop. Typically, the body of the loop will consist of more than a single statement and therefore, most while loops will look like the following: while (<boolean expression>) { statement 1; statement 2; . . . statement n; } statement A; 1) Evaluate the boolean expression 2) If its true, execute statement 1 through statement n in order. 3) If its false, skip to statement A and continue execution. 4) After statement n is executed, one iteration is completed. Execution goes back to step 1 and everything is repeated. Graphical representation of while-loop control structure Examples (1) product = 1; while ( product <= 100) product = 2 * product; Control Structures - 2 true false condition statement(s)
Background image of page 2
(2) x = 1; while (x < 10) { printf (“%d \n”, x); x = x + 1; } printf (“%d\n”, x); (3) /* illustration of an infinite loop!!!! */ x = 1; while (x != 10) { printf (“%d \n”, x); x = x + 2; } (4) /* illustration of short circuit evaluation */ /* assume x = 7 and y = 5 at this point */ while ((x > 0) && (y > 0)) { printf (“%d \n”, x * y); x = x – 2; y = y – 3; } Example Algorithms Using While Loops In Class Exercise Control Structures - 3 /* Determine the amount of money everyone in class has with them */ #include <stdio.h> int main ( ) { double individual_amt; /* amount on one student */ total = 0; /* total amount on the class */ char response; /* a student’s response */ printf (“Does any one have any money on them? Enter y or Y\n”);
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

control4 - Control Structures Chapter 3 and Chapter 5...

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

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