Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PPS Unit-3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Loops in C

Loops in programming come into use when we need to repeatedly execute a block of statements. There are
mainly two types of loops:

1. Entry Controlled loops: In this type of loops the test condition is tested before entering the
loop body. For Loop and While Loop are entry controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is tested or evaluated at the end of loop
body. Therefore, the loop body will execute atleast once, irrespective of whether the test condition is
true or false. do – while loop is exit controlled loop.

for loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
The syntax of a for loop in C programming language is −
for ( initialization; condition; increment/decrement(updation) )
{
statements;
}
For examples-
#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:

1
2
3
while loop:
A while loop in C programming repeatedly executes a target statement as long as a given condition
is true.
The syntax of a while loop in C programming language is −
Initialization;
while(condition)
{
statements;
increment/decrement(updation);
}
For example-
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
Output:

1234

do...while loop:
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C
programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least
one time.
The syntax of a do...while loop in C programming language is −
Initialization;
do {
statements;
increment/decrement(updation);
} while( condition );
For example –
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=2);
return 0;
}
Output:

Value of variable j is: 0


Value of variable j is: 1
Value of variable j is: 2
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop
executes once before the condition is tested.
nested loop:
C programming allows to use one loop inside another loop. The following section shows a few
examples to illustrate the concept.
The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ) {

for ( init; condition; increment ) {


statements;
}
statements;
}
For example -
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %d\n",i ,j);
}
}
return 0;
}
Output:

0, 0
0, 1
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3

The syntax for a nested while loop statement in C programming language is as follows −
while(condition) {

while(condition) {
statements;
}
statements;
}
The syntax for a nested do...while loop statement in C programming language is as follows −
do {
statements;

do {
statements;
}while( condition );
}while( condition );
PPS NOTES BY PRADEEP KUMAR SHARMA

A final note on loop nesting is that you can put any type of loop inside any other type of loop.

C – break statement:
The break statement is used inside loops and switch case.
1. It is used to come out of the loop instantly, it is same as set to loop’s condition to false . When a
break statement is encountered inside a loop, the control directly comes out of loop and the loop gets
terminated. It is used with if statement, whenever used inside loop.
2. This can also be used in switch case control structure. Whenever it is encountered in switch-case
block, the control comes out of the switch-case(see the example below).For example -

#include <stdio.h>
int main()
{
int var;
for (var =100; var>=10; var --)
{
printf("var: %d\n", var);
if (var==99)
{
break;
}
}
printf("Out of for-loop");
return 0;
}
Output:

var: 100
var: 99
Out of for-loop

C – Continue statement:
The continue statement is used inside loops. When a continue statement is encountered inside a
loop, control jumps to the beginning of the loop for next iteration, skipping the execution of statements
inside the body of loop for the current iteration. for example -
#include <stdio.h>
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
{

continue;
}

printf("%d ", j);


}
return 0;
}
Output: 0 1 2 3 5 6 7 8

4 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Functions(Module)
Function is set of statements that do a particular task. Function provide reusability, easy error
handling.
Types of function
There are two types of Function
1. Predefined( library function ) e.g. scanf(), getch(), sqrt() etc.
2. User defined (defined by user)-these are 4 types-
a. With return type –with parameter
b. With return type –without parameter
c. Without return type –with parameter
d. Without return type –without parameter
We can make program either by using function or without using function, using function we
have some important advantages.
Benefits of Using Functions
1. It provides modularity to your program's structure.
2. It makes your code reusable. You just have to call the function by its name to use it,
wherever required.
3. In case of large programs with thousands of code lines, debugging and editing becomes
easier if you use functions.
4. It makes the program more readable and easy to understand.

Function parts
For creating function we use 3 things or parts
 Declaration of function
 Definition of function
 Calling of function
Declaration of function
Like any variable or an array, a function must also be declared before its used. Function
declaration informs the compiler about the function name, parameters is accept, and its return
type. The actual body of the function can be defined separately. It's also called as Function
Prototype. Function declaration consists of 4 parts.
 returntype
 function name
 parameter list

5 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

 terminating semicolon
