You are on page 1of 23

APTITUDE TRAINING

(Technical Part)
At 6th Semester of Graduate Programme

in all Branches of Engineering / Technology

Instructor : Dr. Somsubhra Gupta, IT Dept. JISCE


Venue: Dr. B.C. Roy Auditorium.

Technical Aptitude Training JISCE

2015

Technical Session 3
(03
(03.02.2015
(03.02.2015)
02 2015)
2015)

(Loop)

Technical Aptitude Training JISCE

2015

Session Objective
To use the while repetition statement to
execute
t statements
t t
t in
i a program repeatedly.
t dl
Counter-controlled repetition and sentinelcontrolled
t ll d repetition.
titi
Additional repetition control structures
-for
f
-dowhile
Technical Aptitude Training JISCE

2015

Th while
The
hil Statement
S

Repetition structure
Programmer specifies an action to be repeated while
some condition remains true
Pseudocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
while

loop repeated until condition becomes false

Example:
int product = 2;
while ( product <= 1000 )
product
d
= 2 * product;
d
Technical Aptitude Training JISCE

2015

Common Programming error


Not pproviding
g the body
y of a while statement with an
action that eventually causes the condition in the
while to become false. Normally, such a repetition
structure will never terminatean error called an
infinite loop.
Spelling the keyword while with an uppercase W as in
While (remember that C is a case-sensitive language).
All off C
Cs reserved
d kkeywords
d such
h as while,
hil if andd
else contain only lowercase letters.

Technical Aptitude Training JISCE

2015

Flow Chart

Technical Aptitude Training JISCE

2015

Counter Controlled Repetition

Counter-controlled repetition
Loop repeated until counter reaches a certain value
Definite repetition: number of repetitions is known
Example: A class of ten students took a quiz. The
grades (integers in the range 0 to 100) for this quiz
are available to you. Determine the class average
on the quiz.
q

Technical Aptitude Training JISCE

2015

Sample source code


1

/* Fig. 3.6: fig03_06.c

2
3

Class average program with counter-controlled repetition */


#include <stdio.h>

Outline

4
5

/* function main begins program execution */

6
7
8

int main( void )


{
int counter; /* number of grade to be entered next */

9
10

int grade;
i
d
int total;

/* grade
/
d value
l
*/
/
/* sum of grades input by user */

11

int average; /* average of grades */

fig03_06.c

(1 of 2 )
Counter to control while loop

12
13
14

/* initialization phase */
total = 0;
/* initialize total */

15

counter = 1; /* initialize loop counter */

Initialize counter to 1

16
17
18

/* processing phase */
while ( counter <= 10 ) {

/* loop 10 times */
/
/

19
20

printf( "Enter grade: " ); /* prompt for input */


scanf( "%d", &grade );
/* read grade from user */

21
22

total = total + grade;


counter = counter + 1;

23

/* add grade to total */


/* increment counter */

} /*
/ end
d while
hil */
/

Technical Aptitude Training JISCE

2015

while loopp iterates as long


g as
counter <= 10

Increment the counter

Sample source code


24
25

/* termination phase */

26

average = total / 10; /* integer division */

O li
Outline

27
28
29

printf( "Class average is %d\n", average ); /* display result */

30
31

return 0; /* indicate program ended successfully */

fig03 06.c
fig03_06.c

(2 of 2 )

32 } /* end function main */


Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Class

Calculate the average

grade: 98
grade: 76
grade: 71
grade: 87
grade: 83
grade: 90
grade: 57
grade: 79
grade: 82
grade: 94
average is 81

Technical Aptitude Training JISCE

2015

Repetition Essentials
Loop
Group of instructions computer executes repeatedly while
some condition remains true
Counter-controlled repetition
p
Definite repetition: know how many times loop will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
U d when
Used
h number
b off repetitions
titi
nott known
k
Sentinel value indicates "end of data"

Technical Aptitude Training JISCE

2015

10

Counter Controlled repetition

Counter-controlled repetition requires


