repetition statements

41
305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University

Upload: orinda

Post on 08-Jan-2016

54 views

Category:

Documents


4 download

DESCRIPTION

305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University. Repetition Statements. Control Structures Revisit. Control structures control the flow of execution in a program or function. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Repetition Statements

305171 Computer Programming

Rattapoom WaranusastDepartment of Electrical and Computer EngineeringFaculty of Engineering, Naresuan University

Page 2: Repetition Statements

Control structures control the flow of execution in a program or function.

Instructions are organized into 3 kinds of control structures– Sequence– Selection (decision, condition)– Repetition (looping)

2

Page 3: Repetition Statements

a series of actions are performed in sequence

3

{int a,n;

n = 7;

printf("n = %d\n",n);printf("a = ++n\n");a = ++n;printf("n = %d, a = %d\n",n,a);

}

Page 4: Repetition Statements

One of two possible actions is taken, depending on a condition.

4

:

:scanf(“%d”, n);if (n == 13){

printf(“Lucky you!”);n++;

}printf(“You entered %d.”,n);

::

YesNon = 13?

n = n + 1

get n

display“Lucky You”

display“You entered”

and n

Page 5: Repetition Statements

A repetition structure represents part of the program that repeats. This structure is commonly known as a loop.

In the flowchart segment, the question “is x < y?” is asked. If the answer is yes, then Process A is performed. The question “is x < y?” is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited.

5

x < y?

AYES

Page 6: Repetition Statements

A program choose among alternative statements by testing the value of key variables.

Most conditions that we use to perform comparisons will use relational operators.

With logical operators, we can form more complicated conditions or logical expressions.

6

Page 7: Repetition Statements

Loops are used to repeat a block of code. Being able to have your program

repeatedly execute a block of code is one of the most basic but useful tasks in programming.

There are three types of loops: while, for, and do..while. Each of them has their specific uses.

7

Page 8: Repetition Statements

while loops are very simple. The basic structure is

while ( condition )statement;

If the condition is true (non-zero), the statement will be executed until the condition is false (zero).

Multiple statements can be grouped by putting them inside curly braces {, }.

8

Page 9: Repetition Statements

Example:

:int x = 0; while ( x < 10 ) {

printf( "%d\n", x ); x++;

}printf (“end\n”);

::

9

YesNox < 10?

x = x + 1

displayx

display“end”

x = 0

Page 10: Repetition Statements

Exercise 1: Basic while1. Start and prepare to run a new project in

VC++ Express2. Copy the code from while.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.

10

Page 11: Repetition Statements

Exercise 2: Summation using while1. Start and prepare to run a new project in

VC++ Express2. Copy the code from sumscore.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to compute mean of the

score.

11

Page 12: Repetition Statements

Exercise 3: Summation using while 21. Start and prepare to run a new project in

VC++ Express2. Copy the code from sumscore2.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to compute mean of the

score.

12

Page 13: Repetition Statements

Exercise 4: Factorial1. Start and prepare to run a new project in

VC++ Express2. Copy the code from factorial.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.

13

Page 14: Repetition Statements

Exercise 5: Yes or No1. Start and prepare to run a new project in

VC++ Express2. Copy the code from yesno1.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the original code to cover small

letters.

14

Page 15: Repetition Statements

Exercise 6: Infinite loop1. Start and prepare to run a new project in

VC++ Express2. Copy the code from infinite.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code so that it no longer an

infinite loop.

15

Page 16: Repetition Statements

Infinite loops are loops that never end. They are created when a loop’s

expression is never set to exit the loop. Every programmer experiences an infinite

loop at least once in his or her career.

16

Page 17: Repetition Statements

The do-while loop is similar to the while loop except the relational test occurs at the bottom (rather than top) of the loop.

This ensures that the body of the loop executes at least once.

As long as the condition is True (non zero), the body of the loop continues to execute.

17

Page 18: Repetition Statements

The format of the do-while is do

    statement; while ( condition );

The condition must be enclosed within parentheses, just as it does with a while statement.

Notice the semi-colon at the end of while condition.

18

Page 19: Repetition Statements

Example:

:int x = 10; while ( x < 10 ) {

printf( "%d\n", x ); x++;

}printf (“end\n”);

::

19

YesNox < 10?

x = x + 1

displayx

display“end”

x = 10

Page 20: Repetition Statements

Example:

:int x = 10; do{

printf( "%d\n", x ); x++;

}while ( x < 10 ); printf (“end\n”);

::

20

YesNox < 10?

x = x + 1

displayx

display“end”

x = 10

Page 21: Repetition Statements

Exercise 7: do...while1. Start and prepare to run a new project in

VC++ Express2. Copy the code from dowhile.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.

21

Page 22: Repetition Statements

Exercise 8: Yes or No 21. Start and prepare to run a new project in

