27
Session 2 Loops and Control flow in C A COMPLETE C PROGRAMMING SERIES FROM BASICS TO ADVANCED WWW.RNSANGWAN.COM 1

Session 2 loops and flow control

Embed Size (px)

Citation preview

Page 1: Session 2 loops and flow control

WWW.RNSANGWAN.COM 1

Session 2 Loops and Control flow in CA COMPLETE C PROGRAMMING SERIES FROM BASICS TO ADVANCED

Page 2: Session 2 loops and flow control

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

Page 3: Session 2 loops and flow control

3

while Loop - Syntax

Initialization step;

while (condition)

{

Statements to be repeated;

Re-Initialization Step;

}

WWW.RNSANGWAN.COM

Page 4: Session 2 loops and flow control

WWW.RNSANGWAN.COM 4

While Loop Flow

Diagram

Page 5: Session 2 loops and flow control

5

for Loop - Syntax

for ( Initialization step; conditional step; re-initialization step )

{

Statements to be repeated;

}

WWW.RNSANGWAN.COM

Page 6: Session 2 loops and flow control

WWW.RNSANGWAN.COM 6

Flow Diagram for loop

Page 7: Session 2 loops and flow control

7

do-while Loop - Syntax

Initialization step;

do

{

Statements to be repeated;

Re-Initialization Step;

} while (condition);

WWW.RNSANGWAN.COM

Page 8: Session 2 loops and flow control

WWW.RNSANGWAN.COM 8

Flow Diagram do-while loop

Page 9: Session 2 loops and flow control

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

Page 10: Session 2 loops and flow control

WWW.RNSANGWAN.COM 10

Break statement

Page 11: Session 2 loops and flow control

WWW.RNSANGWAN.COM 11

continue statement

Page 12: Session 2 loops and flow control

12

exit ()

• Is a library function which terminates the execution of the program itself.

WWW.RNSANGWAN.COM

Page 13: Session 2 loops and flow control

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

Page 14: Session 2 loops and flow control

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

Page 15: Session 2 loops and flow control

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)

Page 16: Session 2 loops and flow control

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

Page 17: Session 2 loops and flow control

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

Page 18: Session 2 loops and flow control

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

Page 19: Session 2 loops and flow control

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

Page 20: Session 2 loops and flow control

WWW.RNSANGWAN.COM 20

Two dimensional

Arrays

Page 21: Session 2 loops and flow control

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

Page 22: Session 2 loops and flow control

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

Page 23: Session 2 loops and flow control

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

Page 24: Session 2 loops and flow control

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

Page 25: Session 2 loops and flow control

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

Page 26: Session 2 loops and flow control

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

Page 27: Session 2 loops and flow control

WWW.RNSANGWAN.COM 27

End of

Session 02