conditionals, loops, and other statements cs-2301, b-term 20091 conditionals, loops, and other kinds...

40
Conditionals, Loo ps, and Other Sta tements CS-2301, B-Term 20 09 1 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Upload: vivian-johnson

Post on 23-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 1

Conditionals, Loops, and Other Kinds of Statements

CS-2301, System Programmingfor Non-Majors

(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

Page 2: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 2

Reading Assignment

• Chapter 3 of Kernighan and Ritchie• Helpful to follow along in class

• Note on §3.8 :– we do not allow goto statements in this course

• There is rarely, if any, situation at WPI where a goto in C would be the right thing

• There is rarely a situation in any program where a goto is the right thing!

Page 3: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 3

Review

• Expression – a sequence of operators and operands that return a value

• Assignment – expression with the side effect of changing the value of the left operand

Page 4: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 4

Definition – Side Effect

• The changing of the value of some data in the course of evaluating or executing something else

• Sometimes unrelated data!

• Examples:–– Explicit assignments

• x = y + 3; i++; --j;– printf()

• Writes to internal buffer; flushes buffer to screen on '\n'

– scanf() – returns values from input• Explicit – assigns data to arguments

• Implicit – keeps track of where it is in internal buffer

Page 5: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 5

Definition – Statement

• Instruction to “do” something

• Specifies order of execution & evaluation

• §A.9 – a statement in C may be any of:–• labeled-statement

• expression-statement

• compound-statement

• selection-statement

• iteration-statement

• jump-statement

Page 6: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 6

Expression Statement

• expressionoptional ;

• Note: semicolon is terminator

• Exists primarily for its side effects

• E.g.,• x = y + 3; i++; --j;• printf(“string”, arg1, arg2, arg3);

• The following is perfectly legal in C:–• y + 3;

• Evaluates the expression, then throws the result away!

Page 7: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 7

Compound Statement

• A sequence of statements surrounded by {}• Example:–

{x = 0;i++;printf("The value of x is %d\n", x);

}

• Reason:– so that we can group together statements in loops, if-else, functions, etc.

Page 8: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 8

Compound Statement (continued)

• Compound statements may be nested{...;{ x = 0; i++;

} /* no semicolon needed here*/

printf("The value of x is %d\n", x);...;

}

Page 9: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 9

Compound Statement (continued)

• Compound statements may include declarations inside{double angle;angle = atan2(y, x) * 360/(2*pi);printf("Angle is %f degrees\n", angle);

}

• Declarations inside of {} are not known outside of {}

• Book says declarations must be at beginning of {}• Later versions of C relax this rule

• Declaration must always precede first use of declared identifier

Note: declaration-definition

of angle

allocates memory.

This is relinquished at end

of {}.

atan2(y,x) calculates the

arctangent of yx without

zero-divide problems

Page 10: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 10

Questions?

Page 11: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 11

If-else Statements

if (expr)statement1 /*do this if expr is non-zero*/

else

statement2 /*do this if expr is zero*/

• The else clause is optional!

May be any kind of statement –

loop, assignment, if-else, {}, etc.

Page 12: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 12

If-else Examples

if (j > limit)return j; /* note semicolon*/

elsej += stepValue; /* note semicolon*/

...

if (++k >= 4)k = 0; /* increment k mod 4*/

Page 13: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 13

If-else Examples (continued)

if (n < maxInput) {

scanf("string", &arg1, &arg2, …);

n++;

printf("string2", arg3, arg4, …);

} else { /* note NO semicolon*/

printf("Summary\n", arg5, …);

return n;

}

Page 14: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 14

Concatenated If-else Statements

if (expr1)

statement1 /*do this if expr1 is non-zero*/

else if (expr2)

statement2 /*i.e., expr1 == 0, expr2 != 0*/

else if (expr3)

statement3 /expr1 and expr2 are zero, expr3 != 0*/

else if (expr4)

statement4 /expr1, expr2 , expr3 all zero, expr4 != 0*/

else statement5 /*i.e., all expr are zero*/

Page 15: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 15

Concatenated If-else Statements

• Last else is always attached to last if

• If it should be attached to any other if, use {} to control the flow of execution.

Page 16: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 16

Switch Statement

• Somewhat like a concatenated if-else• Occasionally easier to read

• Each arm is called a case• Evaluate switch expression, select the

appropriate case (if any)– default case is optional

• Difference from concatenated if-else• Need break statement to exit switch after a case• Otherwise, control falls through to next case

• See §3.4 and p. 59

Page 17: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 17

Switch Statement Example

int month, daysInMonth, leapYear;…switch (month) {

case 0: printf("January\n");daysInMonth = 31;break;

case 1: printf("February\n");daysInMonth = leapYear ? 29 : 28;break;

case 2: printf("March\n");daysInMonth = 31;break;

case 3: printf("April\n");daysInMonth = 30;break;

…} // switch

Page 18: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 18

Switch Statement Example

int month, daysInMonth, leapYear;…switch (month) {

case 0: printf("January\n");daysInMonth = 31;break;

case 1: printf("February\n");daysInMonth = leapYear ? 29 : 28;break;

case 2: printf("March\n");daysInMonth = 31;break;

case 3: printf("April\n");daysInMonth = 30;break;

…} // switch

case values m

ust be

constants

break sta

tement needed to

jump over subsequent cases

defaul

t case is optional

(not shown)

Page 19: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 19

Questions?

Page 20: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 20

Iterative Statement

• while loop• for loop• do-while loop

Page 21: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 21

while loops

while (expression)statement

• Evaluate expression• If true, execute statement and then repeat• Repeat until expression becomes false

• statement may be executed zero or more times!

Often a compound statement

Page 22: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 22

int sum = 0;int count = 0;int input;while (scanf("%d", &input) != EOF){

sum += input;count++;printf("Input value of %f recorded.\n", input);...

}if (count > 0)

printf("Average is %f\n", (double)sum/count);else

printf("No inputs recorded\n");

while loop exampleNote initialization of sum

and count

What is this?

Page 23: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 23

while loop examples (continued)

• Study example on p. 59 (§3.4)

• A program to count digits, white space characters, and other characters.

• Includes a while loop with a switch statement inside

Page 24: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 24

do-while loop

• do statement

while (expression);

• Similar to while loop, but guaranteed to execute statement at least once

• See §3.6

Note: semicolon is

required here

Page 25: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 25

Breaking out of a Loop

• When it becomes necessary to terminate a loop prematurely– break; /*exits smallest containing

switch or loop*/

• When it becomes necessary to terminate the current iteration and start the next one– continue; /*terminates this iteration only*/

• See p. 65, §3.7

Page 26: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 26

Questions?

Page 27: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 27

for loop

• A counting loopfor (expr1; expr2; expr3)

statement

• Evaluate expr1 to initialize• Evaluate expr2 to test

• If true, execute statement• If not true, exit for loop

• Evaluate expr3 to prepare for next iteration• Repeat expr2 to test

Remember: zero is false,

non-zero is true

Page 28: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 28

for loops and while loops

• The for loopfor (expr1; expr2; expr3)

statement

• is exactly equivalent to the followingexpr1; while (expr2) {

statementexpr3;

}

• See p. 60

Page 29: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 29

The most common kind of for-loop

int i;

for (i = 0; i < limit; i++) {

...;

/* do something with ith entity */

...;

}

Loop “body” is typically

a compound statement

Page 30: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 30

The most common kind of for-loop

int i;

for (i = 0; i < limit; i++) {

...;

/* do something with ith entity */

...;

}

It is traditional in C that for-

loops start counting at zero

and test that the counter is less

than the upper limit

Page 31: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 31

The most common kind of for-loop

int i;

for (i = 0; i < limit; i++) {

...;

/* do something with ith entity */

...;

} • This loop iterates limit times.

– Iterations are numbered i = 0, 1, 2, …, limit-1

• Reason:– arrays are indexed this way!

Page 32: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 32

Nested for-loops

int i, j;for (i = 0; i < limit; i++) {...; /*prepare subgroup i*/for (j=0; j < limit2; j++) {

...;/* do something with item j of

subgroup i*/...;

}...;

}

Page 33: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 33

An Extension in Modern C Compilers

• The following construct (not in book) is legal in C99:–for (int i = 0; i < limit; i++){expression involving i;...;printf(“Iteration %d completed.\n”, i);...;

}

• The loop counter i is declared in for loop• Not visible outside of loop!

• Common practice• Good programming style

Page 34: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 34

Notes on Loop Style

for (int i = 0; i < limit; i++) {

...; /*prepare for subgroup i*/

for (int j=0; j < limit2; j++) {

...;

/* do something with item j of

subgroup i*/

...;

} /* end of loop j */

...;

} /* end of loop i */

Declare loop variables in

for-statements – C99

Page 35: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 35

Notes on Loop Style

for (int i = 0; i < limit; i++) {

...; /*prepare for subgroup i*/

for (int j=0; j < limit2; j++) {

...;

/* do something with item j of

subgroup i*/

...;

} /* end of loop j */

...;

} /* end of loop i */

Include a comment at end of

each loop to show which loop

it is

Page 36: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 36

Questions?

Page 37: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 37

Simple Example

• Read a series of numbers

• Add up the numbers and also the squares of those numbers “on-the-fly”

• After all numbers are input, print out:–– The mean – The standard deviation

Page 38: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 38

Simple Example (continued)

#include <stdio.h>#include <math.h>

int main(int argc, char*argv[]) {

unsigned int n; double mean; double sum = 0; double sumOfSquares = 0;

printf("Enter sequence“" of numbers:- ");

for (n = 0; ; n++) {int rc;double input;

rc = scanf("%lf", &input);

if (rc == EOF) break;else if (rc == 0) continue;

sum += input;sumOfSquares += input *

input;

} //for (n = 0 ...

Page 39: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 39

Simple Example (concluded)

printf("The total number of input entries“" was %u.\n", n);

if (n > 0 ) {

printf("The mean is %g.\n", mean = sum/n);

printf("The standard deviation is %g.\n",

sqrt(sumOfSquares/n - pow(mean, 2)));

} // if (i > 0)

return 0;

} //main

Example can be found here

Page 40: Conditionals, Loops, and Other Statements CS-2301, B-Term 20091 Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors

Conditionals, Loops, and Other Statements

CS-2301, B-Term 2009 40

Questions?