cs 101 introductory programming - lecture 7: loops in c & good coding practices presenter: ankur...

21
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Upload: ann-bonsell

Post on 01-Apr-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding

Practices

Presenter: Ankur Chattopadhyay

Page 2: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

2

Command

Command

Command

Command

Command

Sequential Structure(straight-line structure)

Command

Test

Commandsto execute

if False

Command

Commandsto execute

if True

TrueFalse

Example Selection Structure(decision or branching structure)

Flowcharts for sequential, selection and iterative control structures

2Iterative Structure(looping structure)

Command

Commands

Command

Setup Done

Page 3: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Repetition Statements

• Repetition statements allow us to execute a statement multiple times

• Often they are referred to as loops

• Like conditional statements, they are controlled by Boolean expressions

• C has three kinds of repetition statements:

– the while loop– the do loop– the for loop

Page 4: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

The for Statement• A for statement has the following syntax:

for ( initialization ; condition ; increment ){ statement;}

The initializationis executed once

before the loop begins

The statement isexecuted until the

condition becomes false

The increment portion is executed at the end of each

iteration

Page 5: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Logic of a for loop

statement

true

conditionevaluated

false

increment

initialization

Page 6: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

//Multiplication Algorithmint a, b, product, count;printf("Enter two non-negative numbers: ");scanf("%i %i", &a, &b);for (product = 0, count = 0; count < b; count = count + 1) { product = product + a;}printf("The product of %i and %i is %i\n", a, b, product);

//Sum of first n natural numbersint sum = 0; // the accumulatorfor (int i = 1; i <= n; i++) { sum += i; }

Examples of for loop: Lecture 6

Page 7: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Dissecting The for Statement

• Each expression in the header of a for loop is optional

• If the initialization is left out, no initialization is performed

• If the condition is left out, it is always considered to be true, and therefore creates an infinite loop

– We usually call this a “forever loop”

• If the increment is left out, no increment operation is performed

Page 8: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

The while Statement• A while statement has the following syntax:

while ( condition ){ statement;}

• If the condition is true, the statement is executed

• Then the condition is evaluated again, and if it is still true, the statement is executed again

• The statement is executed repeatedly until the condition becomes false

Page 9: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Logic of a while Loop

statement

true false

conditionevaluated

Page 10: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

The while Statement

• An example of a while statement:int count = 1;while (count <= 5){printf(“%i”, count); count++; }

• If the condition of a while loop is false initially, the statement is never executed

• Therefore, the body of a while loop will execute zero or more times

Page 11: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Analyzing The while Statement

• A while loop is functionally equivalent to the following structure:

initialization;while ( condition ){ statement; increment;}

Page 12: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

// for loop example: Lecture 6for (product = 0, count = 0; count < b; count = count + 1) { product = product + a;}Counter-Controlled Loop - Definite Repetition// equivalent while loop (note: a for loop is more appropriate in this case)product = 0;count = 0;while (count < b) { product = product + a; count = count + 1;}

Examples of while loop

Page 13: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Sentinel-Controlled Loop: Indefinite Repetition

Nesting an if statement inside a loopint keep_going = 1;

while (keep_going == 1) { // computation would go here ...

int answer; printf("Continue? (1 for yes, 0 for no) "); scanf("%i", &answer); if (answer == 0) { keep_going = 0; }}

Another Example of while loop

Page 14: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

The do-while Statement

• A do-while statement (also called a do loop) has the following syntax:

do{ statement;} while ( condition )

• The statement is executed once initially, and then the condition is evaluated

• The statement is executed repeatedly until the condition becomes false

Page 15: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Logic of a do-while Loop

true

conditionevaluated

statement

false

Page 16: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Example of a do Statement

• An example of a do loop:

• The body of a do loop executes at least once

int count = 0;do{ count++; printf(“%i”, count);} while (count < 5);

Page 17: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Comparing while and do

statement

true false

conditionevaluated

The while Loop

true

conditionevaluated

statement

false

The do Loop

Page 18: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Infinite Loops• The body of a while loop eventually must

make the condition false

• If not, it is called an infinite loop, which will execute until the user interrupts the program

• This is a common logical (semantic) error

• You should always double check the logic of a program to ensure that your loops will terminate normally

Page 19: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Example: Infinite Loop

• An example of an infinite loop:

int count = 1;while (count <= 25){ printf(“%i”, count); count = count - 1;}

• This loop will continue executing forever

Page 20: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Good Coding Style Practice

• Always place braces around the body of a while loop.

• Advantages:– Easier to read– Will not forget to add the braces if you go back

and add a second statement to the loop body– Less likely to make a semantic error

• Indent the body of a loop - use spaces -- be consistent!

Page 21: CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay

Some Case Studies/Sample Programs To Try Out

• Problem 1:- Write a C program that takes a series of

integer scores as inputs using a loop, exits if a score entered is negative and computes the average of all the scores entered.

• Problem 2:- Write a C program that takes an integer

number as input and determines whether the number is prime or not.

• Problem 3: Using loops in a C program print the following pattern - 1

1 2 1 2 3 1 2 3 4 1 2 3 4 5 (Hint: Use nested loops)