session 2 loops and flow control
TRANSCRIPT
WWW.RNSANGWAN.COM 1
Session 2 Loops and Control flow in CA COMPLETE C PROGRAMMING SERIES FROM BASICS TO ADVANCED
2
Looping Constructs• Executing a set of statements repeatedly until a specified condition
is satisfied. The segment of code executed repeatedly is termed as a Loop.o while - loopo for - loopo do – while loop
WWW.RNSANGWAN.COM
3
while Loop - Syntax
Initialization step;
while (condition)
{
Statements to be repeated;
Re-Initialization Step;
}
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 4
While Loop Flow
Diagram
5
for Loop - Syntax
for ( Initialization step; conditional step; re-initialization step )
{
Statements to be repeated;
}
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 6
Flow Diagram for loop
7
do-while Loop - Syntax
Initialization step;
do
{
Statements to be repeated;
Re-Initialization Step;
} while (condition);
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 8
Flow Diagram do-while loop
9
break & continue statements in loop constructs
• When a break statement is encountered inside a loop, the loop is terminated and control passes to the statement following the loop body.
• The continue statement forces the next iteration of the loop to take place, skipping any code following the continue statement in the loop body.
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 10
Break statement
WWW.RNSANGWAN.COM 11
continue statement
12
exit ()
• Is a library function which terminates the execution of the program itself.
WWW.RNSANGWAN.COM
13
goto statement
• It allows us to push the program control to any statement in the program.
goto <label-name>; where, label-name with a colon is used somewhere in the program.
WWW.RNSANGWAN.COM
14
Arrays
• Storing a collection of elements of homogenous data type in contiguous memory locations referenced by a single name.
• Individual items in array can be referred to by an array name and a subscript or index.
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 15
Array Memory Map
4 Bytes for Integer With some Starting Address say “a” (let it be 100)
Address will be a+4 (if a is 100 it will be 116)
Value will be *(a+4) or a[4] or 4[a] or *(4+a)
16
Array handling in C Array declaration :-int i[5];
Array initialization :-static int i[5] = {1, 2, 3, 4, 5};
Entering Data into Array :-i[3] = 10;
Reading data from Array :-j = i[3];
WWW.RNSANGWAN.COM
17
Complete Program using Arrays
void main()
{
int i[5], j;for (j=0; j<5; j++)
i[ j ] = j+1;for (j=0; j<5; j++)
printf ( “%d”, i[ j ]); }
WWW.RNSANGWAN.COM
18
Some facts about Arrays in C
• Array elements are given garbage values till initialized.• Bounds checking is not taken care of regarding arrays in C.
Hence, this issue becomes the botheration of the programmer.• String constant is a one-dimensional array of characters
terminated by null.
WWW.RNSANGWAN.COM
19
Two Dimensional Arrays Referred as a Matrix. Array declaration :-int i [5] [3]; 5 by 3 2-D matrix
Array initialization :-static int i [5] [2] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
Entering Data into Array :-i[3][4] = 20;
Reading data from Array :-j = i[3][4];
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 20
Two dimensional
Arrays
21
Modular Programming• A long continuous program is breaked up into a series of individual
modules.• All these modules are related to each other in a specified fashion.• Called as sub-programs or functions.
WWW.RNSANGWAN.COM
22
Functions in C• A function is a self-contained block of code performing a coherent
task of some kind.• Any C program contains at least one function – main(). • All other functions are called from main() itself.
WWW.RNSANGWAN.COM
23
Function declaration and Function definition• Function declaration specifies the interpretation & attributes of a set of
identifiers. It alludes to a function that is defined elsewhere in the program. void abc ( int a );
• Function definition defines what function does as well as the kind of data it expects & returns.
void abc ( int a ) Function Header
{
… Function Body
}
WWW.RNSANGWAN.COM
24
The Function Headerint AddNum(int a, int b)• This is the first line of the function. It consists of 3 parts :
◦ The type of the return value.◦ The name of the function.◦ The parameters of the function.
WWW.RNSANGWAN.COM
25
The Function Body• The statements in the Function Body perform the desired
computation in a function body following the function header.• All the variables declared within the body of the function, as well as
the parameters are local to the function.
WWW.RNSANGWAN.COM
26
Functions with Arguments• Mechanism to convey information to called functions from caller
functions. void abc ( int a ) ; Called Function
void main( ) Caller Function { a is a formal argument
int b = 10; abc (b); b is an actual argument
}
WWW.RNSANGWAN.COM
WWW.RNSANGWAN.COM 27
End of
Session 02