Control flow &
statements
PRESENTED BY
[Link] KV
[Link] S
[Link] KUMAR K
[Link] CHOWDARY
[Link] M
Introduction
CONTROL FLOW REFERS TO THE ORDER IN WHICH
INDIVIDUAL STATEMENTS, INSTRUCTIONS, OR FUNCTION CALLS ARE
EXECUTED OR EVALUATED IN A PROGRAMMING LANGUAGE. IT
ALLOWS PROGRAMS TO MAKE DECISIONS, REPEAT TASKS, AND
CONTROL THE SEQUENCE OF OPERATIONS. WITHOUT CONTROL
FLOW, PROGRAMS WOULD EXECUTE LINEARLY FROM TOP TO
BOTTOM WITHOUT ANY FLEXIBILITY.
2
Control flow & statements
FOR C PROGRAM
Conditional statement’s
These statements enable your program to make decisions based on
conditions.
The most common conditional statements in C are:
• If statement : Executes a block of code if a specified condition is true.
• else statement : Executes a block of code if the condition in the if statement is false.
• else if statement : Adds additional conditions to an if statement.
• switch statement : Executes one block of code out of many based on the value of a
variable.
4
Example for conditional
statement’s INT X = 10;
IF (X > 0) {
PRINTF("X IS POSITIVE\N");
} ELSE IF (X == 0) {
PRINTF("X IS ZERO\N");
} ELSE {
PRINTF("X IS NEGATIVE\N");
} INT CHOICE = 2;
SWITCH (CHOICE) {
CASE 1:
PRINTF("CHOICE IS 1\N");
BREAK;
CASE 2:
PRINTF("CHOICE IS 2\N");
BREAK;
DEFAULT:
PRINTF("CHOICE IS NOT 1 OR 2\N");
BREAK;
}
Looping Statements
These statements allow your program Example for looping statements:
to repeat a block of code multiple for (int i = 0; i < 5; i++) {
times. The most common looping printf("%d\n", i);
statements in C are: }
int y = 5;
•for loop: Repeats a block of code a while (y > 0) {
specific number of times.
printf("%d\n", y);
• while loop: Repeats a block of code as y--;
long as a specified condition is true. }
• do-while loop: Similar to the while loop,
int z = 3;
but checks the condition after executing do {
the block of code, ensuring the block is printf("%d\n", z);
executed at least once.
z--;
} while (z > 0);
6
Jump Statements
These statements allow you to alter the flow of control unconditionally. The most common jump statements in
C are:
• break : Exits a loop or switch statement prematurely.
• continue : Skips the current iteration of a loop and proceeds to the next iteration.
• return : Exits a function and optionally returns a value.
• goto : Jumps to a labeled statement within the same function. (Use with caution, as it can make code difficult to read and
maintain.)
7
Example for Jump statements
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
if (i % 2 == 0) {
continue; // Skip the current iteration if i
is even
}
printf("%d\n", i);
}
int add(int a, int b) {
return a + b; // Exit the function and return
the sum
}
goto_label:
printf("Jumped to label\n");
int j = 0;
if (j == 0) {
goto goto_label; // Jump to the labeled
statement 8
Thank you !
ANY QUERIES