Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Lecture 8 - Control Structure

for it

Uploaded by

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

Lecture 8 - Control Structure

for it

Uploaded by

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

Control Structure in C

Ryan Daniel S. Asuncion | rdasuncion@mmsu.edu.ph


Department of Information Technology | CCIS | MMSU
Control Structure in C Programming
Control structures are a fundamental concept in programming that
allow you to control the flow of execution of your program. In C
programming, control structures can be broadly categorized into
three types: sequence, selection, and iteration (looping).
1. Sequence Control Structure
The sequence structure represents
the default flow of control.
Instructions are executed one after
another in a linear sequence. Every
program begins with this structure.

Here, the statements are executed


sequentially from top to bottom.
2. Selection (Decision-Making)
Control Structure
Selection control structures allow you to make decisions and
execute code based on certain conditions.
2.1 if Statement
The simplest decision-making
structure in C, the if statement,
allows you to execute a block of
code only if a specified condition is
true.
if (condition) {
// Code to be executed if the condition is
true
}

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.

if (condition1) { Syntax for declaring an if-


// Code block 1 else if – else statement
} else if (condition2) {
// Code block 2
} else {
// Default code block
}
2.3 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.

Syntax for declaring for


for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
statement in C programming
}
3.1 for Loop Statement
The for loop is used when you know how many times you want to
iterate.

Example of For Loop


statement in C programming
3.2 While Loop
The while loop is used when you want to repeat a block of code as
long as a condition is true.

Syntax for declaring while


while (condition) {
// Code to be executed while the condition is true statement in C programming
}
3.2 While Loop

Example of while statement


in C programming
3.3 do-while Loop
The do-while loop is similar to the while loop, but it guarantees that
the code block will be executed at least once.

do { Syntax for declaring do-while


// Code to be executed statement in C programming
} while (condition);
3.3 do-while Loop

Example do-while statement


in C programming
4. Jump Statements
C provides a few jump statements that allow you to alter the normal flow of
execution.
4.1 break Statement
The break statement is used to exit a loop or switch statement
prematurely.
Break statement in C
programming
4.3 return Statement
The return statement is used to exit from a function and return a
value.

#include <stdio.h>

int addNumbers(int a, int b) {


return a + b; // Return the sum
} Return statement in C
programming
int main() {
int sum = addNumbers(5, 10);
printf("Sum: %d\n", sum);
return 0;
}
5. Nested Control Structure
You can nest control structures within each other. For instance, you can place
loops inside conditionals or other loops.
5.1 Nested if statement
Nested If Statement
5.2 Nested Loop
#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;
}

You might also like