More Related Content
More from เทวัญ ภูพานทอง (20)
คำสั่งวนซ้ำ
- 3. i++ = i
= i--1 =
i+ i = i-
1
i+=5 = i=
i+i-=5 =
5 i = i-
5 3
- 4. while
while (condition) START
{
stmt1;
stmt2;
: false
condition
stmtN;
} true
Statement
• stmt1
stmtN Statement
condition
END
4
- 5. Counting
Loop)
•
•
int i, sum = 0;
i = 1;
while (i <= 10)
{
sum = sum + i;
i = i + 1;
}
printf("Sum = %dn", sum);
5
- 6. while
#include <stdio.h>
int main()
while1.c
{ Hello 1
Hello 2
int i=1; Hello 3
while(i<=10) Hello 4
{ Hello 5
Hello 6
printf("Hello %dn",i); Hello 7
i++; Hello 8
} Hello 9
Hello 10
getch();
return 0;
} 6
- 7. while
(INFINITY LOOP)
#include <stdio.h>
int main() while2.c
{ Hello 1
int i=1; Hello 2
while(1) Hello 3
{ Hello 4
printf("Hello %dn",i); Hello 5
if(i==10) Hello 6
Hello 7
break; Hello 8
i++; Hello 9
} Hello 10
getch();
return 0;
} 7
- 8. do…while
do START
{
stmt1;
stmt2;
: Statement1
stmtN;
} while (condition);
StatementN
• stmt1...stmtN
true
condition
condition false
•
END
stmt1...stmtN
8
- 9. do…while
#include <stdio.h>
int main() dowhile1.c
{ Hello 1
int i=1; Hello 2
do Hello 3
{ Hello 4
Hello 5
printf("Hello %dn",i); Hello 6
i++; Hello 7
} Hello 8
while(i<=10); Hello 9
getch(); Hello 10
return 0;
}
9
- 10. for
•
for (init_stmt; condition; update_stmt)
{
statement1;
statement2;
:
statementN;
}
•
1. init_stmt
2. condition
statement1...statementN
10
- 11. for
START
init_stmt
for (init_stmt; condition; update_stmt)
{
statement1;
false
statement2; condition
:
statementN;
}
true
Statement1
StatementN
update_stmt
END
11
- 12. for
#include <stdio.h>
int main() for1.c
{ Hello 1
Hello 2
int i; Hello 3
for(i=1;i<=10;i++) Hello 4
{ Hello 5
Hello 6
printf("Hello %d",i); Hello 7
printf("n"); Hello 8
} Hello 9
Hello 10
getch();
return 0;
} 12