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

C Theory UNIT3

This document provides an overview of control flow statements in C programming, including if, if-else, nested if-else, if-else-if ladder, switch statements, and various types of loops (for, while, do-while). It also covers nested loops, the use of break and continue statements, and the concept of functions, including built-in and user-defined functions, recursion, and parameter passing methods (call by value and call by reference). Examples are provided to illustrate each concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Theory UNIT3

This document provides an overview of control flow statements in C programming, including if, if-else, nested if-else, if-else-if ladder, switch statements, and various types of loops (for, while, do-while). It also covers nested loops, the use of break and continue statements, and the concept of functions, including built-in and user-defined functions, recursion, and parameter passing methods (call by value and call by reference). Examples are provided to illustrate each concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT 3:

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
}

// C program to illustrate If statement

#include <stdio.h>

int main()

int i = 10;

if (i > 15) {

printf("10 is greater than 15");

printf("I am Not in if");

}
2. if-else in C
The if statement alone tells us that if a condition is true it will execute a block
of statements and if the condition is false it won’t. But what if we want to do
something else when the condition is false? Here comes the
C else statement. We can use the else statement with the if statement to
execute a block of code when the condition is false. The if-else
statement consists of two blocks, one for false expression and one for true
expression.
Syntax of if else in C
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
// C program to illustrate If statement
#include <stdio.h>

int main()
{
int i = 20;

if (i < 15) {

printf("i is smaller than 15");


}
else {

printf("i is greater than 15");


}
return 0;
}
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.
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}
Flowchart of Nested if-else

Example of Nested if-else


C

// C program to illustrate nested-if statement


#include <stdio.h>

int main()
{
int i = 10;

if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");

// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
else {
if (i == 20) {

// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 22)
printf("i is smaller than 22 too\n");
else
printf("i is greater than 25");
}
}

return 0;
}

Output
i is smaller than 15
i is smaller than 12 too
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.

Syntax of if-else-if Ladder


if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

// C program to illustrate nested-if statement


#include <stdio.h>

int main()
{
int i = 20;

if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}

Output
i is 20
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;
}
Note: The switch expression should evaluate to either integer or character. It
cannot evaluate any other data type.

// C Program to illustrate the use of switch statement


#include <stdio.h>

int main()
{
// variable to be used in switch statement
int var = 2;

// declaring switch cases


switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}

return 0;
}

Output
Case 2 is executed

6.For Loop
When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:
Syntax
for (expression 1; expression 2; expression 3) {
// code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4:

Example
int i;

for (i = 0; i < 5; i++) {


printf("%d\n", i);
}

7.While Loop
The while loop loops through a block of code as long as a specified condition
is true:

Syntax
while (condition) {
// code block to be executed
}

In the example below, the code in the loop will run, over and over again, as long
as a variable (i) is less than 5:

Example
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}

8.The Do/While Loop


The do/while loop is a variant of the while loop. This loop will execute the code
block once, before checking if the condition is true, then it will repeat the loop
as long as the condition is true.

Syntax
do {
// code block to be executed
}
while (condition);

The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:

Example
int i = 0;

do {
printf("%d\n", i);
i++;
}
while (i < 5);
1. Nested for Loop

It is also possible to place a loop inside another loop. This is called a nested
loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

Example
int i, j;

// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times

// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
++ pehle hi for loop

2. Nested while Loop


Example

Here’s an example of a nested while loop in C. This program prints a multiplication table
from 1 to 5:

#include <stdio.h>

int main() {
int outer = 1;

// Outer while loop for the multiplication table


while (outer <= 5) {
int inner = 1;

// Inner while loop for each row in the table


while (inner <= 10) {
printf("%d * %d = %d\n", outer, inner, outer * inner);
inner++;
}

// Move to the next number in the outer loop


outer++;
}

return 0;
}

In this example:

 The outer while loop runs from outer = 1 to outer = 5.

 For each value of outer, the inner while loop runs from
inner = 1 to inner = 10, printing the multiplication table
for that specific number.

 The outer loop then increments outer, and the process


repeats until outer becomes greater than 5.

This results in a program that prints the multiplication table


for numbers 1 to 5.

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Each line shows a multiplication expression and its result,


covering the multiplication table from 1 to 5. The outer loop
iterates for each number from 1 to 5, and the inner loop
iterates for each value from 1 to 10, resulting in the complete
multiplication table.
3. Nested do-while Loop
Syntax of nested do-while loop in C:

do {
// Outer loop body
do {
// Inner loop body
} while (inner_condition);
// Updation condition/ Code after the inner loop
} while (outer_condition);

Example:

#include <stdio.h>

int main(){
int i,j;
i=1;
printf("Here is your pattern:\n");

do{ j=1;

do{
printf("* ");
j++;
}while(j<=5);

printf("\n");
i++;
}while(i<=5);

return 0;
}

Semicolon is like empty instruction. If we don't put any instruction after


while or use loop while with {} we must use semicolon to tell compiler
that all we want from while loop is doing this empty instruction.