The name of a control variable (or loop counter)
The initial value of the control variable
An increment (or decrement) by which the
control variable is modified each time through the
loop
A condition that tests for the final value of the
control
t l variable
i bl (i.e.,
(i whether
h th looping
l
i should
h ld
continue)

Technical Aptitude Training JISCE

2015

11

Counter Controlled repetition

Example:
int counter = 1;
// initialization
while ( counter <= 10 ) { // repetition condition
printf( "%d\n", counter );
++counter;
;
// increment
}

The statement
int counter = 1;

Names counter
Defines it to be an integer
Reserves space for it in memory
Sets it to an initial value of 1

Technical Aptitude Training JISCE

2015

12

f Statements
for
S

Technical Aptitude Training JISCE

2015

13

Using an incorrect relational operator or using an


incorrect initial or final value of a loopp counter in the
condition of a while or for statement can cause off-byone errors.
Using the final value in the condition of a while or
for statement and using the <= relational operator will
help avoid off-by-one errors. For a loop used to print
the values 1 to 10,, for example,
p , the loop-continuation
p
condition should be counter <= 10 rather than
counter < 11 or counter < 10.
Technical Aptitude Training JISCE

2015

14

for Repetition statement

Format when using for loops


( initialization; loopContinuationTest; increment )
statement

for

E ample:
Example:
for( int counter = 1; counter <= 10; counter++ )
printf( "%d\n", counter );

Prints the integers from one to ten

Technical Aptitude Training JISCE

2015

15

for Repetition statement


For loops can usually be rewritten as while loops:
initialization;
while ( loopContinuationTest ) {
statement;
increment;
}
Initialization and increment
C be
Can
b comma-separatedd lists
li
Example:
for (int i = 0, j = 0; j + i <
<= 10;
j++, i++)
printf( "%d\n", j + i );

Technical Aptitude Training JISCE

2015

16

for Repetition statement


Arithmetic expressions
Initialization loop
loop-continuation
continuation, and increment can contain
- Initialization,
arithmetic expressions. If x equals 2 and y equals 10
for ( j = x; j <= 4 * x * y; j += y / x)
i equivalent
is
i l to for
f
( j = 2;
2 j <= 80;
80 j += 5 )
Notes about the for statement:
Increment may be negative (decrement)
- "Increment"
- If the loop continuation condition is initially false
The body of the for statement is not performed
Control proceeds with the next statement after the for
statement
- Control variable often printed or used inside for body, but not
necessary
Technical Aptitude Training JISCE

2015

17

for Flow Chart

Technical Aptitude Training JISCE

2015

18

d
do-while
hil repetition
i i statement

The dowhile repetition statement


Similar
Si
il to the
h while structure
Condition for repetition only tested after the body of
th loop
the
l
is
i performed
f
d
All actions are performed at least once

Format:
do {

statement;
} while ( condition );

Technical Aptitude Training JISCE

Example (letting counter = 1):


do {
printf( "%d ", counter );
} while (++counter <= 10);
Prints the integers from 1 to 10

2015

19

d
do-while
hil flow
fl chart
h

Technical Aptitude Training JISCE

2015

20

PracticeCode
Calculate sum & average of N numbers using Array
Calculate
C l l
mean, variance,
i
standard
d d deviation
d i i off N numbers
b
Calculate Sum of digits of a given number
Prime numbers below 200
Find Length of String
Find maximum value

Technical Aptitude Training JISCE

2015

21

Assignment:Day3
1. List all the prime numbers between two given numbers.
2. Write a program, that accepts a integer from the user and print the
integer
g with reverse digits.
g For eg:
g rev ((1234)) = 4321.
3. Find the sum of the digits of a given number. Find the digit of
convergence
4. Given a number,, determine its absolute value.
5. Given three numbers, determine whether they can form the sides of a
triangle.
6. Given a number, determine whether it is a valid year and if so, whether
it is a leap year.
7. Given two numbers, determine whether they are valid years, and if so,
list all leap years between the two years (both included).
included)
Technical Aptitude Training JISCE

2015

22

You might also like