BCSC 1102 Introduction to Programming Lecture 5
BCSC 1102 Introduction to Programming Lecture 5
Contents
1 Introduction 2
2 Selection Statements 2
2.1 If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 If-Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Nested If Statement . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Switch-Case Statement . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Looping Constructs 4
3.1 For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Do-While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.4 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5 Conclusion 7
1
1 Introduction
Control structures such as selection statements and loops allow a program to
control the flow of execution based on conditions or repetition. In this lecture,
we will cover selection statements (if, if-else, nested if, switch-case) and
looping constructs (for, while, do-while), including nested loops and control
statements (break, continue, goto).
2 Selection Statements
2.1 If Statement
The if statement evaluates a condition and executes the code block if the
condition is true.
1 # include < stdio .h >
2
3 int main () {
4 int a = 10;
5 int b = 20;
6
7 if ( a + b > 25) {
8 printf ( " The ␣ sum ␣ of ␣ a ␣ and ␣ b ␣ is ␣ greater ␣ than ␣
25.\ n " ) ;
9 }
10
11 return 0;
12 }
Listing 1: If statement example with arithmetic and relational operators
3 int main () {
4 int x = 15;
5 int y = 25;
6
2
11 }
12
13 return 0;
14 }
Listing 2: If-Else statement example with logical operators
3 int main () {
4 int num = 5; // binary : 0101
5
6 if ( num & 1) {
7 if ( num & (1 << 1) ) {
8 printf ( " The ␣ number ␣ is ␣ odd ␣ and ␣ the ␣ second ␣
bit ␣ is ␣ set .\ n " ) ;
9 } else {
10 printf ( " The ␣ number ␣ is ␣ odd ␣ and ␣ the ␣ second ␣
bit ␣ is ␣ not ␣ set .\ n " ) ;
11 }
12 } else {
13 printf ( " The ␣ number ␣ is ␣ even .\ n " ) ;
14 }
15
16 return 0;
17 }
Listing 3: Nested If statement example with bitwise operators
3 int main () {
4 int operation ;
5 int a = 8;
6 int b = 4;
7
3
9 scanf ( " % d " , & operation ) ;
10
11 switch ( operation ) {
12 case 1:
13 printf ( " Result : ␣ % d \ n " , a + b ) ;
14 break ;
15 case 2:
16 printf ( " Result : ␣ % d \ n " , a - b ) ;
17 break ;
18 case 3:
19 printf ( " Result : ␣ % d \ n " , a * b ) ;
20 break ;
21 case 4:
22 if ( b != 0) {
23 printf ( " Result : ␣ % d \ n " , a / b ) ;
24 } else {
25 printf ( " Division ␣ by ␣ zero ␣ is ␣ not ␣
allowed .\ n " ) ;
26 }
27 break ;
28 default :
29 printf ( " Invalid ␣ operation ␣ selected .\ n " ) ;
30 break ;
31 }
32
33 return 0;
34 }
Listing 4: Switch-Case statement example with arithmetic and relational
operators
3 Looping Constructs
3.1 For Loop
The for loop is used when the number of iterations is known beforehand. It
consists of three parts: initialization, condition, and increment/decrement.
1 # include < stdio .h >
2
3 int main () {
4 for ( int i = 0; i < 10; i ++) {
5 printf ( " Iteration : ␣ % d \ n " , i ) ;
6 }
7 return 0;
8 }
4
Listing 5: For loop example with arithmetic and relational operators
3 int main () {
4 int x = 0;
5 while ( x < 5) {
6 printf ( " x ␣ is : ␣ % d \ n " , x ) ;
7 x ++;
8 }
9 return 0;
10 }
Listing 6: While loop example with logical operators
3 int main () {
4 int y = 0;
5 do {
6 printf ( " y ␣ is : ␣ % d \ n " , y ) ;
7 y ++;
8 } while ( y < 3) ;
9 return 0;
10 }
Listing 7: Do-While loop example with arithmetic operators
5
3 int main () {
4 for ( int i = 1; i <= 3; i ++) {
5 for ( int j = 1; j <= 3; j ++) {
6 printf ( " i : ␣ %d , ␣ j : ␣ % d \ n " , i , j ) ;
7 }
8 }
9 return 0;
10 }
Listing 8: Nested loops example with arithmetic operators
3 int main () {
4 for ( int i = 0; i < 10; i ++) {
5 if ( i == 5) {
6 break ; // exit the loop
7 }
8 printf ( " i ␣ is : ␣ % d \ n " , i ) ;
9 }
10 return 0;
11 }
Listing 9: Break statement example
3 int main () {
4 for ( int i = 0; i < 10; i ++) {
5 if ( i % 2 == 0) {
6 continue ; // skip even numbers
7 }
8 printf ( " i ␣ is : ␣ % d \ n " , i ) ;
9 }
10 return 0;
6
11 }
Listing 10: Continue statement example
3 int main () {
4 int num = 0;
5
6 start :
7 num ++;
8 printf ( " num ␣ is : ␣ % d \ n " , num ) ;
9 if ( num < 5) {
10 goto start ; // jump to the label ’ start ’
11 }
12 return 0;
13 }
Listing 11: Goto statement example
5 Conclusion
Selection statements and looping constructs are fundamental for controlling the
flow of a program. The if, if-else, nested if, and switch-case statements
enable conditional execution, while the for, while, and do-while loops allow
for repetitive execution. Control statements like break, continue, and goto
provide additional control over the loop’s behavior.