0% found this document useful (0 votes)
23 views

Lecture 2 - Iterative Control

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 2 - Iterative Control

Uploaded by

frankkinunghi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIVERSITY OF DAR ES SALAAM

PROGRAMMING IN C

Name

Dr Masoud H. Mahundi

Affiliation

Department of Computer Science and Engineering


University of Dar es Salaam

Emails
[email protected], [email protected]

Phone Numbers
+255713832252, +255768832424
ITERATIVES
For, while, and Do-While
Introduction
▪ Another structure to control the execution flow

▪ Allows for execution of a statement or a block of, several times

▪ Often referred to as loops

▪ Examples
▪ Prompt for PIN in the ATMs – three times

▪ When entering similar information for a group of records – e.g. students records

▪ Number of prompts will be equal to number of students

▪ C has three ways of doing this

▪ for loops, while and do…while loops


Introduction
Entry
(initialisation) Statement(s) to be
executed repetitively
Introduction
▪ In each of the loop a programmer needs to decide on three things
▪ Initialization – otherwise called the entry point of the loop, which tells how the loop will
start the iteration.

▪ Shift - an increment or decrement – this tells how, in the course of execution, the program
will jump from iteration to the next

▪ The Condition – or exit strategy; this gives the detail of how the execution will come out
of the loop. The intension of the loop is to perform some executions repeatedly. The
condition says when the loop should stop this repetition

▪ There might be different terms in literature but might mean a similar thing
For loop
▪ For loop often used when the beginning and the end of the loop are known

▪ They can also come from the user for (initialisation; condition; shift)
{
Statements;
………………..
}

Example 1 Example 2
1. #include<stdio.h> 1. #include<stdio.h>
2. int i; 2. int i;
3. main() { 3. main() {
4. for(i = 1; i <= 10; i = i + 1) { 4. for(i = 1; i <= 10; i = i + 1) {
5. printf(“Printing 10 Times»); 5. printf(“Item #%d», i);
6. } 6. }
7. } 7. }
For loop
▪ The next iteration may be by reducing the previous value of the control

Example 3 Example 4
1. #include<stdio.h> 1. #include<stdio.h>
2. main() { 2. main() {
3. int i, j = 8; 3. int curr, k=10;
4. for(i = 1; i <= 12; i = i + 1) { 4. printf("\n");
5. printf("%d x %d = %d\n", i, j, j*i); 5. for(curr = 5; curr >= 1;curr--)
6. } 6. printf("\t %d",curr*k)
7. } 7. }
The while loop
▪ Can be used in place of for loop
▪ Can also be used when the number of iterations is not necessarily known
▪ The three elements (initialisation, condition and shift) are not found in one line
Example 1
1. #include<stdio.h>
initialization 2. main() {
while(condition) 3. int i, j = 8;
{ 4. printf(“ Times 8 Table\n");
Statements; 5. i = 1;
……………….. 6. while(i <= 12) {
Shift 7. printf(“%d x %d = %d\n", i, j, j*i);
} 8. i = i + 1;
9. }
10. }
The while loop
WHAT WILL BE THE OUTPUT OF THIS

1. #include<stdio.h>
2. main() {
3. int ctr, k=10;
4. printf("\n");
5. ctr = 5;
6. while(ctr >= 1){
7. printf("\t %d",ctr*k)
8. ctr--;
9. }
Sometimes one doesn’t know the end
1. #include<stdio.h>
2. main(){
3. int result = 0;
4. int count = 0;
5. int sum = 0;
6. while(result!=-1){
7. sum = sum + result;
8. printf("Enter Results ");
9. scanf("%d",&result);
10. count++;
11. }
12. printf("# of elements: %d \nThe Sum: %d", count-1, sum);
13. }
The do … while loop
▪ In the do … while there will be at least one execution
▪ Since the first iteration is done first before testing the condition
▪ In the while loop the condition is tested firs

initialisation
do
{
statement;
shift;
}
While (condition);
The do … while loop
▪ In the do … while there will be at least one execution

1. #include <stdio.h>
2. main()
3. {
4. printf("Enter the Entries for Max or 0 for the End\n");
5. int result, count = 1;
6. do
7. {
8. printf("Entry Number %d\t", count);
9. scanf("%d",&result);
10. printf("\tYour Entry was ------ %d-------", result);
11. count++;
12. printf("\n");
13. } while(result !=0);
14. printf("Thanx for the Exercise");
15. printf("\n");
16. }
break and continue
▪ They regulate the execution of iterations in a loop
▪ There are times we would want the loop not to finish all the iterations and break out of it
▪ There are, as well, times when we want some iteration to be skipped and continue with
others
▪ They are used in all types of loops; for, while and do while
1. #include <stdio.h> 1. #include <stdio.h>
2. main(){ 2. main(){
3. int i, sum = 0; 3. int i, sum = 0;
4. for(i=0; i<10; i++){ 4. for(i=0; i<10; i++){
5. if(i==5) 5. if(i%2 != 0)
6. break; 6. continue;
7. sum = sum + i; 7. sum = sum + i;
8. } 8. }
9. printf(" The sum is %d\n", sum); 9. printf(" The sum is %d\n", sum);
10. } 10. }

You might also like