Control Flow & Statements
Control Flow & Statements
statements
PRESENTED BY
1.CHANDRU KV
2.HEMAVARDAN S
3.PEVIN KUMAR K
4.HEMANTH CHOWDARY
5.DHANUSH 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.
• 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:
6
Jump Statements
These statements allow you to alter the flow of control unconditionally. The most common jump statements in
C are:
• continue : Skips the current iteration of a loop and proceeds to the next iteration.
• 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