Lecture 8 - Control Structure
Lecture 8 - Control Structure
Syntax
2.1 if Statement
Example of if statement in
C programming
2.2 if-else Statement
The if-else statement provides an alternative code block if the
condition is false.
if (condition) {
// Code to be executed if condition is true Syntax for declaring an if
} else { else statement in C
// Code to be executed if condition is false programming
}
2.2 if-else Statement
Example of if else
statement in C
programming
2.3 else if Ladder
When you need to test multiple conditions, you can use an else if
ladder.
Example of else if
statement in C
programming
2.4 switch Statement
The switch statement is an alternative to using multiple if-else
statements when you are testing a variable against multiple
constant values.
switch(expression) {
case constant1:
// Code block for case 1 Syntax for declaring a
break; switch statement
case constant2:
// Code block for case 2
break;
default:
// Default code block
}
2.4 switch Statement
Example of switch
statement in C
programming
3. Iteration (Looping) Control
Structure
Loops allow you to execute a block of code repeatedly, either a fixed number of
times or until a condition is met.
3.1 for Loop Statement
The for loop is used when you know how many times you want to
iterate.
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) { Nested Loop Statement
printf("* ");
}
printf("\n");
}
return 0;
}