Practice Session 2 - Control Flow Statements in C & Applications
wethanhcong
4 views
28 slides
Oct 22, 2025
Slide 1 of 28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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...
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 emphasizes writing efficient C programs that make decisions, perform iterations, and implement logic for real-world applications such as number checks, menu-driven programs, and pattern generation.
Size: 130.86 KB
Language: en
Added: Oct 22, 2025
Slides: 28 pages
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.
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);
}