c Programming Notes Unit 2
c Programming Notes Unit 2
UNIT 2
The conditional statements (also known as decision control structures) such as
if, if else, switch, etc. are used for decision-making purposes in C programs.
They are also known as Decision-Making Statements and are used to evaluate
one or more conditions and make the decision whether to execute a set of
statements or not. These decision-making statements in programming
languages decide the direction of the flow of program execution.
break
continue
goto
return
1. if in C
The if statement is the most simple 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.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
3. Nested if-else in C
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.
4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple options. The C if
statements are executed from the top down. As soon as one of the conditions controlling the if is
true, the statement associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if
ladder is similar to the switch statement.
5. Switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the
switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
C – Loops
Loops in programming are used to repeat a block of code until the specified condition is met.
A loop statement allows programmers to execute a statement or group of statements multiple times
without repetition of code.
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of
the loop body. The loop body will execute at least once, irrespective of whether the
condition is true or false. do-while Loop is Exit Controlled loop.
for Loop
for loop in C programming is a repetition control structure that allows programmers to write a loop
that will be executed a specific number of times. for loop enables programmers to perform n number
of steps together in a single line.
Syntax:
Test Expression: In this expression, test conditions are performed. If the condition evaluates
to true then the loop body will be executed and then an update of the loop variable is done.
If the test expression becomes false then the control will exit from the loop. for example,
i<=9;
Update Expression: After execution of the loop body loop variable is updated by some value
it could be incremented, decremented, multiplied, or divided by any value.
While Loop
While loop does not depend upon the number of iterations. In for loop the number of iterations was
previously known to us but in the While loop, the execution is terminated on the basis of the test
condition. If the test condition will become false then it will break from the while loop else body will
be executed.
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
Comparison between the loop
C Functions
A function in C is a set of statements that when called perform some specific tasks. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain
meanings and performing certain operations. They are also called subroutines or procedures
in other languages.
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls
Function Declarations
In a function declaration, we must provide the function name,
its return type, and the number and type of its parameters. A
function declaration tells the compiler that there is a function
with the given name defined somewhere else in the program.
Syntax
4. return_type name_of_the_function (parameter_1,
parameter_2);
Function Call
A function call is a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.
In the below example, the first sum function is called and 10,30 are passed to the sum
function. After the function call sum of a and b is returned and control is also returned back
to the main function of the program
Note: Function call is neccessary to bring the program control to the function
definition. If not called, the function statements will not be executed.
C Recursion
Recursion is the process of a function calling itself repeatedly till the given condition is
satisfied. A function that calls itself directly or indirectly is called a recursive function and
such kind of function calls are called recursive calls
In C, recursion is used to solve complex problems by breaking them down into simpler sub-
problems. We can solve large numbers of problems using recursion in C. For example,
factorial of a number, generating Fibonacci series, generating subsets, etc.
Recursive Functions in C
In C, a function that calls itself is called Recursive Function. The recursive functions contain a
call to themselves somewhere in the function body. Moreover, such functions can contain
multiple recursive calls