Control Structures
Nguyen Dung
Falculty of Information Technology
Hue University of Science
Content
Selection
if statement
switch statement
Iteration (loop)
for statement
while statement
do … while statement
Jump:
break
continue
return
goto
2
if statement
Syntax 1st:
if ( expression ) {B;}
zero
exp
nonzero
B
3
if statement
Example:
int a = 5, b = 9, max;
if (max < b)
max = b;
max = a;
if ( max < b ){
max = b;}
printf(“Max is: %d”,max);
Max is: 9
4
if statement
Syntax 2nd:
if ( exp ) {B1;}
else {B2;}
zero
nonzero
exp
B2
B1
5
if statement
float a, b, x;
printf(“a = ");scanf("%f",&a);
printf(“b = ");scanf("%f",&b);
if (a == 0 ){
if ( b != 0 )
printf(“Equation hasn’t root.”);
else
printf(“Equation has countless
root.”);
}
else{
x = -b/a;
printf(“Root of equation is x = %f”,x);
}
a = 2
b = 6
Root of equation is x = -3.00
6
Excercises
1. Xác định xem một số nguyên là chẵn hay lẻ.
2. Xác định học lực dựa vào điểm trung bình
của sinh viên, biết:
ĐTB
[0, 4)
[4, 5)
[5, 6.5)
[6.5, 8)
[8, 9)
[9, 10]
Học lực
Kém
Yếu
Trung bình
Khá
Giỏi
Xuất sắc
3. Xác định số có giá trị lớn hơn trong hai số
thực a, b theo 2 cách: sử dụng lệnh if và
không sử dụng lệnh if
4. Hiển thị một số tự nhiên bất kì từ 0 đến 9
dưới dạng chữ.
7
exp==
exp1
B1;
Non-Zero
Zero
exp==
exp2
B2;
Non-Zero
Zero
…
Zero
exp==
expn
Bn;
Non-Zero
Zero
B0;
8
switch statement
Syntax:
expression
switch (exp)
{
case (exp1):
B1; break;
case (exp2):
B2; break;
...
case (expN):
BN; break;
[default: B0;]
}
Constant-expression
9
Example:
void main()
{
Nhap so: 6
Sau
int x;
printf("Nhap so: ");
scanf("%d", &x);
switch(x){
case 0: printf(“Khong”);break;
case 1: printf(“Mot”);break;
case 2: printf(“Hai”);break;
case 3: printf(“Ba”);break;
case 4: printf(“Bon”);break;
case 5: printf(“Nam”);break;
case 6: printf(“Sau”);break;
case 7: printf(“Bay”);break;
case 8: printf(“Tam”);break;
case 9: printf(“Chin”);break;
}
getch();
}
10
while .. do statement
Syntax:
while (exp)
{
B;
}
exp
B;
Non-Zero
Zero
11
Example
Find “greatest common divisor” of a and b.
Then find “least common multiple” of a and
b
Input: a, b
Output: GCD(a,b) and LCM(a,b)
Method:
𝑎 𝑖𝑓 𝑏 = 𝑎
𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑎 − 𝑏, 𝑏 𝑖𝑓 𝑎 > 𝑏
𝐺𝐶𝐷 𝑎, 𝑏 − 𝑎 𝑖𝑓 𝑎 < 𝑏
𝑎 𝑖𝑓 𝑏 = 0
𝐺𝐶𝐷 𝑎, 𝑏 =
𝐺𝐶𝐷 𝑏, 𝑏 𝑚𝑜𝑑 𝑎 𝑖𝑓𝑏 ≠ 0
𝑎𝑏
𝐺𝐶𝐷 𝑎, 𝑏 =
𝐿𝐶𝑀 (𝑎, 𝑏)
12
Implement
unsigned int x, y, a, b;
printf("Nhap x, y: ");
scanf("%d,%d", &x, &y);
if (x == 0 && y == 0)
printf(“Both a and b are 0\n");
else if (x * y == 0){
printf(“GCD(%d,%d) is: %d\n", x, y, x+y);
}
else{
unsigned int r = 0;
a = x;b = y;
while(b!=0){
while(a != b){
r = a % b;
if (a > b)
a = b;
a -= b;
b = r;
else
}
b -= a;
}
printf(“GCD(%d,%d): %d\n", x, y, a);
printf(“LCM(%d,%d) is: %d\n",x, y,(x*y)/a);
}
13
do .. while statement
Syntax:
do
{
B;
B;
}
while (exp);
Non-Zero
exp
Zero
Different with
while statement???
14
Example
Enter x: 6
6 is even
Press ESC to exit...
Enter x: 7
7 is odd
Press ESC to exit...
#include <stdio.h>
#include <conio.h>
void main()
{
int x; char c;
do{
printf(“\nEnter x: ”);
scanf(“%f”, &x);
if (x%2==0){
printf(“%d is even”,x);
}
else
printf(“%d is odd”,x);
printf(“\nPress ESC to exit... ”);
c = getch();
} while (c!=27);
}
15
for statement
Syntax:
for(exp0;exp1;exp2)
{
B;
}
exp0;
Zero
exp1
Non-Zero
B;
exp2;
16
Example
Find the following sum:
S = 1 + 2 + 3 + ... + n =
𝒏
𝒊=𝟏 𝒊
Input: n
Output: sum of n number from 1 to n
Method:
s = 0;
For i = 1 to n do
s = s + i;
17
Implement
#include <stdio.h> Enter n: 6
#include <conio.h> Sum is: 21
void main()
{
int n, s=0;
printf(“Enter n: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++)
s = s + i;
printf(“Sum is: ", s);
getch();
}
18
break, continue, goto
The break command allows you to terminate
and exit a loop (that is, do, for,
and while) or switch command from any point
Example
int s = 0;
for(int i = 0; i <= 10; i++){
if (i == 5)
break;
s = s + i;
s= ???
}
printf(“s = %d”,s);
19
break, continue, goto
The continue statement jumps the next
loop.
Example
int s = 0;
for(int i = 0; i <= 10; i++){
if (i == 5)
continue;
s = s + i;
S = ???
}
printf(“s = %d”,s);
20
break, continue, goto
Goto: self-study
21
return
The return statement:
terminates the execution of a
function
or return a result(address or value)
to the calling function and exit
function.
Syntax:
return exp;
22
Warning
Homework
Please visit website
to get it.
23
Questions
24
The end
25