Professional Documents
Culture Documents
3 - Control Flow - Updated
3 - Control Flow - Updated
0! = 1
1! = 1
2! = 2 x 1
3! = 3 x 2 x 1
4! = 4 x 3 x 2 x 1
5! = 5 x 4 x 3 x 2 x 1
6! = 6 x 5 x 4 x 3 x 2 x 1
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1
8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
factorial
factorial[2] = 2 x factorial[1]
factorial[3] = 3 x factorial[2] = 3 x 2 = 6
factorial[4] = 4 x factorial[3] = 4 x 6 = 24
factorial[5] = 5 x factorial[4] = 5 x 24 = 120
…..
factorial[i] = i x factorial[i-1] = i x (i-1)! = i!
Loop- for – factorial – tính giai thừa
Loop- for – the triangle of stars
// New line
Loop- while
while (expression)
{
statements;
} expression 0
As long as the expression is different from
zero, the statements are executed over and
over.
Loop- while
The while loop is a for loop without the initialization and update parts;
while-loop has just a test-condition and a body:
while (test-condition)
body
• First, a program evaluates the parenthesized test-condition expression.
• If the expression evaluates to true, the program executes the statement(s)
in the body.
• After it finishes with the body, the program returns to the test condition.
• If the condition is true, the program executes the body again.
• This cycle of testing and execution continues until the test condition
evaluates to false.
statement1
while (test_expr)
statement2
statement3
Example: Import your name from Keyboard
\0, aka null character, is the final item of name[ArSize] or is the end of a
string.
\0 is automatically added to name[ArSize] when you import your name from
Keyboard.
name[ArSize]
Use while loop to deal with your name.
T h a n h \0
for Versus while
Two blocks of statements are equivalent to each other.
for Versus while
Two blocks of statements are equivalent to each other.