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

week8-loop

This document covers loops in programming, specifically the while and do-while repetition structures, along with examples and explanations of break and continue statements. It includes programming exercises that require the implementation of various loop-based tasks, such as calculating factorials, analyzing examination results, and creating a simple game. The document is designed for students at the School of Information and Communication Technology.

Uploaded by

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

week8-loop

This document covers loops in programming, specifically the while and do-while repetition structures, along with examples and explanations of break and continue statements. It includes programming exercises that require the implementation of various loop-based tasks, such as calculating factorials, analyzing examination results, and creating a simple game. The document is designed for students at the School of Information and Communication Technology.

Uploaded by

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

Week 8

Loop
Topic of this week

• Loops
• The While,do Repetition Structure
• Notes and Observations
• Continue and break
• Programming Exercises

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

2
while, do Repetition Structure

• While Statement
• The expression is evaluated. If it is true, statement is
executed and expression is re-evaluated. This cycle
continues until expression becomes false.

while (expression) {
Statement1;
Statement2;
...
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

3
while, do Repetition Structure

• Example of While

#include <stdio.h>
#define PERIOD ‘.’
int main() {
char C;
while ((C = getchar())!= PERIOD)
putchar(C);
printf(“Good Bye.\n”);
}

Result?

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

4
while, do Repetition Structure

• Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;

true
product <= 1000 product = 2 * product

false

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

5
while, do Repetition Structure

• Do-While Statement
• The do-while, tests at the bottom after
making each pass through the loop body; the
body is always executed at least once
do {
statement1;
statement2;

} while (expression);

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

6
while, do Repetition Structure

• Example of Do-While
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i <= 50);
printf(“The sum of 1 to 50 is %d\n”, sum);

Result?

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

7
while, do Repetition Structure

counter = 1;
do {
printf( "%d ", counter );
} while (++counter <= 10);

Prints the integers from 1 to 10


action(s)

true
condition

false

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

8
Continue and Break

• Break and Continue Statement


• The break statement provides an early exit from
structures including for, while, and do.
break;

• The continue statement is related to break, but


less often used; it causes the next iteration of the
enclosing for, while, or do loop to begin.
continue;

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

9
Continue and Break

• Example of Break and Continue

int c;
while ((c = getchar()) != -1) {
if (c == ‘.’)
break;
else if (c >= ‘0’ && c <= ‘9’)
continue;
else putchar(c);
}
printf(“*** Good Bye ***\n”);

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

10
Create Menu interaction
char ch;
do {
scanf(" %c", &c); //add a space before %c to remove
newline character
switch (ch) {
case 'A’:
/* do some thing */ break;
case 'B’:
/* do some thing */ break;

….
case 'Q’:
print Quit; break;
}
}while (ch!='Q');

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

11
input validation using do while

do {
printf("input n:");
scanf(&n);
if (n is not valid)
printf ("Warning\n");
}while (n is not valid);

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

12
Programming Menu
int choice;
do {
printf("Menu 1 2 3 4.. your selection:");
scanf("%d", &choice);
switch (choice){
case 1: do smt; break
case 2: do smt; break
case n: do smt; break
default: warning input not valid; break;
}
} while (choice != selection to quit);

How to clear newline character from the buffer


while (getchar()!='\n');
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology

13
Exercise 8.1

• Write a program that computes the factorial of


n (i.e., n!) using a loop.

• You can use:


• Counter variable, i, ranging from 1 to n.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

14
Exercise 8.2

• Extend the Ex 8.1.


• Write a program that has a following interface
1. Calculate n!
2. Exit
• If users select 1, the program asks users to enter a
positive number named n. If the inputted value is not
valid, the program repeats the request, until receives
a positive number. After that, it calculates n!, and
shows the result to the monitor. Finally, the program
shows the menu again
• If users select 2, then the program stops.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

16
Exercise 8.3

• Write a program that uses while structure to


analyze the examination results, how many
passed students and failed students.
• Assume that the total of students is 10. You
should ask the grade of each student (range
from 0 to 10).
• Rule
grade >=4: pass
grade<4: fail

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

18
Exercise 8.4

• Write a program that copies content inputed


from the keyboard to the screen, but replace
the sequence of blank characters by only one
blank character.
• You can use getchar() and putchar() method to
carry out this program.
• The program exits when users enter tab (\t)

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

20
Exercise 8.5

• Calculate square root by using newton method.


Program will ask user to input a number n to
calculate the square root
• Input validation: n must be positive
Method:
• xo = n
• xk+1 = (xk + n/ xk)/2
• stop when |xk+1 – xk| <= 0.0001

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

22
Exercise 8.6

• Write a program to calculate the average of a set


of grades.
• Input: a set of grades, until the inputted grade is
smaller than 0
• Output: the average of inputted grades
• Requirement: use while structure

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

24
Excercise 8.7

• Write a program with a menu as follows:


Chương trình với giao diện menu
1. Thực hiện bài tập 8.1
2. Thực hiện bài tập 8.2
3. Thực hiện bài tập 8.3
4. Thực hiện bài tập 8.4
5. Thực hiện bài tập 8.5
6. Thoát
Lựa chọn của bạn (từ 1 tới 6):
• The program will perform an activity according
to user’s choice

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

26
Exercise 8.8. SmallGame

• You are requested to write a game for 2 players. The


goal is to reach a given number (for example, 100 –
inputted from users) from the starting number
(inputted).
• In each turn, a player chooses a positive number that is
(i) less than or equal to 5 and (ii) is odd if the previous
is even, and vice versa.
• The selected value will be added to the accumulated
sum.
• The player who makes the sum equal to or greater
than the destination wins the game.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology

27

You might also like