Control Structures and Functions
FE (PSP)
Branching structures
01 02 03
Use if to specify a block Use else if to specify a Use switch to specify
of code to be executed, new condition to test, if many alternative blocks
if a specified condition the first condition of code to be executed
is true. Use else to is false
specify a block of code
to be executed, if the
same condition is false
If Statement
Use the if statement to specify a block of code to be executed if a condition
is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Example
• int x = 20;
int y = 18;
if (x > y) {
printf("x is greater than y");
}
• Output
• x is greater than y
Use the else statement to specify a block of code to be executed
if the condition is false.
The if-else
Statement Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
• int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."
Nested if-else statements
When a nested if-else statement is executed, the conditions are
evaluated in a hierarchical manner.
A nested if-else statement is when you have an if-else block inside
another if or else block. It's like placing one decision within another
decision. You use this when you want to check for a new condition
after a previous condition has already been found true (or false).
if (condition1) {
Syntax of // Statements to execute if condition1 is true
Nested If else
Statement if (condition2) {
// Statements to execute if condition1 and
condition2 are both true
} else {
// Statements to execute if condition1 is true
and condition2 is false
}
} else {
// Statements to execute if condition1 is false
}
Example
• Problem Statement:
• Determine the grade of a student based on their score.
If the score is above 90, it's an ‘A+’.
• If the score is between 80 and 90, check if it's above 85
for an 'A' or else 'B’.
• Below 80, if it's above 70, it's a 'C', otherwise, it's a 'D'.
Example
code
• Instead of writing many if..else statements, you can use
the switch statement.
• The switch statement selects one of many code blocks to be
Switch executed:
• switch (expression) {
Statement case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
Looping
structures
Part 2B
Break
int i;
for (i = 0; i < 10; i++) {
The break statement can also be used This example jumps out of the for if (i == 4) {
to jump out of a loop. loop when i is equal to 4: break;
}
printf("%d\n", i);
}
• The continue statement breaks one iteration (in
the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
• This example skips the value of 4:
Continue • Example
Statement
• int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
Break in While Loop
• int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}
• int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
Continue in }
}
printf("%d\n", i);
i++;
While Loop
• When you know exactly how many times
you want to loop through a block of code,
use the for loop instead of a while loop:
For Loop
• for (expression 1; expression 2; expression
3) {
// code block to be executed
}
• Expression 1 is executed (one time)
before the execution of the code block.
• Expression 2 defines the condition for
executing the code block.
• Expression 3 is executed (every time)
after the code block has been executed.
Example and O/P
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
Nested Loops
• It is also possible to place a loop inside another loop. This is called
a nested loop.
• The "inner loop" will be executed one time for each iteration of the
"outer loop":
Example and O/P
• int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i);
// Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j);
// Executes 6 times (2 * 3)
}
}
Loops
Loops can execute a block Loops are handy because
of code as long as a they save time, reduce
specified condition is errors, and they make
reached. code more readable.
While Loop
The while loop loops through a block of code as long as a specified
condition is true:
Syntax
while (condition) {
// code block to be executed
}
Example of While loop
do while loop
A "do-while" loop is a form of a loop in C that executes
the code block first, followed by the condition.
If the condition is true, the loop continues to run; else, it
stops.
However, whether the condition is originally true, it
ensures that the code block is performed at least once.
Example of Do-While loop