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

BMM1313 Computer Programming: Control Structures (Repetition)

This document discusses control structures for repetition in C programming, specifically the while and do-while loops. It provides examples of using while and do-while loops to print output and calculate values repeatedly until a condition is met. It also discusses the differences between while and do-while loops. Exercises are provided for students to construct C programs using these loops to produce sample outputs.

Uploaded by

HarithZakaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

BMM1313 Computer Programming: Control Structures (Repetition)

This document discusses control structures for repetition in C programming, specifically the while and do-while loops. It provides examples of using while and do-while loops to print output and calculate values repeatedly until a condition is met. It also discusses the differences between while and do-while loops. Exercises are provided for students to construct C programs using these loops to produce sample outputs.

Uploaded by

HarithZakaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

BMM1313 Computer Programming

Control Structures (Repetition)


while and do-while

Faculty of Mechanical Engineering

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

• Selection Structures: while


• Selection Structures: do-while
• Nested Loops
– Nested while
– Nested do-while
• Examples
• Conclusion

Computer Programming
Control structures

• There are 3 control structures for C programs:


1. Sequence
• Each statement is executed sequentially (as seen in the
previous lectures
2. Selection
• One statement is selected over another depending on a
Selection
– If, else if, else & switch
– If var1 > 10, do this…, else do that…
3. Repetition
• Statements are repeatedly executed until it meets a
certain condition
– for, while, do-while loops

Computer Programming
Control Structure – Repetition (Overview)

• Three kinds of selections structures


– while Loop
– do-while Loop
– for Loop
• Loops Consist of
– 1: Loop Initialization
– 2: Terminating Condition (Loop Guard)
– 3: Loop Body (Statement)
– 4: Terminating Action
Computer Programming
Before we go to while loop…

• We need to know additional arithmetic


operations.
• Let i and a are integer; i = 1, a = 2.
C code Operation Example
i++ i = i+1 i = i+1 = 1+1 = 2
*now i=2
i-- i = i-1 i = i-1 = 1-1 = 0
*now i=0
i+=a i = i+a i = i+a = 1+2 = 3
*now i=3
• There are a lot more ( -= , *= , /= . %= )
Computer Programming
Control Structure (Repetition): while

• The flow chart and the syntax of a while loop is


given as follows where statement will be
executed until expression is FALSE.

while (expression) {
statement
}

Computer Programming
Example 1
Output:

What happen if there is no a++?


Can you describe the code using flowchart?

Computer Programming
Exercise 1a
#include <stdio.h>

int main () {

int a = 10; What is the output?

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 () {

int a = 10, b=0;

while( a >= 1 ) { a) What is the output?


b++; b) What is the output if the line
if(a%3==0){ if(a%2==0){ is replaced with
printf("%d\n", b);
else if(a%2==0){
}
if(a%2==0){
printf("%d\n", a);
}
else{
printf("%d\n", a+b);
}
a--;
}

return 0;
}

Computer Programming
Exercise 1d
#include <stdio.h>

int main () {

int a, b=10, c;

printf("Enter value a:\n"); What is the output when user enter:


scanf("%d", &a); a) a=20
b) a=0
if(a>0){
while(a>b){
c = a-b;
printf("%d\n", c);
a--;}
}
else{
while(a<b){
c = a+b;
printf("%d\n",c);
a++;}
}
return 0;
}
Computer Programming
Control Structure (Repetition): while

• Example 2:
Write a code that calculates the factorial of n
where n is the user input using while loops

For example: If the use inputs n = 5, then the


code will return 120 since
5! = 1*2*3*4*5 = 120

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

• The flow chart and the syntax of a do-while


loop is given as follows where statement will
be executed until expression is FALSE.
do{

statement

} while (expression);

Computer Programming
Example 1: do-while loop

Computer Programming
SO? What is the difference??

• What is the difference between while and do-


while loop?
– while  your condition is at the beginning of the
loop (it is possible to never enter the loop)
– do-while  your condition is at the end of the
loop (the code must enter the loop at least once)

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

• Modify the code below so that it will produce


the following output.

Computer Programming
Conclusion

while(condition){
statement
}

do{
statement
}while(condition);
Computer Programming
BMM1313 Computer Programming

Lecture 6

Computer Programming

You might also like