A Look Back at Arithmetic Operators: the Increment and Decrement - - PDF document

a look back at arithmetic operators the increment and
SMART_READER_LITE
LIVE PREVIEW

A Look Back at Arithmetic Operators: the Increment and Decrement - - PDF document

1/21/2016 A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate on a single operand.


slide-1
SLIDE 1

1/21/2016 1

A Look Back at Arithmetic Operators: the Increment and Decrement

Spring Semester 2016 Programming and Data Structure 27

Increment (++) and Decrement (--)

  • Both of these are unary operators; they
  • perate on a single operand.
  • The increment operator causes its operand

to be increased by 1.

– Example: a++, ++count

  • The decrement operator causes its operand

Spring Semester 2016 Programming and Data Structure 28

to be decreased by 1.

– Example: i--, --distance

slide-2
SLIDE 2

1/21/2016 2

  • Operator written before the operand (++i, --i))

– Called pre-increment operator. Called pre increment operator. – Operator will be altered in value before it is utilized for its intended purpose in the program.

  • Operator written after the operand (i++, i--)

– Called post-increment operator.

Spring Semester 2016 Programming and Data Structure 29

– Operator will be altered in value after it is utilized for its intended purpose in the program.

Examples

Initial values :: a = 10; b = 20; x = 50 + ++a; a = 11 x = 61 x = 50 + ++a; a = 11, x = 61 x = 50 + a++; x = 60, a = 11 x = a++ + --b; b = 19, x = 29, a = 11 x = a++ – ++a; Undefined value (implementation

Spring Semester 2016 Programming and Data Structure 30

; ( p dependent)

Called side effects:: while calculating some values, something else get changed.

slide-3
SLIDE 3

1/21/2016 3

Control Structures that Allow Repetition

Spring Semester 2016 Programming and Data Structure 31

Types of Repeated Execution Types of Repeated Execution

  • Loop: Group of instructions that are

executed repeatedly while some condition remains tr e remains true. How loops are controlled?

Sentinel Sentinel Controlled Controlled Counter Counter Controlled Controlled Condition Condition Controlled Controlled

slide-4
SLIDE 4

1/21/2016 4

  • Counter-controlled repetition

– Definite repetition – know how many times loop will execute. execute. – Control variable used to count repetitions.

  • Condition-controlled repetition

– Loop executes as long as some specified condition is true.

  • Sentinel-controlled repetition

Spring Semester 2016 Programming and Data Structure 33

p

– Indefinite repetition. – Used when number of repetitions not known. – Sentinel value indicates “end of data”.

Counter-controlled Repetition

  • Counter-controlled repetition requires

– name of a control variable (or loop counter). name of a control variable (or loop counter). – initial value of the control variable. – condition that tests for the final value of the control variable (i.e., whether looping should continue). – increment (or decrement) by which the control

Spring Semester 2016 Programming and Data Structure 34

( ) y variable is modified each time through the loop.

slide-5
SLIDE 5

1/21/2016 5

Counter Controlled Loop Counter Controlled Loop

Read 5 integers and display the value of their ti counter ← 1, sum ← 0 summation. counter < 6 sum ← sum + n false true input n

int counter=1, sum=0; while (counter <6 ) { scanf (“%d”, &n); +

sum sum n counter++

  • utput sum

sum = sum + n; counter++; } printf (“\nSum is: %d”, sum); int counter, sum=0; for (counter=1;counter<6; counter++) { scanf (“%d”, &n); sum = sum + n; } printf (“\nSum is: %d”, sum);

Spring Semester 2016 Programming and Data Structure 36

slide-6
SLIDE 6

1/21/2016 6

while Statement

  • The “while” statement is used to carry out

looping operations, in which a group of statements is e ec ted repeatedl as long as statements is executed repeatedly, as long as some condition remains satisfied.

while (condition) statement_to_repeat; while (condition) { statement_1;

Spring Semester 2016 Programming and Data Structure 37

... statement_N; }

C statement(s) true false Single-entry / single-exit structure

Spring Semester 2016 Programming and Data Structure 38

statement(s)

slide-7
SLIDE 7

1/21/2016 7

while :: Examples

int digit = 0; while (digit <= 9) int weight=100; while ( weight > 65 ) ( g ) printf (“%d \n”, digit++); ( g ) { printf ("Go, exercise, "); printf ("then come back. \n"); printf ("Enter your weight:"); scanf ("%d", &weight); }

Spring Semester 2016 Programming and Data Structure 39

Example: Sum of N Natural Numbers

START

int main () { int N, count, sum; scanf (“%d”, &N) ; sum = 0; 1

READ N SUM = 0 COUNT = 1 SUM = SUM + COUNT COUNT = COUNT + 1

count = 1; while (count <= N) { sum = sum + count; count = count + 1; } printf (“Sum=%d\n”, sum); return 0; }

IS COUNT > N? OUTPUT SUM STOP

YES NO

slide-8
SLIDE 8

1/21/2016 8

Example: Maximum of inputs

printf (“Enter positive numbers, end with -1.0\n”); max = 0.0; f(“%f” & t) scanf(“%f”, &next); while (next != -1.0) { if (next > max) max = next; scanf(“%f”, &next); } i f ( h i b i %f\ ) printf (“The maximum number is %f\n”, max) ;

Example of Sentinel-controlled loop Inputs: 10 5 100 25 68 -1

do-while Statement

  • Similar to “while”, with the difference that

the check for continuation is made at the d f h end of each pass.

– In “while”, the check is made at the beginning.

  • Loop body is executed at least once.

do { statement-1; do statement_to_repeat;

Spring Semester 2016 Programming and Data Structure 42

statement-2; statement-n; } while (condition ); while (condition );

slide-9
SLIDE 9

1/21/2016 9 C statement(s) false Single-entry / single-exit structure

Spring Semester 2016 Programming and Data Structure 43

true

do-while :: Examples

int digit = 0; do printf (“%d \n”, digit++); while (digit <= 9); int weight; do { printf ("Go, exercise, ");

Spring Semester 2016 Programming and Data Structure 44

printf ( Go, exercise, ); printf ("then come back. \n"); printf ("Enter your weight: "); scanf ("%d", &weight); } while (weight > 65 ) ;

slide-10
SLIDE 10

1/21/2016 10

for Statement

  • The “for” statement is the most commonly

used looping structure in C.

  • General syntax:

for (expression1; expression2; expression3) statement-to-repeat; for (expression1; expression2; expression3) {

Spring Semester 2016 Programming and Data Structure 45

{ statement_1; : statement_N; }

  • How it works?

– “expression1” is used to initialize some variable (called index) that controls the looping action. – “expression2” represents a condition that must be true for the loop to continue. – “expression3” is used to alter the value of the index initially assigned by “expression1”.

Spring Semester 2016 Programming and Data Structure 46

int digit; for (digit=0; digit<=9;digit++) printf (“%d \n”, digit); int digit; for (digit=9;digit>=0;digit--) printf (“%d \n”, digit);

slide-11
SLIDE 11

1/21/2016 11 expression1 Single-entry / single-exit structure expression2 t t t( ) true false

Spring Semester 2016 Programming and Data Structure 47

statement(s) expression3

for :: Examples

int fact = 1, i, N; scanf (“%d”, &N); int sum = 0, N, count; scanf (“%d”, &N); ( , ); for (i=1; i<=N; i++) fact = fact * i; printf (“%d \n”, fact); ( , ); for (i=1; i<=N, i++) sum = sum + i * i; printf (“%d \n”, sum);

Compute factorial

Spring Semester 2016 Programming and Data Structure 48

Sum of squares of N natural numbers

slide-12
SLIDE 12

1/21/2016 12

2-D Figure

Print * * * * *

#define ROWS 3 #define COLS 5

* * * * * * * * * *

#define COLS 5 .... for (row=1; row<=ROWS; row++) { for (col=1; col<=COLS; col++) { printf(“*”); } printf(“\n”); }

Another 2-D Figure

Print * * *

#define ROWS 5

* * * * * * * * * * * * * *

.... int row, col; for (row=1; row<=ROWS; row++) { for (col=1; col<=row; col++) { printf(“* ”); } printf(“\n”); }