Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | roi-vincent-gementiza |
View: | 3 times |
Download: | 0 times |
8/10/2015 2
Loops or Iteration
A loop is a structure
that will execute a
group of lines of code
a number (i.e., zero to
many) times based on
predetermined criteria
8/10/2015 3
Loops All languages need an iteration
statement
Recall that a Structured program
is made up of only the three
constructs:
Sequence
Selection
Iteration
8/10/2015 4
Loops in C The purpose of a loop is to execute a
group of lines a number of times
The block is the unit that gets repeated.
Recall that a block is any number of
simple c statements surrounded by curly
braces.
a block can contain any number or type of
statements
a block can contain conditional logic, or
other loops
8/10/2015 5
Loops Have:
Initialization
Test to exit the loop
Statements in the body of the loop
Increment or other statement that
will change the value that
determines whether you will stay in
the loop
some loops omit some of these
the order of these may vary
8/10/2015 7
Under What Conditions Do You Use Each
for - when you want to initialize a
variable at the beginning of the loop
and increment it after each iteration
while -when initialization and/or
increments are not done by the loop.
This is the "test before" loop
do / while -same as while but used
when you want to execute the body of
the loop at least once. This is the
"test after" loop
8/10/2015 8
The for loop
A specially designed count-controlled
loop
for (initialization; test; updatecounter)
statement;
for (initialization; test; updatecounter)
{
statement1;
statement2;
. . .
}
8/10/2015 9
The for loop
Actions of the for spread out
around the loop
initialization occurs only ONCE
at the start
testing is the first repeated
action of the loop (it gets done
before the body each iteration)
updatecounter occurs at the end
of the loop body
8/10/2015 10
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“%d\n”,count);
Example
Initialization Test Updatecounter
End
Body
11
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
8/10/2015 12
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
1
8/10/2015 13
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
1
True
8/10/2015 14
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
1
1
8/10/2015 15
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
2
1
8/10/2015 16
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
2
True
1
8/10/2015 17
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
2
12
8/10/2015 18
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
3
12
8/10/2015 19
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
3
True
12
8/10/2015 20
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
3
123
8/10/2015 21
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
4
123
8/10/2015 22
int count;
for (count = 1; count <= 3; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
Output
count
Example (Cont ..)
4
False
123
When the loop control condition is evaluated and has value false, the loop is said to be “dissatisfied” and control passes to the statement following the for
structure.
8/10/2015 23
int count;
for (count = 1;; count++)
{
printf(“%d\n”,count);
}
printf(“Count = %d\n”,count);
;
Example (Cont ..)
Output
count
4
123Count = 4
8/10/2015 24
Points to Note about the Structure of the "for" Stmt "for" is lower case
the three parameters are surrounded by
parentheses
the three parameters are separated by semi-
colons
all three parameters must be identified (but
could be empty)
can initialize variable(s) within the
parentheses e.g., (int counter = 0; … )
can have multiple initialization and increment
statements (separated by commas)
increment expression may involve ++ or --
operators
no punctuation follows the closing parenthesis
8/10/2015 25
Structure of a "while" Loop
while (boolean-expression)
statement-1
where statement-1 can be simple or compound
8/10/2015 26
Structure of the "while" Statement
while ( count < 3 )
while starts with a lower case letter
the boolean expression is enclosed in
parentheses and can be simple or complicated
there is no punctuation that follows the
closing parenthesis (this is a common error)
there is no punctuation that follows the
closing brace
8/10/2015 27
"while" with no block
Basic while (without a block)
int count = 0;
while ( count <= 3 )
printf(“%d\n”,count);
note: count ++ means count = count + 1
8/10/2015 28
"while" with a Block
while with a block
int count = 3;
while ( --count >= 0 )
{
printf(“%d\n”,count);
}
8/10/2015 29
Increment Using the Postfix Operator ++
Using ++
Example: count ++;
Means the same ascount = count + 1;
but uses value of count and then
increments
count --
similar to ++ but means count =
count - 1;
uses value of count and then
decrements
8/10/2015 30
Increment Using the Prefix Operator ++
Example: ++count;
Means the same as
count = count + 1;
but the increment takes place before
the statement is executed
There is also a prefix -- operator
Example of the -- prefix operator:
amtOwed = --numEmpl * amtPerEmpl;
8/10/2015 31
Clarifying Prefix and Postfix
What will be printed:
int i = j = 15;
prinf(“%d\n”,i);
prinf(“%d\n”,i++);
prinf(“%d\n”,i);
prinf(“%d\n”,j);
prinf(“%d\n”,++j);
prinf(“%d\n”,j);
8/10/2015 32
int count;
count = 4; // initialize loop variable
while (count > 0) // test condition
{
printf(“%d\n”count); // repeated action
count--; // update loop variable
}
printf(“Done\n”);
Example
33
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
Example (cont.)
8/10/2015 34
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
4
Example (cont.)
8/10/2015 35
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
4
True
Example (cont.)
8/10/2015 36
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
4
4
Example (cont.)
8/10/2015 37
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
3
4
Example (cont.)
8/10/2015 38
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
3
4
True
Example (cont.)
8/10/2015 39
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
;
Output
count
3
43
Example (cont.)
8/10/2015 40
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
2
43
Example (cont.)
8/10/2015 41
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
2
43
True
Example (cont.)
8/10/2015 42
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
2
432
Example (cont.)
8/10/2015 43
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
1
432
Example (cont.)
8/10/2015 44
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
1
432
True
Example (cont.)
8/10/2015 45
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
1
4321
Example (cont.)
8/10/2015 46
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
4321
0
Example (cont.)
8/10/2015 47
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
0
4321
False
Example (cont.)
8/10/2015 48
int count;
count = 4;
while (count > 0)
{
printf(“%d\n”count);
count--;
}
printf(“Done\n”);
Output
count
0
4321Done
Example (cont.)
8/10/2015 49
"do while" Loop sometimes called a "do" loop
"do while" loop has the test at the end
Example:
char response = 'Y';
do
{
… //other statements in body of loop
printf(“Edit more Y?N”);
scanf(“%c”,response);
} while (response == 'Y');
Relational expression follows word while
Note: no punctuation after do
Note: remember to put the ; following the while stmt
8/10/2015 50
Example 2
// display the numbers from 4 to 1
// using the posttest do while loop
int count;
count = 4; // initialize loop variable
do
{
printf(“%d\n”,count); // repeated action
count--; // update loop variable
} while (count > 0); // test condition
printf(“Done\n”);
8/10/2015 51
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
4
8/10/2015 52
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
4
4
8/10/2015 53
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
;
Output
count
3
4
8/10/2015 54
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
3
4
8/10/2015 55
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
3
4
True
8/10/2015 56
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
3
43
8/10/2015 57
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
2
43
8/10/2015 58
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
2
43
8/10/2015 59
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
2
43
True
8/10/2015 60
Example 2
int count;
count = 4;
do
{
P rintf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
2
432
8/10/2015 61
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
1
432
8/10/2015 62
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
1
432
8/10/2015 63
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
1
432
True
8/10/2015 64
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
1
4321
8/10/2015 65
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
0
4321
8/10/2015 66
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
0
4321
8/10/2015 67
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
0
4321
False
8/10/2015 68
Example 2
int count;
count = 4;
do
{
printf(“%d\n”,count);
count--;
} while (count > 0);
printf(“Done\n”);
Output
count
0
4321Done
8/10/2015 69
When Do You Use a "do while" Loop?
Very useful when one must ask the
question at the end of the loop
You use this when you know you want to
execute the body of the loop at least
once
Examples:
Do you want to enter another customer?
Entering a value to be edited. You enter
the value in the body of the loop, and then
after the body is completed, you check to
see if the value entered was acceptable.
Note that you must enter a value in order
to be able to have something to edit.
8/10/2015 70
Comparing "for" and "while" Statements
for (init-expr-1; bool-expr-1; incr-expr-1)
{ statement-1 }
becomes
init-expr-1
while (bool-expr-1)
{
statement-1
incr-expr-1
}
8/10/2015 72
Practice
Write a for loop that will add up the values
5 cubed plus 10 cubed plus … all the way up
to 350 cubed
Write a while loop that will read in an
integer value from the console, and verify
it is between 1 and 10. If not, it will
write an error message on the console, and
prompt the user to enter the value again.
Write a for loop that will calculate the
factorial of a number.