BMM1313 Computer Programming: Control Structures (Repetition)
BMM1313 Computer Programming: Control Structures (Repetition)
Computer Programming
Control Structures (Repetition)
• Aims
– Introduce students to Control Structures (Repetition): while
– Introduce students to Control Structures (Repetition): do-while
• Expected Outcomes
– Students are able to construct simple C programs that can
implement repetition control structures while
– Students are able to construct simple C programs that can
implement repetition control structures do-while
• References
– Harry H. Cheng, 2010. C for Engineers and Scientists: An
Interpretive Approach, McGraw Hill
Computer Programming
Content
Computer Programming
Control structures
Computer Programming
Control Structure – Repetition (Overview)
while (expression) {
statement
}
Computer Programming
Example 1
Output:
Computer Programming
Exercise 1a
#include <stdio.h>
int main () {
while( a >= 1 ) {
printf("%d\n", a);
a--;
}
return 0;
}
Computer Programming
Exercise 1b
#include <stdio.h>
int main () {
What is the output?
int a = 10, b;
while( a >= 1 ) {
b += a;
printf("%d\n", b);
a--;
}
return 0;
}
Computer Programming
Exercise 1c
#include <stdio.h>
int main () {
return 0;
}
Computer Programming
Exercise 1d
#include <stdio.h>
int main () {
int a, b=10, c;
• Example 2:
Write a code that calculates the factorial of n
where n is the user input using while loops
Computer Programming
Control Structure (Repetition): while
Computer Programming
Control Structure (Repetition): while
Computer Programming
Example 1: Nested while-loop
Computer Programming
Exercise 2
#include <stdio.h>
int main () {
int i=0, j;
Modify this code to produce the
while(i<10){ following output.
j=0;
while(j<10){
printf("%4d ",j);
j++;
}
i++;
printf("\n");
}
return 0;
}
Computer Programming
Exercise 4
#include <stdio.h>
int main () {
int i=0, j=0, k;
while(i<5){ Write the output of the code.
while(j<5){
printf("*",j);
k=j;
while(k>0){
if(k%2==0){
printf("+");
}
else{
printf("-");
}
k--;
}
j++;
}
i++;
j=i;
printf("\n");
}
return 0;
}
Computer Programming
#include <stdio.h>
Exercise 5
int main(){
int a=6, b, c;
while(a>=0){
c=a%2; Write the output of the code.
b=0;
switch(c){
case 0:{
while(b<6){
if(b<a){printf("%4d", b);}
else{
printf("%4d", b+1);
b++;}
b++;}
break;}
case 1:{
while(b<6){
if(b>a){printf("%4d", b);}
else{
printf("%4d", b+1);
b++;}
b++;}
break;}}
printf("\n");
a--;}
return 0;}
Computer Programming
Control Structure (Repetition): do-while
statement
} while (expression);
Computer Programming
Example 1: do-while loop
Computer Programming
SO? What is the difference??
Computer Programming
while loop vs do-while loop
Both of the code are almost similar but will produce different output.
Computer Programming
Example application of do-while loop
Computer Programming
Exercise 6
Computer Programming
Conclusion
while(condition){
statement
}
do{
statement
}while(condition);
Computer Programming
BMM1313 Computer Programming
Lecture 6
Computer Programming