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

BCSC 1102 Introduction to Programming Lecture 3 PPT

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

BCSC 1102 Introduction to Programming Lecture 3 PPT

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

BCSC 1102 : Intro To Programming Week 3

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 1 / 18
Introduction

Control structures: selection statements and loops


Selection statements: if, if-else, nested if, switch-case
Looping constructs: for, while, do-while
Control statements: break, continue, goto

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 2 / 18
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

Listing 1: If statement example with arithmetic and relational operators

@*)

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 3 / 18
If Statement (continued)

1 return 0;
2 }

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 4 / 18
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 " );
11 }
12

Listing 2: If-Else statement example with logical operators

@*)

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 5 / 18
If-Else Statement (continued)

1 return 0;
2 }

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 6 / 18
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 ␣ se
11 }
12 } else {
13 printf ( " The ␣ number ␣ is ␣ even .\ n " );
14 }
15

Listing 3: Nested If statement example with bitwise operators

@*)
The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 7 / 18
Nested If Statement (continued)

1 return 0;
2 }

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 8 / 18
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.
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 ; The Co-operatetive University of Kenya Sep
21Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 9 / 18
Switch-Case Statement (continued)

1 case 4:
2 if ( b != 0) {
3 printf ( " Result : ␣ % d \ n " , a / b );
4 } else {
5 printf ( " Division ␣ by ␣ zero ␣ is ␣ not ␣ allowed .\ n " );
6 }
7 break ;
8 default :
9 printf ( " Invalid ␣ operation ␣ selected .\ n " );
10 break ;
11 }
12
13 return 0;
14 }

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 10 / 18
For Loop

The for loop is used when the number of iterations is known beforehand.

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 }

Listing 5: For loop example with arithmetic and relational operators

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 11 / 18
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

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 12 / 18
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

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 13 / 18
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
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

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 14 / 18
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

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 15 / 18
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;
11 }

Listing 10: Continue statement example

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 16 / 18
Goto Statement

The goto statement transfers control to a labeled statement.

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

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 17 / 18
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.
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.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 3 18 / 18

You might also like