Practice Session 2 - Control Flow Statements in C & Applications

wethanhcong 4 views 28 slides Oct 22, 2025
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

This session focuses on understanding and applying control flow statements in the C programming language. Students will practice using conditional statements (if, if-else, nested if, switch-case) and looping constructs (for, while, do-while) to control the execution of programs. The session emphasiz...


Slide Content

Nguyễn Trung Hiếu
Practice Session II
IT1016 – 738793

Control Flow Statements

CFS: Introduction
Fundamental constructs in C.
Enable the execution of different code blocks based on
certain conditions.
Good understanding → Efficient, structured code.

CFS: Types
Conditional Statements
Looping Statements
Jumping Statements

Conditional Statements

CS: Types
if-else
switch
(condition) ? do_if_true : do_if_false

CS: if
if (cond) {
do_if_true();
}
other_code();

CS: if-else
if (cond) {
do_if_true();
} else {
do_if_false();
}
other_code();

CS: nested if-else
if (cond1) {
do_if_true();
if (cond2) { do_if_true_T(); }
else { do_if_true_F(); }
}
else { … }

CS: if-else if-else
if (cond1) { do_if_1_true(); }
else if (cond2) { do_if_2_true(); }

else (cond3) { … }

CS: switch
switch (expression) {
case value1: statements;
case value2: statements;

default: statements;
}

CS: Ternary Operator
(condition) ? [true_statements] :
[false_statements] ;

Looping Statements

LS: for
for (init; test; update)
{

}

LS: nested for
for (init; test; update)
{
for (init2; test2; update2)
{ … }
}

LS: while
while (condition)
{
do_something();
}

LS: while (1)
while (1)
{
do_something();
break;
}

LS: do
do {
something();
} while (condition);

LS: do & while
do { something(); }
while (condition);
while (condition)
{ do_something(); }

Jump Statements

CS: Types
break
continue
return
goto

JS: break in loops
for (int i = 0; i < 10; i++) {
do_something();
if (i == 5) { break; }
}
while (cond1) { if (cond2) {break; } }

JS: break in loops
for (int i = 0; i < 10; i++) {
for (int j = 0; i < 10; i++) {
if (j == 5) { break; }
}
}

JS: break in switch
switch (cond) {
case a: do_st_a(); break;
case b: do_st_b(); break; …
default: do_st_df();
}
do_after_switch();

JS: break in switch
What if no break at the end of a case?
switch (cond) {
case a: do_st_a();
case b: do_st_b();
case c: do_st_c(); …
default: do_st_df(); }

JS: When not to break?
switch (day) {
case 1: case 2: case 3: case 4:
case 5: printf("Weekday"); break;
case 6: case 7:
printf(“Weekend"); break;
default: printf(“Invalid day"); }

JS: continue
Skip the remaining code after the continue statement
within a loop and jump to the next iteration of the loop.

JS: continue
for (i = 0; i < 5; i++) {
    if (i == 2) { continue; }
   printf("%d", i);
}