0% found this document useful (0 votes)
5 views

BCSC 1102 Introduction to Programming Lecture 5

This document is a lecture outline for BCSC 1102: Intro to Programming, covering selection statements and looping constructs. It includes detailed explanations and examples of if statements, loops (for, while, do-while), and control statements (break, continue, goto). The course is taught by Dr. Shem Mbandu Angolo at The Cooperative University of Kenya from September to December 2024.

Uploaded by

eddygg2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

BCSC 1102 Introduction to Programming Lecture 5

This document is a lecture outline for BCSC 1102: Intro to Programming, covering selection statements and looping constructs. It includes detailed explanations and examples of if statements, loops (for, while, do-while), and control statements (break, continue, goto). The course is taught by Dr. Shem Mbandu Angolo at The Cooperative University of Kenya from September to December 2024.

Uploaded by

eddygg2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BCSC 1102 : Intro To Programming Week 3

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

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

4 Control Statements: break, continue, and goto 6


4.1 Break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 Goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

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

2.2 If-Else Statement


The if-else statement provides an alternative path of execution if the condition
is false.
1 # include < stdio .h >
2

3 int main () {
4 int x = 15;
5 int y = 25;
6

7 if ( x > 10 && y < 30) {


8 printf ( " Both ␣ conditions ␣ are ␣ true .\ n " ) ;
9 } else {
10 printf ( " One ␣ or ␣ both ␣ conditions ␣ are ␣ false .\ n " ) ;

2
11 }
12

13 return 0;
14 }
Listing 2: If-Else statement example with logical operators

2.3 Nested If Statement


Nested if statements are if statements within another if or else block.
1 # include < stdio .h >
2

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

2.4 Switch-Case Statement


The switch-case statement selects a block of code to execute based on the
value of an expression.
1 # include < stdio .h >
2

3 int main () {
4 int operation ;
5 int a = 8;
6 int b = 4;
7

8 printf ( " Select ␣ an ␣ operation : ␣ 1. ␣ Add ␣ 2. ␣ Subtract ␣ 3.


␣ Multiply ␣ 4. ␣ Divide \ n " ) ;

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.2 While Loop


The while loop is used when the number of iterations is not known beforehand
and depends on a condition.
1 # include < stdio .h >
2

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.3 Do-While Loop


The do-while loop is similar to the while loop, but it guarantees that the loop
body is executed at least once.
1 # include < stdio .h >
2

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

3.4 Nested Loops


Nested loops are loops within another loop. They are used for multi-dimensional
data structures or complex iterations.
1 # include < stdio .h >
2

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

4 Control Statements: break, continue, and goto


4.1 Break Statement
The break statement terminates the loop immediately.
1 # include < stdio .h >
2

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

4.2 Continue Statement


The continue statement skips the current iteration and proceeds to the next
iteration of the loop.
1 # include < stdio .h >
2

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

4.3 Goto Statement


The goto statement transfers control to a labeled statement. It is generally
advised to use it sparingly.
1 # include < stdio .h >
2

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.

You might also like