VC++ Express2. Copy the code from yesno2.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the original code to cover small

letters.

22

Page 23: Repetition Statements

Exercise 9: Check for a negative number1. Start and prepare to run a new project in

VC++ Express2. Copy the code from checknegative.c and

paste into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to check for positive value,

even or odd number.

23

Page 24: Repetition Statements

The for loop enables you to repeat sections of your program for a specific number of times.

Unlike the while and do-while loops, the for loop is a determinate loop. This means when you write your program you can usually determine how many times the loop takes place.

24

Page 25: Repetition Statements

The format of the for loop is

for (start_expr; condition; count_expr)   statement;

C evaluates the start expression before the loop begins. Typically, the start expression is an assignment statement, but it can be any legal expression. C looks at and evaluates start expression only once, at the top of the loop.

Every time the body of the loop repeats, the count expression executes, usually incrementing or decrementing a variable. The condition evaluates to True (nonzero) or False (zero), and then determines if the body of the loop repeats again.

25

Page 26: Repetition Statements

Example:

:int x = 0; while ( x < 10 ) {

printf( "%d\n", x ); x++;

}printf (“end\n”);

::

26

YesNox < 10?

x = x + 1

displayx

display“end”

x = 0

Page 27: Repetition Statements

Exercise 10: for loop1. Start and prepare to run a new project in

VC++ Express2. Copy the code from for.c and paste into the

code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to use other increment or

initialization methods.6. Modify the code to use only while loop.

27

Page 28: Repetition Statements

Exercise 11: Range1. Start and prepare to run a new project in VC++

Express2. Copy the code from range.c and paste into the

code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to count the number of

integers between two inputs.6. Modify the code to calculate the sum of all

integers between A and B, and also find the average.

28

Page 29: Repetition Statements

Exercise 12: Even and odd revisit1. Start and prepare to run a new project in

VC++ Express2. Copy the code from evenodd.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to let the user input the

minimum and maximum numbers.6. Modify the code to count the integers

between A and B that are divisible by 5.29

Page 30: Repetition Statements

Exercise 13: Factorial revisit1. Start and prepare to run a new project in

VC++ Express2. Copy the code from factorial.c and paste

into the code editor.3. Modify the code to use only for loop.

30

Page 31: Repetition Statements

for (start_expr; condition; count_expr)   statement;

is equivalent to

start_expr;while ( condition )

statement;count_expr;

31

Page 32: Repetition Statements

The break statement is used to manipulate program flow in structures such as loops.

When a break statement is executed in a loop, the loop is terminated and program control returns to the next statement following the end of the loop.

32

Page 33: Repetition Statements

Exercise 14: break statement1. Start and prepare to run a new project in

VC++ Express2. Copy the code from break.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to use while loop.

33

Page 34: Repetition Statements

The continue statement is used to manipulate program flow in a loop structure.

When executed, any remaining statements in the loop are passed over and the next iteration of the loop is sought.

34

Page 35: Repetition Statements

Exercise 15: continue statement1. Start and prepare to run a new project in

VC++ Express2. Copy the code from continue.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to use while loop.

35

Page 36: Repetition Statements

Any C statement can go inside the body of a loop—even another loop. When you put a loop within a loop, you are creating a nested loop.

Each time the outer loop is repeated, the inner loops are reentered, their loop control expressions are reevaluated, and all required iterations are performed.

36

Page 37: Repetition Statements

Exercise 16: Nested loops1. Start and prepare to run a new project in

VC++ Express2. Copy the code from nestedfor.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to print more than one

rectangle.

37

Page 38: Repetition Statements

Example:

:for(j = 0; j < row; j++) {

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

printf("*");} printf("\n");

}

::

38

i < col?

YesNoj < row?

i = i + 1

display*

new line

j = 0

i = 0

Yes

No

j = j + 1

Page 39: Repetition Statements

Exercise 17: Multiplication table1. Start and prepare to run a new project in

VC++ Express2. Copy the code from mtable.c and paste into

the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to allow a user to specify

the first and the last tables.

39

Page 40: Repetition Statements

int abs (int x); IxIdouble fabs (double x); IxI

double sin (double x); sin xdouble cos (double x); cos xdouble tan (double x); tan x double asin (double x); sin-1 x double acos (double x); cos-1 x double atan (double x); tan-1 x

double exp (double x); ex

double log (double x); ln xdouble log10 (double x); log x

double pow (double x,double y); xy

double sqrt(double x); x

40

Page 41: Repetition Statements

Exercise 17: Multiplication table1. Start and prepare to run a new project in

VC++ Express2. Copy the code from distance.c and paste

into the code editor.3. Run the code and observe the result.4. Let’s play with the code.5. Modify the code to compute an angle forms

by the line that pass these points and x-axis.

41