PPS Unit-3
PPS Unit-3
PPS Unit-3
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:
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;
}
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
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.
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.