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

c unit 1.pptx

The document provides an overview of control statements in C programming, which alter the execution flow based on conditions or loops. It covers various types of control statements including conditional statements (if, if-else, switch), loop control statements (for, while, do-while), and jump statements (break, continue, goto, exit). Additionally, it explains nested control structures that allow for more complex decision-making and iterative processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c unit 1.pptx

The document provides an overview of control statements in C programming, which alter the execution flow based on conditions or loops. It covers various types of control statements including conditional statements (if, if-else, switch), loop control statements (for, while, do-while), and jump statements (break, continue, goto, exit). Additionally, it explains nested control structures that allow for more complex decision-making and iterative processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Control Statements

► Control statements in C programming allow the execution flow of


the program to be altered based on certain conditions or loops.

► They help in decision-making, iteration, and branching of


program flow.
Types of Control Statements in C

1. Conditional Control Statements:-

These are used to make decisions based on conditions.


if statement:

► Executes a block of code if the condition is true.


► Syntax:-

if (condition)
{
// code to be executed if condition is true
}
Example
If….else statement

► An if-else statement in C is used to execute different blocks of code based on


whether a condition is true or false.

► Syntax-
if (condition) {
// Code to execute if condition is true
}
else {
// Code to execute if condition is false
}
Example
If-else-if Ladder

► An if-else if ladder is used to test multiple conditions


sequentially.

► It works by evaluating conditions one by one. When a true


condition is found, the corresponding block of code is executed,
and the rest of the conditions are ignored
Syntax
► if (condition1) {
// Code to execute if condition1 is true
}
else if (condition2) {
// Code to execute if condition2 is true
}
else if (condition3) {
// Code to execute if condition3 is true
}
else {
// Code to execute if none of the above conditions are true
}
Example
2. Loop Control Statements

► These control the repetition of a block of code.


for Loop

► The for loop in C Language provides a functionality to repeat a


set of statements a defined number of times.
► The for loop is in itself a form of an entry-controlled loop.
► Syntax-
for (initialization; condition; increment/decrement)
{
// Code to execute
}
Example
while Loop

► A while loop in C is used to execute a block of code repeatedly as


long as a specified condition is true
► A while loop is called an entry-controlled loop because the
condition is evaluated before the loop's body is executed. In other
words, the control enters the loop only if the condition is true; if
the condition is false at the outset, the loop's body will not
execute at all.
Syntax

► while (condition)
{
// code to be executed
}
Example
do-while Loop

► The do-while loop in C is an exit-controlled loop, which means


the condition is checked after the loop body has been executed.
As a result, the loop body is guaranteed to execute at least once,
regardless of the condition.
Syntax
► do {
// Code to be executed
} while (condition);
Example
Switch Statement
► The switch statement in C is an alternate to if-else-if ladder statement which
allows us to execute multiple operations for the different possibles values of
a single variable called switch variable.

► Syntax-
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
How it works:

•The switch expression is evaluated once


•The value of the expression is compared with the values of each case
•If there is a match, the associated block of code is executed
•The break statement breaks out of the switch block and stops the
execution
•The default statement is optional, and specifies some code to run if
there is no case match
Example
Nested Control Structures

► Nested control structures involve placing one control structure


(such as an if, for, while, or switch) inside another control
structure. This allows you to build more complex
decision-making and iterative processes within your code.
Nested-if Statement

► One if statement can be placed inside another if or else statement.


This allows for multiple conditions to be checked sequentially.
Example
Nested Loops

► Loops (such as for, while, or do-while) can be placed


inside each other. Nested loops are commonly used
when working with multi-dimensional arrays or
matrices.
Nested for Loop
Nested switch Statements
Jump Statements
► jump statements are used to jump from one part of the code to
another altering the normal flow of the program.
► They are used to transfer the program control to somewhere else
in the program.
► There are 4 types of jump statements in C:
1. break
2. continue
3. goto
4. exit
break

► The break statement exits or terminates the loop or switch


statement based on a certain condition, without executing the
remaining code.
► When encountered, control is transferred to the first statement
after the loop or switch.
► Uses of break in C
The break statement is used in C for the following purposes:
1. To come out of the loop.
2. To come out from the nested loops.
3. To come out of the switch case.
Example
Explanation

• Loop Execution Starts and goes normally till i = 4.


• When i = 5, the condition for the break statement becomes true
and the program control immediately exits the loop.
• The control continues with the remaining statements outside the
loop.
continue

► The continue statement in C is used to skip the remaining code


after the continue statement within a loop and jump to the next
iteration of the loop.
► When the continue statement is encountered, the loop control
immediately jumps to the next iteration, by skipping the lines of
code written after it within the loop body.
Example
goto

► The goto statement is used to jump to a specific point


from anywhere in a function.
► It is used to transfer the program control to a labeled
statement within the same function.
Example
exit
► exit jump statement in C, which is used to terminate a program
immediately. It comes from the <stdlib.h> header file and stops
the execution of the program regardless of where it is called
► Non-zero (e.g., 1): Indicates abnormal program termination or
failure.
► 0: Indicates successful program termination.
Example

You might also like