Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Control flow

Dr. Nguyen Van Tinh


Difference between statements and blocks
• Each statement is ended with a semicolon ( ; ).
int j = 1;
prinf(“j=%d\n”,j);
• Each block is established with a couple of braces { } and without a
semicolon ;
{
++c;
A block
printf(“The number of characters = %d\n”, c);
}
• Braces are utilized with the purpose of grouping several declarations
and statements into a block.
• Generally speaking, a block is equivalent to a single statement.
If -Else
• If – else is used to show decisions. The syntax is
if (expression)
{
true_case_statements; // if the expression is true
}
else
{
false_case_statements; // if the expression is false
}
#include <stdio.h>
int main(){ If -Else
int a,b,c;
printf(“importing a from keyboard\n”);
scanf_s(“%d”, &a);
printf(“importing b from keyboard\n”);
scanf_s(“%d”, &b);
if (a > b){
c = a; // if a is greater than b.
printf(“a is greater than b. c = %d\n”, c );
}
else{
c = b; // if a is less than or equal to b.
printf(“a is less than or equal to b. c = %d\n”, c );
}
}
if (first_expression){
first_statements; // if first_expression is true.
Else - If
}
else if (second_expression) //otherwise first_expression is false
{
second_statements; // if second_expression is true.
}
else if (third_expression) // otherwise second_expression is false
{
third_statements; // if third_expression is true.
}
……………….
else { // all above cases are false
final_case_statements;
}
}
Else - If
if (a > b)
{
printf(“a is greater than b\n”); // if a > b is true.
}
else if (a==b) //otherwise a > b is false
{
printf(“a is equal to b\n”); // if a==b is true.
}
else // all two cases above are false
{
printf(“a is less than b\n”); // a < b is true.
}
}
Switch
• switch is a multi-choice
switch (expression) decision. Particularly, it checks
{ whether the expression
case first_case: statements; matches one of several
break; constant integer values.
case second_case: statements; • The default case is executed if
break; none of the other cases are
satisfied.
case third_case: statements;
break; • The break statement helps to
…… immediately exit from the
default: statements; switch statement, which
} avoids a fall-through situation.
#include <stdio.h>
int main(){
int national_number;
scanf_s(“%d”,& national_number); // importing a value from the keyboard.
switch (national_number){
case 1: printf(“USA\n”);
break;
case 84: printf(“Vietnam\n”);
break;
case 81: printf(“Japan\n”);
break;
case 86: printf(“China\n”);
break;
default: printf(“no result found\n”);
}
}
This program includes
three slides
// alibi: evidence that proves that a person was in
another place at the time of a crime
Continue
// alibi: evidence that proves that a person was in
another place at the time of a crime
Switch
• Execution is started in a certain case where the expression matches the
corresponding case value.
• All case values must be different (first_case  second_case  third_case)
• The default case is executed if none of the other cases are satisfied. In
contrast, if the default case isn’t written, no action is executed here at
all.
• The break statement helps to immediately exit from the switch
statement, which avoids a fall-through situation.
In particular, the fall-through situation is defined as follows:
the program falls through from one case to another case.
Using Enumerators as Labels
Loop- for
for (initialization; test-expression; update-expression)
{
body
}

test-expression being a bool value determines whether body is executed.


Particularly, test-expression = 1, body is executed.
Otherwise, body is ignored.
Loop- for
statement1;
for (int_expr; test_expr; update_expr)
statement2;
statement3;
Loop- for
statement1;
for (int_expr; test_expr; update_expr)
statement2;
statement3;
Loop- for
Loop- for
This loop begins by setting the integer i to 0:
i = 0;
the program tests whether i is less than 5.
i < 5;
If it is, the program executes the loop body:
printf("C program knows loops.\n");
printf("i = %d. See you soon. \n", i);
Then the program uses the loop update part of the loop to increase i by 1:
i++ ;
Loop- for
If i is diferent from 0, its bool value is equal to 1,
otherwise, its bool value is 0.
Loop- for – factorial – tính giai thừa

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

[0] [1] [2] [3] [4] [5] [i-1] [i] …


1 1 2 6 24 120 (i-1)! i!

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.

Here, the first and third expressions of for are empty.


do Loop- do while
{
statements; condition  0
} while(condition)
As long as the condition is different from
zero, the statements are executed over
and over.
Unlike the while loop, which checks the If the condition evaluates to
condition before performing statements, false, the loop terminates;
do while performs statements at least otherwise, a new cycle of
one time before checking the condition. execution and testing begins.
Loop- do while
Loop- do while
The break and continue Statements
• The break and continue statements enable a program to skip over
parts of the code.
• You can use the break statement in a switch statement and in any of
the loops such as while, for, do while. It causes program execution to
pass to the next statement following the switch or the loop.
• The continue statement is used in loops and causes a program to skip
the rest of the body of the loop and then start a new loop cycle.
Comparison between continue and break
start start
Next loop cycle
Next loop cycle
Loop or
switch
Does it
meet break? Does it meet Yes
No
continue? skip
Done full
Yes old loop
cycle No
Following
continue skip the rest of the body of the
statement
loop and then start a new loop cycle.
Break statement
Break statement right away = immediately

• break statement allows the


program to exit from for, while,
do while, and switch right away.
• break makes the closest loop or
switch be exited immediately.
Continue statement
• continue statement allows the next loop cycle
continue (iteration) of enclosing loops for, while, do
while, and switch right away.
• continue makes the closest loop go to the next
cycle immediately.
continue doesn’t work with switch statement.
#include <stdio.h> #include <stdio.h>
int main(){ int main(){
int national_number; int national_number;
scanf_s(“%d”,& national_number); scanf_s(“%d”,& national_number);
switch (national_number){ switch (national_number){
case 1: printf(“USA\n”); case 1: printf(“USA\n”);
break; // correct continue; // wrong
case 84: printf(“Vietnam\n”); case 84: printf(“Vietnam\n”);
break; // correct continue; // wrong
case 81: printf(“Japan\n”); case 81: printf(“Japan\n”);
break; // correct continue; // wrong
default: printf(“no result found\n”); default: printf(“no result found\n”);
} }
} }

You might also like