0% found this document useful (0 votes)
15 views

Control Statement in C

Uploaded by

Sabree Pavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Control Statement in C

Uploaded by

Sabree Pavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CONTROL

STATEMENT IN C
Table of contents

01 02
TYPES OF CONTROL CONDITIONAL STATEMENT
STATEMENTS

03 04
LOOPING STATEMENT JUMP STATEMENT
Introduction
● The Statement i.e. used to control the flow of execution of a program is
called control structure.
● By default the statements are executed sequentially. However, in practice,
we have situations where we may have to change the order of executions
of statements until specified conditions are met.
● They determine the order in which instructions are executed and allow us
to make decisions based on certain conditions.
Why are control structures important?
● Flow control: Control structures help us define the sequence in which statements
or blocks of code are executed. This is crucial for achieving the desired behavior
and logic in a program.
● Decision-making: Control structures allow us to make decisions based on
specific conditions. We can create different paths of execution based on whether
a condition is true or false.
● Repetition: Control structures enable us to repeat a block of code multiple times.
This is useful when we want to perform a task iteratively or until a certain
condition is met.
Conditional Statements (If-Else)
Conditional statements are programming constructs that allow us to make
decisions based on certain conditions. They help control the flow of execution by
executing specific blocks of code only if a condition evaluates to true.

One commonly used conditional statement is the if-else statement. It allows us to


specify two different blocks of code: one to execute when the condition is true,
and another to execute when the condition is false.
if-else statement
Example
Structure
#include <stdio.h>
int main() {
if (condition) {
int x = 10, y = 18;
// Code to execute when the
condition is true
if (x > y) {
} else { printf("x is greater than y");
// Code to execute when the } else {
condition is false printf ("x is less than y”);
} }
return 0; }
#include <stdio.h>

int main() {
int age = 14;

if (age >= 18) {


printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}

return 0;
}
In the example above, if the variable age is greater than or equal to 18, the message "You are
eligible to vote" will be displayed. Otherwise, the message "You are not eligible to vote yet"
will be displayed.

The if-else statement is a fundamental control structure that allows us to create decision-making
logic in our programs. By evaluating conditions, we can direct the program's behavior based
on different scenarios.
Switch Statement
● The switch statement is a control structure used to perform
different actions based on the value of a variable or an
expression. It provides an alternative to using multiple if-else
statements when dealing with multiple possible cases.
Syntax of a switch statement in C:

switch (variable) {
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
// Add more cases as needed
default:
// Code to execute if none of the above cases match
break;
}
#include <stdio.h>
case 4:
int main() { printf("Thursday\n");
int day = 3; break;
case 5:
switch (day) { printf("Friday\n");
case 1: break;
printf("Monday\n"); default:
break; printf("Weekend\n");
case 2: break;
printf("Tuesday\n"); }
break;
case 3: return 0;
printf("Wednesday\n"); }
break;
LOOPING STATEMENT

● Loops are essential programming constructs that allow us to repeat a


block of code multiple times. They are used to automate repetitive
tasks, iterate over collections of data, and perform actions until a
specific condition is met.
While Loop
The while loop executes a block of code repeatedly as long as a given condition remains true.

Example of a while loop: Structure of a while loop in C:


#include <stdio.h>
int main() { while (condition) {
int count = 0; // Code to execute while the condition is true
while (count < 5) { }
printf("Count: %d\n", count);
count++;
}
return 0; }
For Loop:
The for loop is another looping construct that provides a more structured approach compared to the
while loop.
It consists of three parts: initialization, condition, and iteration.

Example of a for loop: Structure of a for loop in C:


#include <stdio.h>
for (initialization; condition; iteration) {
int main() {
// Code to execute in each iteration
for (int i = 0; i < 5; i++) {
}
printf("Iteration: %d\n", i);
}
return 0; }
D0 While Loop
The do-while loop is a control flow structure in C that allows you to execute a block of
code repeatedly based on a condition.

Unlike the while loop, the do-while loop guarantees that the code block will be executed
at least once, regardless of the initial condition.
Example of a do-while loop: Structure of a do-while loop in C:

int num = 1;
do {
do {
printf("Number: %d\n", num);
// Code to be executed
num++;
} while (condition);
} while (num <= 5);
JUMP STATEMENT
In C, jump statements are used to transfer control within a program to a different location.
There are three main jump statements in C: break, continue, and goto.

Break Statement: Continue Statement Goto Statement:


The break statement is The continue statement is also The goto statement allows
commonly used in loops used in loops. It skips the transferring control to a
and switch statements. It remaining code within the labeled statement within the
is used to exit the loop for the current iteration same function. However, the
innermost loop or switch and proceeds to the next use of goto is generally
statement immediately, iteration. discouraged as it can make
terminating the loop code harder to understand and
iteration or the switch maintain.
case.
Goto Statement
Break Statement Continue Statement
#include<stdio.h>
int main(){
#include <stdio.h> #include <stdio.h> printf("We are at the first printf
int main() { int main() { statement!!!\n") ;
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) { goto last ;
if (i == 3) { if (i == 3) { printf("We are at the second
break; } continue; } printf statement!!!\n") ;
printf("%d ", i); printf("%d ", i); printf("We are at the third printf
} } statement!!!\n") ;
return 0; return 0; last : printf("We are at the last printf
} } statement!!!\n") ; }
#include <stdio.h>
int main() {
int num = 0;
start:
printf("%d ", num);
num++;
if (num < 5) {
goto start; // Jump back to the
'start' label
}
return 0; }
Summary and Conclusion
● Control structures are fundamental programming constructs that allow
us to control the flow of execution in a program.
● Conditional statements, such as if-else and switch, enable us to make
decisions based on certain conditions.
● Comparison operators (e.g., ==, <, >, <=, >=, !=) help compare values
and create conditions in control structures.
● Loops, such as while and for, allow us to repeat a block of code
multiple times based on specific conditions.
● Loop control statements, including break and continue, provide
additional control and flexibility within loops.
● Control structures can be used together to create more complex logic
and solve problems effectively.
Questions
and
Discussion

You might also like