Upload
warawut
View
592
Download
0
Embed Size (px)
DESCRIPTION
Control Structure
Citation preview
CHAPTER 3
CONTROL STRUCTUREMr.Warawut Khangkhan
e-Mail: [email protected]
Social Media: www.facebook.com/AjWarawut
������������ ����������������������������������� �����������������������
�������������� ������� ��� (Sequence
Control Statement)
�������������� ������������ (Selection
Control Statement)
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
Control Statement)
�������������� ����� �� (Iteration
Control Statement)
2
Control Structure
SEQUENCE
CONTROL
Mr.Warawut Khangkhan
Chapter 3 Control Structure
CONTROL
STATEMENT
Control Structure
3
����� ��������������������������� ����������������������
�� ����� ������ ���
�������� ��� �!
�����"� ���#"$%����&� '���(�&�������&�
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
����"� ���#"$%����&� '���(�&�������&�
4
Control Structure
SELECTION
CONTROL
Mr.Warawut Khangkhan
Chapter 3 Control Structure
CONTROL
STATEMENT
Chapter 3 Control Structure
5
����� ����������������������������� ������������������������
�� �����������#������������� �������� �
��%)����������� �� 4 �%)���
� if statement
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
� if…else statement
� nested if statement
� switch statement
6
Control Structure
True or False
IFIF STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
7
Control Structure
IFIF STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
if (boolean_expression) {
statements;
}
8
Control Structure
boolean_expression = � ��������� �������!����
����������
statements = "����������������������������!��� ��� �
EEXX. C. CODEODE IIFF SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
9
Control Structure
IFIF……ELSEELSE STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
10
Control Structure
IFIF……ELSEELSE STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
if (boolean_expression) {
statement-1;
} else {
statement-2;
11
Chapter 3 Control Structure
statement-2;
}
boolean_expression = � ��������� �������!����
����������
statement-1 = "����������������������������!��� ��� �
statement-2 = "����������������������������!��� ���#�
EEXX. C. CODEODE IIFF…E…ELSELSE
SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
12
Control Structure
NESTEDNESTED IFIF STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
13
Chapter 3 Control Structure
NESTEDNESTED IFIF STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
if (boolean_expression_1) {
statement-1;
} else if (boolean_expression_2) {
statement-2;
14
Control Structure
statement-2;
} else {
statement-3;
}
EX
. CO
DE
NE
ST
ED
ST
AT
EM
EN
T
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
ES
TE
DIF
15
Chapter 3 Control Structure
SWITCHSWITCH STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
16
Control Structure
SWITCHSWITCH STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
switch (expression) {
case list_value_1:
statement-1;
break;
case list_value_2:
17
Control Structure
case list_value_2:
statement-2;
break;
…
[default:
statement;
}
EEXX
. C. C
OD
EO
DE
SSW
ITC
HW
ITC
H
SST
AT
EM
EN
TT
AT
EM
EN
T
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
WIT
CH
WIT
CH
18
Control Structure
ITERATION
CONTROL
Mr.Warawut Khangkhan
Chapter 3 Control Structure
CONTROL
STATEMENT
Control Structure
19
����� �����������������$%������� �����������������$%��
�� �������%) (� �� ) #���������)+���,# (False)
� while statement
� do…while statement
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
� for statement
20
Control Structure
WHILEWHILE STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
21
Control Structure
while (boolean_expression) {
statements;
}
EEXX. C. CODEODE WWHILEHILE SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
22
Control Structure
DODO……WHILEWHILE STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
23
Control Structure
DODO……WHILEWHILE STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
do {
statements;
} while (boolean_expression);
24
Control Structure
EEXX. C. CODEODE DDOO…W…WHILEHILE
SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
25
Control Structure
FORFOR STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
26
Control Structure
FORFOR STATEMENTSTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
for (ctrl_var = val; bool_exp; inc./dec.) {
statements;
}
27
Control Structure
EEXX. C. CODEODE FFOROR SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
28
Chapter 3 Control Structure
BREAK &
CONTINUE
Mr.Warawut Khangkhan
Chapter 3 Control Structure
CONTINUE
STATEMENT
Control Structure
29
BREAKBREAK & & CONTINUECONTINUE
STATEMENTSTATEMENT
�� ��� break �)+�� ��� -�-&����� �
�� ��� continue �)+�� ��� -������� �
Mr.Warawut
Khangkhan
Chapter 3 Control Structure
30
Control Structure
EEXX. C. CODEODE BBREAKREAK SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
31
Control Structure
EEXX. C. CODEODE CCONTINUEONTINUE
SSTATEMENTTATEMENT Mr.Warawut
Khangkhan
Chapter 3 Control Structure
32
Control Structure