Lecture 02 Control
Lecture 02 Control
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 1 / 30
Session 3: Decision Making and Management
Problem Statement:
Assume given marks are x for a student.
Determine the grade of the student based on the following conditions:
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 2 / 30
Decision Making and Management
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 3 / 30
Conditional Operators in C
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 4 / 30
if Statement Syntax
if (condition) {
// Code to execute if the condition is true
}
Example:
if (x > 0) {
printf("x is positive.");
}
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 5 / 30
if-else Statement Syntax
The syntax of the if-else statement in C is as follows:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
if (x > 0) {
printf("x is positive.");
} else {
printf("x is not positive.");
}
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 6 / 30
if-else if-else Statement Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}
Example:
if (x > 0) {
printf("x is positive.");
} else if (x < 0) {
printf("x is negative.");
} else {
printf("x is zero.");
}
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 7 / 30
Solution Using if-else
Example program to solve the problem:
1 # include < stdio .h >
2
3 int main () {
4 int x ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & x );
7
8 if ( x > 95) {
9 printf ( " Student is brilliant \ n " );
10 } else if ( x < 30) {
11 printf ( " Student is poor \ n " );
12 } else {
13 printf ( " Student is average \ n " );
14 }
15 return 0;
16 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 8 / 30
Solution Using Nested if-else
Example program with nested conditions:
1 # include < stdio .h >
2
3 int main () {
4 int x ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & x );
7
8 if ( x > 95) {
9 printf ( " Student is brilliant \ n " );
10 } else {
11 if ( x < 30) {
12 printf ( " Student is poor \ n " );
13 } else {
14 printf ( " Student is average \ n " );
15 }
16 }
17 return 0;
18 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 9 / 30
Ternary Operator Syntax
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 10 / 30
Solution Using Ternary Operator
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 11 / 30
Summary of Approaches
Key Points:
if-else: Clear and simple for straightforward logic.
Nested if-else: Useful for multiple levels of conditions.
Ternary Operator: Concise but less readable for complex conditions.
Choose the Approach:
Use if-else for simplicity.
Use nested if-else for more structured conditions.
Use the ternary operator for concise, single-line conditions.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 12 / 30
Exercise: if-else
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 13 / 30
Exercise: Nested if-else
Example of nested if-else to check grading system:
1 # include < stdio .h >
2
3 int main () {
4 int marks ;
5 printf ( " Enter marks : " );
6 scanf ( " % d " , & marks );
7
8 if ( marks >= 50) {
9 if ( marks >= 75) {
10 printf ( " Grade A \ n " );
11 } else {
12 printf ( " Grade B \ n " );
13 }
14 } else {
15 printf ( " Fail \ n " );
16 }
17 return 0;
18 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 14 / 30
Switch Statement Syntax
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 15 / 30
Exercise: switch
Example of switch statement to check day of the week:
1 # include < stdio .h >
2 int main () {
3 int day ;
4 printf ( " Enter day number (1 -7): " );
5 scanf ( " % d " , & day );
6 switch ( day ) {
7 case 1: printf ( " Sunday \ n " ); break ;
8 case 2: printf ( " Monday \ n " ); break ;
9 case 3: printf ( " Tuesday \ n " ); break ;
10 case 4: printf ( " Wednesday \ n " ); break ;
11 case 5: printf ( " Thursday \ n " ); break ;
12 case 6: printf ( " Friday \ n " ); break ;
13 case 7: printf ( " Saturday \ n " ); break ;
14 default : printf ( " Invalid day \ n " );
15 }
16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 16 / 30
Session 4: Looping Constructs
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 17 / 30
Why Looping?
Consider an Example:
1 // C program to illustrate need of loops
2 # include < stdio .h >
3
4 int main ()
5 {
6 printf ( " Hello World \ n " );
7 printf ( " Hello World \ n " );
8 printf ( " Hello World \ n " );
9 printf ( " Hello World \ n " );
10 printf ( " Hello World \ n " );
11 printf ( " Hello World \ n " );
12 printf ( " Hello World \ n " );
13 printf ( " Hello World \ n " );
14 printf ( " Hello World \ n " );
15 printf ( " Hello World \ n " );
16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 18 / 30
Types of Loop
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 19 / 30
Syntax of for Loop
Example:
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 21 / 30
Syntax of while Loop
The syntax of the while loop in C is as follows:
while (condition) {
// Code to execute while the condition is true
}
Example:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
- The **condition** is checked before entering the loop body. - The loop
runs as long as the condition is true.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 22 / 30
Example: while Loop
3 int main () {
4 int i = 1;
5 while ( i <= 5) {
6 printf ( " % d " , i );
7 i ++;
8 }
9 return 0;
10 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 23 / 30
Syntax of do-while Loop
The syntax of the do-while loop in C is as follows:
do {
// Code to execute at least once
} while (condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
- The **do-while** loop executes the code block at least once, regardless
of the condition. - The condition is checked after the code block executes.
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 24 / 30
Example: do-while Loop
3 int main () {
4 int i = 1;
5 do {
6 printf ( " % d " , i );
7 i ++;
8 } while ( i <= 5);
9 return 0;
10 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 25 / 30
Example using Nested Loops: Multiplication Table
3 int main () {
4 for ( int i = 1; i <= 10; i ++) {
5 for ( int j = 1; j <= 10; j ++) {
6 printf ( " % d \ t " , i * j );
7 }
8 printf ( " \ n " );
9 }
10 return 0;
11 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 26 / 30
Break and Continue Statements
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 27 / 30
Example and Exercise
// Program to calculate the sum of first n natural numbers:
1 # include < stdio .h >
2 int main ()
3 {
4 int num , count , sum = 0;
5
6 printf ( " Enter a positive integer : " );
7 scanf ( " % d " , & num );
8 // for loop terminates when count exceeds num
9 for ( count = 1; count <= num ; ++ count )
10 {
11 sum += count ;
12 }
13
14 printf ( " Sum = % d " , sum );
15
16 return 0;
17 }
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 28 / 30
Example and Exercise
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 29 / 30
Exercise: Practice with Conditions and Loops
Prof. Dr. Rafiqul Islam C Programming: Control Structures and Loops January 23, 2025 30 / 30