use of break and continue statements:


Break statement stops the entire process of the loop. Continue
statement only stops the current iteration of the loop. Break also
terminates the remaining iterations. Continue doesn't terminate the
next iterations; it resumes with the successive iterations.

Break
Break was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the for loop when i is equal to 4:

Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
break;
}
printf("%d\n", i);
}
Continue Statement:

https://www.w3schools.com/c/c_break_continue.php

The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.

This example skips the value of 4:

Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
printf("%d\n", i);
}
 What are functions?
Functions are self-contained blocks of code that perform a specific task. They are the
fundamental building blocks of a C program and are used to break down a program
into smaller, more manageable units.
 Types of functions
There are two types of functions in C: built-in and user-defined. Library functions are a
type of built-in function, and examples include ( floor(), ceil(), outs(), gets()) printf(),
and scanf(). User-defined functions are created by the programmer and can be used
multiple times.
 Passing values to functions
When a parameter is passed to a function, it is called an argument. For example, if the
parameter is "name", and the arguments are "Liam", "Jenny", and "Anja", then "name"
is the parameter and "Liam", "Jenny", and "Anja" are the arguments.
 Recursive functions
A recursive function is a function that calls itself again and again, either directly or
indirectly, until the program meets a specific condition
Recursion is the technique of making a function call itself. This technique
provides a way to break complicated problems down into simple problems which
are easier to solve.

Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is
more complicated. In the following example, recursion is used to add a range of
numbers together by breaking it down into the simple task of adding two
numbers:

Example
int sum(int k);

int main() {
int result = sum(10);
printf("%d", result);
return 0;
}

int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}

Example Explained
When the sum() function is called, it adds parameter k to the sum of all numbers
smaller than k and returns the result. When k becomes 0, the function just
returns 0. When running, the program follows these steps:

10 + sum(9)
10 10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 (8) )

...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0

Since the function does not call itself when k is 0, the program stops there and
returns the result.

CALL BY VALUE AND CALL BY REFERENCE:

In C, a function specifies the modes of parameter passing to it. There are two ways to specify
function calls: call by value and call by reference in C. In call by value, the function parameters
gets the copy of actual parameters which means changes made in function parameters did not
reflect in actual parameters. In call by reference, the function parameter gets reference of actual
parameter which means they point to similar storage space and changes made in function
parameters will reflect in actual parameters.

Introduction
Suppose you have a file and someone wants the information present in the file. So to protect
from alteration in the original file, you give a copy of your file to them and if you want the
changes done by someone else in your file then you have to give them your original file. In C
also, if we want the changes done by function to reflect in the original parameters also, then we
pass the parameter by reference, and if we don't want the changes in the original parameter, then
we pass the parameters by value. We get to know about both call by value and call by reference
in c and their differences in upcoming sections.

Difference Between Call by Value and Call by Reference in


C
Calling by Value Calling by Reference
Pass a pointer that contains the memory
Copies the value of an object. address of an object that gives access to its
contents.
Guarantees that changes that alter the state of the Changes that alter the state of the
parameter will only affect the named parameter parameter will reflect to the contents of
bounded by the scope of the function. the passed object.
More difficult to keep track of changing
Simpler to implement and simpler to reason with. values that happens for each time a
function may be called.

Call by Value in C
Calling a function by value will cause the program to copy the contents of an object passed into a
function. To implement this in C, a function declaration has the following form: [return type]
functionName([type][parameter name],...).

Call by Value Example: Swapping the values of the two variables


#include <stdio.h>

void swap(int x, int y){


int temp = x;
x = y;
y = temp;
}

int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output:

Values before swap: x = 10, y = 11


Values after swap: x = 10, y = 11
We can observe that even when we change the content of x and y in the scope of
the swap function, these changes do not reflect on x and y variables defined in the
scope of main. This is because we call swap() by value and it will get separate
memory for x and y so the changes made in swap() will not reflect in main().

Call by Reference in C

Calling a function by reference will give function parameter the address of original parameter
due to which they will point to same memory location and any changes made in the function
parameter will also reflect in original parameters. To implement this in C, a function declaration
has the following form: [return type] functionName([type]* [parameter name],...).

Call by Reference Example: Swapping the values of the two variables


#include <stdio.h>

void swap(int *x, int *y){


int temp = *x;
*x = *y;
*y = temp;
}

int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y);
}

Output:

Values before swap: x = 10, y = 11


Values after swap: x = 11, y = 10

We can observe in function parameters instead of using int x,int y we used int *x,int *y and in
function call instead of giving x,y, we give &x,&y this methodology is call by reference as we
used pointers as function parameter which will get original parameters' address instead of their
value. & operator is used to give address of the variables and * is used to access the memory
location that pointer is pointing. As the function variable is pointing to the same memory
location as the original parameters, the changes made in swap() reflect in main() which we can
see in the above output.
https://www.scaler.com/topics/c/call-by-value-and-call-by-reference-in-c/

You might also like