Course Code TACSE101 Faculty Mr.
Raja M
Course Title Programming in C EMP_ID 22666
Module:2 - Control Structures and Functions
Decision Making Statements: if, if-else, nested if-else, switch-case. Looping Constructs: for, while, do-
while loops, nested loops. Jump statements – break and continue – goto statement and its limitation -
Function Basics – Function declaration and definition – Function call and return types – Parameter
passing mechanisms (Call by value and call by reference) – Recursion.
Decision Making in C
In C, programs can choose which part of the code to execute based on some condition.
This ability is called decision making and the statements used for it are called conditional
statements.
These statements evaluate one or more conditions and make the decision whether to execute a block
of code or not.
Types of Conditional Statements in C
if
The if statement is the simplest decision-making statement. It is used to decide whether a certain statement
or block of statements will be executed or not i.e if a certain condition is true then a block of statements is
executed otherwise not.
A condition is any expression that evaluates to either a true or false (or values convertible to true or false).
The if in C is the simplest decision-making statement. It consists of the test condition and a block of code
that is executed if and only if the given condition is true. Otherwise, it is skipped from execution.
Syntax of if
if-else
The if else in C is an extension of the if statement which not only allows the program to execute one
block of code if a condition is true, but also a different block if the condition is false. This enables
making decisions with two possible outcomes.
Syntax of if-else Statement
Working of if-else statement
Example:
if , else if ladder
In C, if else if ladder is an extension of if else statement used to test a series of conditions sequentially,
executing the code for the first true condition. A condition is checked only if all previous ones are false.
Once a condition is true, its code block executes, and the ladder ends.
Syntax of if, else if ladder
Nested if
A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if
statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can
place an if statement inside another if statement.
Syntax
Example:
Switch Statement in C
C switch statement is a conditional statement that allows you to execute different code blocks based on the
value of a variable or an expression. It is often used in place of if-else ladder when there are multiple
conditions.
Syntax
Working switch statement
Example:
Looping Constructs
Loops in C programming are used to repeat a block of code until the specified condition is met. It allows
programmers to execute a statement or group of statements multiple times without writing the code again
and again.
Types of Loops in C
Example:
for Loop:
for loop is an entry-controlled loop, which means that the condition is checked before the loop's body
executes.
The various parts of the for loop are:
Initialization: Initialize the variable to some initial value.
Test Condition: This specifies the test condition. If the condition evaluates to true, then body of the
loop is executed. If evaluated false, loop is terminated.
Update Expression: After the execution loop’s body, this expression increments/decrements the loop
variable by some value.
Body of Loop: Statements to repeat. Generally enclosed inside {} braces.
Example:
while Loop
The while loop in C allows a block of code to be executed repeatedly as long as a given condition remains
true. It is often used when we want to repeat a block of code till some condition is satisfied.
Syntax
Initialization: In this part, we initialize the loop variable to some initial value. Initialization is not
part of while loop syntax but it is essential when we are using some variable in the test expression
Conditional: This is one of the most crucial part as it decides whether the block in the while
loop code will execute. The while loop body will be executed if and only the test condition defined
in the conditional statement is true.
Body: It is the actual set of statements that will be executed till the specified condition is true.
Updation: It is an expression that updates the value of the loop variable in each iteration. It is also
not part of the syntax, but we have to define it explicitly in the body of the loop.
Example:
do-while Loop
The do-while loop is an exit-controlled loop, which means that the condition is checked after executing the
loop body. Due to this, the loop body will execute at least once irrespective of the test condition.
Syntax
When the program control comes to the do...while loop, the body of the loop is executed first and
then the test condition/expression is checked, unlike other loops where the test condition is
checked first. Due to this property, the do...while loop is also called exit controlled or post-tested
loop.
When the test condition is evaluated as true, the program control goes to the start of the loop and
the body is executed once more.
The above process repeats till the test condition is true.
When the test condition is evaluated as false, the program controls move on to the next
statements after the do...while loop.
Example:
Nested Loops in C
A nested loop means a loop statement inside another loop statement. For a nested loop, the inner loop
performs all of its iterations for each iteration of the outer loop.
Types of Nested Loops in C
1. Nested for Loop
Nested for loop refers to any type of loop that is defined inside a 'for' loop. Below is the equivalent flow
diagram for nested 'for' loops:
Example:
2. Nested while Loop
A nested while loop refers to any type of loop that is defined inside a 'while' loop.
Example:
3. Nested do-while Loop
A nested do-while loop refers to any type of loop that is defined inside a do-while loop.
Example:
Jump Statements in C
In C, 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.
In this article, we will discuss the jump statements in C and how to use them.
Types of Jump Statements in C
There are 4 types of jump statements in C:
break
continue
goto
return
Break Statement in C
The break statement exits or terminates the loop or switch statement based on a certain condition, without
executing the remaining iteration of the loop or checking remaining cases in switch statement.
The break statement is used in C for the following purposes:
1. To come out of the loop.
2. To come out of the switch case.
Example:
Continue Statement in C
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.
Syntax of continue in C
The loop's execution starts after the loop condition is evaluated to be true.
The condition of the continue statement will be evaluated.
o If the condition is false, the normal execution will continue.
o If the condition is true, the program control will jump to the start of the loop and all the
statements below the continue will be skipped.
Steps 1 and 2 will be repeated till the end of the loop.
Example:
Goto Statement in C
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.
Syntax of goto Statement
Example:
Return Statement in C
C return statement is used to end the execution of a function and return a value. It also passes the control
back to the function from which it was called. The return statement may or may not return a value
depending on the function's return type. For example, int returns an integer value, void returns nothing, etc.
In C, we can only return a single value from the function using the return statement.
A C function can have multiple return statements, but only one is executed.
Syntax of return in C
Example:
Function Basics
A function is a named block of code that performs a specific task. It allows you to write a piece of logic once
and reuse it wherever needed in the program. This helps keep your code clean, organized, and easier to
understand.
Functions play a vital role in building modular programs. They allow you to break down complex problems
into smaller, manageable parts.
How Functions Work in C?
Explanation of each part:
Return type: Specifies the type of value the function will return. Use void if the function does not
return anything.
Function name: A unique name that identifies the function. It follows the same naming rules as
variables.
Parameter list: A set of input values passed to the function. If the function takes no inputs, this can
be left empty or written as void.
Function body: The block of code that runs when the function is called. It is enclosed in curly
braces { }.
Function Declaration vs Definition
It's important to understand the difference between declaring a function and defining it. Both play different
roles in how the compiler understands and uses your function.
Function Declaration
A declaration tells the compiler about the function's name, return type, and parameters before it is actually
used. It does not contain the function's body. This is often placed at the top of the program or in a header
file.
Function Definition
A definition provides the actual implementation of the function. It includes the full code or logic that runs
when the function is called.
Calling a Function
Once a function is defined, you can use it by simply calling its name followed by parentheses. This tells the
program to execute the code inside that function.
Example:
Types of Function in C
In C programming, functions can be grouped into two main categories: library functions and user-defined
functions. Based on how they handle input and output, user-defined functions can be further classified into
different types.
[Link] Functions:
These are built-in functions provided by C, such as printf(), scanf(), sqrt(), and many others. You can use
them by including the appropriate header file, like #include <stdio.h> or #include <math.h>.
[Link]-Defined Functions:
These are functions that you create yourself to perform specific tasks in your program. Depending on
whether they take input or return a value, they can be of four types:
No arguments, no return value: The function neither takes input nor returns any result.
Arguments, no return value: The function takes input but does not return anything.
No arguments, return value: The function does not take input but returns a result.
Arguments and return value: The function takes input and returns a result.
Each type serves different purposes depending on what the program needs. Using the right type helps make
your code more organized and efficient.
How to use User-Defined Functions in C?
To use a user-defined function, we first have to understand the different parts of its syntax. The user-defined
function in C can be divided into three parts:
1. Function Prototype
2. Function Definition
3. Function Call
C Function Prototype
A function prototype is also known as a function declaration which specifies the function's name, function
parameters, and return type.
The function prototype does not contain the body of the function. It is basically used to inform the compiler
about the existence of the user-defined function which can be used in the later part of the program.
C Function Definition
Once the function has been called, the function definition contains the actual statements that will be
executed. All the statements of the function definition are enclosed within { } braces.
C Function Call
In order to transfer control to a user-defined function, we need to call it. Functions are called using their
names followed by round brackets. Their arguments are passed inside the brackets.
Example:
Components of Function Definition
There are three components of the function definition:
1. Function Parameters
2. Function Body
3. Return Value
1. Function Parameters
Function parameters (also known as arguments) are the values that are passed to the called function by the
caller. We can pass none or any number of function parameters to the function.
We have to define the function name and its type in the function definition and we can only pass the same
number and type of parameters in the function call.
Example
2. Function Body
The function body is the set of statements that are enclosed within { } braces. They are the statements that
are executed when the function is called.
Example
3. Return Value
The return value is the value returned by the function to its caller. A function can only return a single value
and it is optional. If no value is to be returned, the return type is defined as void.
The return keyword is used to return the value from a function.
Syntax Example:
Passing Parameters to User-Defined Functions
We can pass parameters to a function in C using two methods:
1. Call by Value
2. Call by Reference
1. Call by value
In call by value, a copy of the value is passed to the function and changes that are made to the function are
not reflected back to the values.
Actual and formal arguments are created in different memory locations.
Example
2. Call by Reference
In a call by Reference, the address of the argument is passed to the function, and changes that are made to
the function are reflected back to the values.
We use the pointers of the required type to receive the address in the function.
Example:
Recursion in C
Recursion means a function calling itself to solve a problem.
The problem is broken into smaller subproblems until a simple case is reached
Why use Recursion?
It makes code shorter and cleaner for problems that have repeated patterns.
Common in problems like factorial, Fibonacci series, tree traversals, etc.
Basic Rule of Recursion:
1. Base Case – Stops the recursion.
2. Recursive Call – Function calls itself with a smaller input.
Syntax:
Example:
Output: