DO-WHILE PSEUDO-CODE EXAMPLES:
Example1:
1. do {
1.1 prompt for a number > 0
1.2 input number
1.3 if(number <= 0)
1.3.1 display error: number <= 0
} while(number <= 0)
2. display log10(number)
3. display loge(number)
4. display sqrt(number)
Example2:
1. count ß 1
2. sum ß 0
3. do{
3.1 prompt for num
3.2 input num
3.3 sum ß sum + num
3.4 count ß count + 1
} while( count < 10)
4. display sum
5. stop
WHILE PSEUDO-CODE EXAMPLES:
Example 1:
1. count ß 0
2. sum ß 0
3. while(count < 20){
3.1 prompt for num
3.2 input num
3.3 sum ß sum + num
3.4 count ß count + 1
}
4. display sum
5. stop
Example2: Find the average, maximum, and minimum of a number of grades (Sentinel controlled loop):
1. sum ß 0
2. count ß 0
3. prompt for grade (>= 0)
4. input grade
5. max ß grade
6. min ß grade
7. while(grade >= 0){
4.1 sum ß sum + grade
4.2 count ß count + 1
4.3 if(grade > max)
4.3.1 max ß grade
else if(grade < min)
4.3.2 min ß grade
4.4 prompt for grade (>= 0)
4.5 input grade
} // end while
8. if(count == 0)
8.1 display error
else{
8.2 average ß sum / count
8.3 display average
8.4 display max
8.5 display min
}
9. stop
FOR PSEUDO-CODE EXAMPLES
Example1: Each of 20 students has done 4 quizzes. Find the class average and the average of
each student.
1. classTotal ß 0
2. for 20 times{
2.1 studentTotal ß 0
2.2 for 4 times {
2.2.1 prompt for student grade
2.2.2 studentTotal ß studentTotal + grade
}
2.3 studentAverage ß studentTotal / 4
2.4 display studentAverage
2.5 classTotal ß classTotal + studentTotal
}
classAverage ß classTotal / 80
display classAverage
Note: For-loops in which the increment/decrement is not one are better written as while loops in pseudo-code