Return type
Return type specifies the type of value(int, float, char, double) that function is expected to
return to the program which called the function.
Note: In case your function doesn't return any value, the return type would be void.

Function Name
Function name is an identifier and it specifies the name of the function. The function name is
any valid C identifier and therefore must follow the same naming rules like other variables in C
language.
Parameter list
The parameter list declares the type and number of arguments that the function expects when it
is called.

Syntax: return_type function_name (parameter list)


Example: int add(int a, int b);
Definition of function Terminating semicolon
We define the Functions in last block of structure of C.
Syntax:
return_type function_name (parameter list)
{
It is Function header. It is
Set of statements same as declaration of
} function without semicolon;
Example of with return type –with parameter
int add(int a, int b)
{
int c;
c=a+b;
return c;
}
Calling a function
When a function is called, control of the program gets transferred to the function. For calling
function we give parameter
For example: for above function, we call as
int s = add(5,6); //it returns 11 to s

6 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

program to multiply two values using function


#include<stdio.h>
int main()
{
int multiply(int a, int b); // function declaration
int i, j, result;
printf("Please enter 2 numbers you want to multiply...");
scanf("%d%d", &i, &j);
result = multiply(i, j); // function call
printf("The result of muliplication is: %d", result);
return 0;
}
int multiply(int a, int b) // function defintion
{
return (a*b);
}
Parameter
Parameter is data value or variable that is used in function. It is also called argument. There are
two types of parameter-
 Actual parameter (it is parameter by which we call the function)
 Formal parameter (it is parameter which is declared in function header)
Parameter passing
It is the way by which we call the function. There are two way for calling a function –
1) Call by value
In the call by value method the actual arguments are copied to the formal arguments, hence
any operation performed by function on formal arguments doesn’t affect actual parameters.
For example: swaping two values using call by value
#include<stdio.h>
int main()
{
void swap(int a, int b); // function declaration
int i,j;
printf("Please enter 2 numbers you want to swap \n");
scanf("%d%d", &i, &j);
swap(i, j); // function call with actual parameter
printf("actual parameter after function call –i=%d,j=%d”,i,j);
return 0;
}
void swap(int a, int b) // function defintion
{
int c;
c = a;
a = b;
b = c;
printf("value after swap -%d,%d”,a,b);

7 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA
Actual parameter i, j
remains unchanged by
} changing formal
Output:
Please enter 2 numbers you want to swap parameter a, b
5
6
value after swap -6,5
actual parameter after function call –i=5,j=6
2) Call by reference or call by address
in this method, address of actual arguments (or parameters) is passed to the formal
parameters, which means any operation performed on formal parameters affects the value
of actual parameters
For example: swaping two values using call by reference
#include<stdio.h>
int main()
{
void swap(int *a, int *b); // function declaration with pointer to store address
int i,j;
printf("Please enter 2 numbers you want to swap \n");
scanf("%d%d", &i, &j);
swap(&i, &j); // function call with address
printf("actual parameter after function call –i=%d,j=%d”,i,j);
return 0;
}
void swap(int *a, int *b) // function defintion
{
int c;
c = *a;
*a = *b;
*b = c; Actual parameter i, j
printf("value after swap -%d,%d”,a,b); has been changed by
}
Output: changing of formal
Please enter 2 numbers you want to swap parameter a, b
5
6
value after swap -6,5
actual parameter after function call –i=6,j=5
Recursive function
If function calls itself then it is known as “Recursion“. We call that function a recursive
function.
For example: factorial of a positive number using recursive function
#include<stdio.h>
int main()
{
long fact(int n);
long f;
int a;
printf("Please enter number \n");
scanf("%d", &a);

8 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

f=fact(a);
printf("factorial =%ld”,f);
return 0;
}
long fact(int n) // function defintion
{
if(n= =0) Function calls itself
return 1;
else
return n*fact(n-1);
}
Output:
Please numbers
5
Factorial =120
Function with array
We can pass array as function parameter and function can return array also. To do this we
pass only name of the array without any index or size since name of the array is base
address of array.

9 PPS NOTES BY PRADEEP KUMAR SHARMA

You might also like