Lecture 4-11327
Lecture 4-11327
Zagazig University
Fall 2023
Lecture #4
Dr. Ahmed Amer Shahin
Dept. of Computer & Systems Engineering
These slides are adapted from the slides accompanying the text “C How to Program, 8/e,” https://deitel.com/c-how-to-program-8-e/
Copyright Deitel 2016
Announcements
• Faculty Platform:
– Tutorial 1 Solution is uploaded (provided by Eng.
Mahmoud)
• Tutorials/Practical Sessions:
– Next week we will start a new round
Outline
• A C Program to Process User Data
• Iteration Essentials
• Iteration Structures in C
• The while Iteration Statement
• The do ... While Iteration Statement
• The for Iteration Statement
• Nested Loops
• break and continue Statements
• Infinite Loops
3
Lecture Map
4
A C PROGRAM TO PROCESS USER
DATA
A C Program to Process User Data
6
A C Program to Process User Data
#include <stdio.h>
int main(void)
{
int n1, n2, n3, n4;
printf("Enter a number:\n");
scanf("%d", &n1);
n1 += 3;
printf("The number after processing is %d\n", n1);
printf("Enter a number:\n");
scanf("%d", &n2);
n2 += 3;
printf("The number after processing is %d\n", n2);
printf("Enter a number:\n");
scanf("%d", &n3);
n3 += 3;
printf("The number after processing is %d\n", n3);
printf("Enter a number:\n");
scanf("%d", &n4);
n4 += 3;
printf("The number after processing is %d\n", n4);
return 0;
}
7
A C Program to Process User Data
8
ITERATION ESSENTIALS
9
Iteration Essentials
• A loop is a group of instructions the computer
executes repeatedly while some loop-continuation
condition remains true.
• There are two means of iteration:
– Counter-controlled iteration
– Sentinel-controlled iteration
• Counter-controlled iteration is sometimes called
definite iteration because we know in advance
exactly how many times the loop will be executed.
• Sentinel-controlled iteration is sometimes called
indefinite iteration because it’s not known in
advance how many times the loop will be executed.
13
Iteration Structures In C
16
The while Iteration Statement
• An iteration statement allows you to specify that an action is to
be repeated while some condition remains true.
• The pseudocode statement
While there are more assignment to do
do next assignment and submit it
• describes the iteration that occurs during a normal student week.
– The condition, “there are more assignment to do” may be true or
false.
– If it’s true, then the action, “do next assignment and submit it” is
performed.
– This action will be performed repeatedly while the condition
remains true.
– The condition will become false, if no more assignments.
– At this point, the iteration terminates, and the first statement after
the iteration structure is executed.
product = 3;
while (product <= 100)
{
product = 3 * product;
} // end while
int main(void)
{
int product;
product = 3;
return 0;
}
// initialization phase
total = 0; // initialize total
counter = 1; // initialize loop counter Pseudocode for the problem
// processing phase
while (counter <= 10)
{
printf("%s", "Enter grade: "); // prompt for input
scanf("%d", &grade); // read grade
total = total + grade; // add grade to total
counter = counter + 1; // increment counter
} // end while
// termination phase
average = total / 10; // integer division
int main(void) {
unsigned int counter; // number of grades entered
int grade = 0; // grade value
int total = 0; // sum of grades
float average; // number with decimal point for average
// processing phase
// get first grade from user
printf("%s", "Enter grade, -1 to end: "); // prompt for input
scanf("%d", &grade); // read grade
// termination phase
// if user entered at least one grade
if (counter != 0) {
// calculate average of all grades entered
average = (float)total / counter; // avoid truncation
29
do…while Iteration Statement
• The do…while iteration statement is similar
to the while statement.
• In the while statement, the loop-continuation
condition is tested at the beginning of the loop
before the body of the loop is performed.
• The do…while statement tests the loop-
continuation condition after the loop body is
performed.
• Therefore, the loop body will be executed at
least once.
© 2016 Pearson Education, Ltd. All rights reserved. 30
do…while Iteration Statement
(Cont.)
• Consider the following program that prints the numbers
from 1 to 10
#include <stdio.h>
int main(void)
{
unsigned int counter = 1;
do
{
printf("%u ", counter);
} while (++counter <= 10);
}
32
for Iteration Statement
• The for iteration statement handles all the details of
counter-controlled iteration.
• To illustrate its power, let’s rewrite the following
program using for iteration statement
#include <stdio.h>
int main(void)
{
unsigned int counter = 1; // initialization
int main(void)
{
// initialization, iteration condition, and increment
// are all included in the for statement header.
for (unsigned int counter = 1; counter <= 10; ++counter)
{
printf("%u\n", counter);
}
}
#include <stdio.h>
int main(void)
{
unsigned int sum = 0; // initialize sum
41
Nested Loops
int main(void)
{
for (int i = 1; i <= 12; i++)
for (int j = 1; j <= 12; j++)
printf("%d * %d = %d\n", i, j, i * j);
}
43
break and continue Statements
• The break and continue statements are
used to alter the flow of control.
• break Statement
– The break statement, when executed in a while,
for, do…while or switch statement, causes an
immediate exit from that statement.
– Program execution continues with the next
statement.
– Common uses of the break statement are to
escape early from a loop or to skip the remainder of
a switch statement
© 2016 Pearson Education, Ltd. All rights reserved. 44
break and continue Statements (Cont.)
#include <stdio.h>
int main(void)
{
unsigned int x; // declared here so it can be used after loop
// loop 10 times
for (x = 1; x <= 10; ++x)
{
// if x is 5, terminate loop
if (x == 5)
{
break; // break loop only if x is 5
}
printf("%u ", x);
}
printf("\nBroke out of loop at x == %u\n", x);
}
• continue Statement
– The continue statement, when executed in a while,
for or do…while statement, skips the remaining
statements in the body of that control statement and
performs the next iteration of the loop.
– In while and do…while statements, the loop-
continuation test is evaluated immediately after the
continue statement is executed.
– In the for statement, the increment expression is
executed, then the loop-continuation test is evaluated.
– A program that uses the continue statement in a for
statement to skip the printf statement and begin the
next iteration of the loop.
int main(void)
{
// loop 10 times
for (unsigned int x = 1; x <= 10; ++x)
{
// if x is 5, continue with next iteration of loop
if (x == 5)
{
continue; // skip remaining code in loop body
}
printf("%u ", x); // display value of x
}
puts("\nUsed continue to skip printing the value 5");
}
48
Infinite Loops
• In some cases, we may need to let the loop run
without stopping
– A for loop without the loop-continuation condition will
run indefinitely.
for (int x = 1; ; ++x)
51