repetition statements

Post on 08-Jan-2016

54 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

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

305171 Computer Programming

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

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

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);

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

for (start_expr; condition; count_expr)   statement;

is equivalent to

start_expr;while ( condition )

statement;count_expr;

31

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

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

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

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

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

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

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

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

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

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

top related