control structures - wordpress.comapr 02, 2011 · control structures nguyen dung falculty of...
TRANSCRIPT
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;}
exp
B
nonzero
zero
3
if statement
Example:
int a = 5, b = 9, max;
max = a;
if ( max < b ){
max = b;}
printf(“Max is: %d”,max);
Max is: 9
if (max < b)
max = b;
4
if statement
Syntax 2nd:
if ( exp ) {B1;}
else {B2;}
exp
B1
nonzero zero
B2
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:
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ữ.
Đ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
7
exp==
exp1
exp==
exp2
…
exp==
expn
B0;
B1;
B2;
Bn;
Zero
Zero
Zero
Zero
Non-Zero
Non-Zero
Non-Zero
8
switch statement
Syntax:
switch (exp)
{
case (exp1):
B1; break;
case (exp2):
B2; break;
...
case (expN):
BN; break;
[default: B0;]
}
expression
Constant-expression
9
Example:
void main()
{ 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();
}
Nhap so: 6
Sau
10
while .. do statement
Syntax:
while (exp)
{
B;
} exp B;
Zero Non-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{
a = x;b = y;
while(a != b){
if (a > b)
a -= b;
else
b -= a;
}
printf(“GCD(%d,%d): %d\n", x, y, a);
printf(“LCM(%d,%d) is: %d\n",x, y,(x*y)/a);
}
unsigned int r = 0;
while(b!=0){
r = a % b;
a = b;
b = r;
}
13
do .. while statement
Syntax:
do
{
B;
}
while (exp);
exp
B;
Zero
Non-Zero
Different with
while statement??? 14
Example
#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);
}
Enter x: 6
6 is even
Press ESC to exit...
Enter x: 7
7 is odd
Press ESC to exit...
15
for statement
Syntax:
for(exp0;exp1;exp2)
{
B;
} exp1
exp0;
Zero
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>
#include <conio.h>
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();
}
Enter n: 6
Sum is: 21
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;
}
printf(“s = %d”,s);
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;
}
printf(“s = %d”,s);
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