incremental operators used as a short-hand i++ or ++i == i = i + 1 i-- or --i == i = i – 1 i...

85
Incremental operators Used as a short-hand i++ or ++i == i = i + 1 i-- or --i == i = i – 1 i += a == i = i + a i -= a == i = i - a

Upload: abel-goodman

Post on 02-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Incremental operators

Used as a short-hand

i++ or ++i == i = i + 1i-- or --i == i = i – 1i += a == i = i + ai -= a == i = i - ai *= a == i = i * ai /= a == i = i / a

Page 2: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Loops

Used to repeat the same instruction(s) over and over again.

Block of code

Some changing state

Loop while some condition holds

Page 3: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Loops

C provides some flexible ways of deciding how many times to loop, or when to exit a loop.

for, while, do-while loops.

Page 4: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

while loops

while (condition)

{

statement(s);

}

The statements are executed as long as condition is true

When the condition is no longer true, the loop is stopped.

Page 5: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example - factorial

#include <stdio.h>int main(){

int i, n, fact = 1;printf("Enter a number\n");scanf("%d", &n);i=1; /* this is the counter */while (i<=n){

fact = fact*i;i++; /* equivalent to i = i+1 */

}printf("the factorial is %d\n", fact);return 0;

}

Page 6: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example – fibonacci series

fibonacci.c

Page 7: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0

Screen

5

lim0

fib11

fib2---

fib_next

Page 8: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0

Screen

5

lim0

fib11

fib2---

fib_next

Page 9: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim0

fib11

fib2---

fib_next

Page 10: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim0

fib11

fib21

fib_next

Page 11: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

Page 12: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

Page 13: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1

Screen

5

lim1

fib11

fib21

fib_next

Page 14: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib21

fib_next

Page 15: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib22

fib_next

Page 16: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib11

fib22

fib_next

Page 17: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib12

fib22

fib_next

Page 18: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1

Screen

5

lim1

fib12

fib22

fib_next

Page 19: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim1

fib12

fib22

fib_next

Page 20: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim1

fib12

fib23

fib_next

Page 21: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib12

fib23

fib_next

Page 22: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib13

fib23

fib_next

Page 23: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2

Screen

5

lim2

fib13

fib23

fib_next

Page 24: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim2

fib13

fib23

fib_next

Page 25: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim2

fib13

fib25

fib_next

Page 26: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib13

fib25

fib_next

Page 27: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

Page 28: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

Page 29: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Fibonacci – step by step

fib1 = 0;fib2 = 1;

printf("%d ", fib1);

while (fib2 < lim){

printf("%d ", fib2);fib_next = fib1 + fib2;fib1 = fib2;fib2 = fib_next;

}

printf("\n");

0 1 1 2 3

Screen

5

lim3

fib15

fib25

fib_next

Page 30: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

getchar

getchar() gets a single character from the user.

Requires including stdio.h Returns a non-positive number on failure. Similar to scanf.

char c;

c = getchar();

char c;

scanf(“%c”, &c);====

Page 31: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Putchar

putchar(char) prints out the character inside the brackets.

Requires including stdio.h Similar to printf.

char c;

putchar(c);

char c;

printf(“%c”, c);====

Page 32: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example – lower-case to upper case.

low2up.c

Page 33: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

#include <stdio.h>

int main(){ char c; char upper_c;

printf(“Enter a string: ");

c = getchar();

Buffer

‘#’

‘@’

c upper_c

Screen

Page 34: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

#include <stdio.h>

int main(){ char c; char upper_c;

printf(“Enter a string: ");

c = getchar();

yeS\n

Buffer

‘#’

‘@’

c upper_c

Screen

Page 35: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

#include <stdio.h>

int main(){ char c; char upper_c;

printf (“Enter a string: ");

c = getchar();

eS\n

Buffer

‘y’ ‘@’

c upper_c

Screen

Page 36: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

eS\n

Buffer

‘y’ ‘@’

c upper_c

Screen

Page 37: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

eS\n

Buffer

‘y’ ‘@’

c upper_c

Screen

Page 38: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

eS\n

Buffer

‘y’ ‘Y’

c upper_c

Screen

Page 39: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

eS\n

Buffer

‘y’ ‘Y’

c upper_c

Y

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 40: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

S\n

Buffer

‘e’ ‘Y’

c upper_c

Y

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 41: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

S\n

Buffer

‘e’ ‘Y’

c upper_c

Y

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 42: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

S\n

Buffer

‘e’ ‘Y’

c upper_c

Y

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 43: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

S\n

Buffer

‘e’ ‘E’

c upper_c

Y

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 44: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

S\n

Buffer

‘e’ ‘E’

c upper_c

YE

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 45: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

\n

Buffer

‘S’ ‘E’

c upper_c

YE

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 46: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

\n

Buffer

‘S’ ‘E’

c upper_c

YE

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 47: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

\n

Buffer

‘S’ ‘E’

c upper_c

YE

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 48: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

\n

Buffer

‘S’ ‘S’

c upper_c

YE

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 49: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

\n

Buffer

‘S’ ‘S’

c upper_c

YES

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 50: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

Buffer

‘\n’ ‘S’

c upper_c

YES

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 51: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

Buffer

‘\n’ ‘S’

c upper_c

YES

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 52: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Low2up – step by step

Buffer

‘\n’ ‘S’

c upper_c

YES

Screen

while (c != '\n' && c >= 0){ if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c;

/* Print the converted character.*/ putchar(upper_c);

/* Get the next character */ c = getchar();

}putchar('\n');

Page 53: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Exercise

Input: Two integers – A and B

Output: How many times A contains B This is the result of the integer division

A/B Note:

Do not use the division operator!

Page 54: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Solution

#include <stdio.h>

int main(){

int a, b, res;

printf("Please enter two numbers.\n");scanf("%d%d", &a, &b);

res = 0;while ( (res+1) * b <= a)

res = res + 1;

printf("%d / %d = %d", a, b, res);return 0;

}

Page 55: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

break in a loop

When break is encountered, the loop is stopped regardless of whether the condition is still true.

The program then continues to run from the first line after the while loop.

If called within a nested loop, break breaks out of the inner loop only.

Page 56: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example – counting letters

break.c

Page 57: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

continue

When continue is encountered, the rest of the loop body is ignored.

The program then continues to run from the beginning of the loop.

Page 58: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

for loops

for loops are controlled by a counter variable.

for (c = begin; c <= end; c += inc)

{

loop body

}

initialization condition increment

Page 59: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

The factorial example again, this time using for

#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 60: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

---

i

3

n

1

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 61: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

1

i

3

n

1

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 62: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

1

i

3

n

1

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 63: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

2

i

3

n

1

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 64: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

2

i

3

n

2

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 65: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

3

i

3

n

2

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 66: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

3

i

3

n

6

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Factorial with for – step by step

Page 67: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

4

i

3

n

6

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Factorial with for – step by step

Page 68: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Factorial with for – step by step

4

i

3

n

6

fact#include <stdio.h>int main(){ int i, n, fact = 1;

printf("Enter a number\n"); scanf("%d", &n);

for (i=1; i<=n; ++i) { fact *= i; }

printf("the factorial is %d\n", fact); return 0;}

Page 69: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

for loops (cont.)

Equivalent to while. Any for loop can be converted to while loop and vice versa

Some applications are more natural to for, and others to while.

If we want to perform something for a predefined number of times, better use for.

If we just wait for something to happen (not after a certain number or iterations), better use while.

Page 70: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example – fahrenheit-celsius conversion table

/* Print a Fahrenheit-to-Celsius conversion table */

#include <stdio.h> int main ( ) {

int fahr;double celsius; int lower = 0, upper = 300;int step = 20;

for (fahr=lower; fahr<=upper; fahr += step){

celsius = 5.0*(fahr -32.0)/9.0;printf("%d\t%g\n", fahr, celsius);

}return 0;

}

Page 71: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Nested for loop – rectangle example/* Print a rectangle of *. The height and width are defined by the

user */#include <stdio.h>

int main( ){

int i, j;int height, width;

printf("Please enter the two box dimensions: \n");scanf("%d%d", &height, &width);

for (i = 1; i <= height; i++){

for (j = 1; j <= width; j++) printf("*");

printf("\n");}

}

Page 72: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Exercise

Write a program that prints an upside-down half triangle of *.

The height of the pyramid is the input.

*****

*****

****

*

Page 73: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Solution

#include <stdio.h>

int main(){

int i, j, size;

printf(“Please enter a size:\n”);scanf(“%d”,&size);for (i = 1; i <= size; i++){

for (j = i; j <= size; j++) printf("*");

printf("\n");}

return 0;}

Page 74: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Exercise

Write a program accepts a number from the user, and prints out all of the prime numbers up to that number.

Page 75: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Solution

#include <stdio.h>int main(){

int i, j, last;

printf("enter a number\n");scanf("%d", &last);for (i = 2; i <= last; i++){

for (j = 2 ; j < i; j++){ if (i % j == 0)

break;}if (j == i) printf("the number %d is prime\n", i);

}return 0;

}

Page 76: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Exercise

Change the former program, so that is displays only the largest prime number which is smaller than or equal to the user’s input.

Page 77: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Solution 1#include <stdio.h>int main(){

int i, j, last;int found = 0; /* This indicates if we found the largest prime */

printf("enter a number\n");scanf("%d", &last);i = last;while (!found) /* Loop until we find our guy */{

for (j = 2 ; j < i; j++) if (i % j == 0) break;

if (j == i) /* If this is true then i is prime */ found = 1;else i--;

}printf("The largest prime not larger than %d is %d.\n", last, i);return 0;

}

Page 78: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Solution 2 (with break)#include <stdio.h>int main(){

int i, j, last;printf("enter a number\n");scanf("%d", &last);for (i=last; i>1; i--){

for (j = 2 ; j < i; j++) if (i % j == 0) break;

if (j == i) /* i is prime. We found our guy */ break;

}printf("The largest prime not larger than %d is %d.\n",

last, i);return 0;

}

Page 79: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

do while loops

do {statement(s)

} while (expression);

Similar to while loops Except the condition is evaluated after the loop

body The loop body is always executed at least once,

even if the expression is never true

Page 80: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Example – waiting for legal input

#include <stdio.h>int main(){

int i;

printf("Please enter a positive number.\n");do {

scanf("%d", &i); if (i <= 0)

printf("Try again.\n");

} while (i<=0);

/* The program continues.... */return 0;

}

Page 81: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Debugging

It is virtually impossible to program without errors

Syntax errors are detected by the compiler

However, often a program has no syntax errors and compiles, but still doesn’t perform as desired

Page 82: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Debugging

Debuggers are software tools designed to help find software bugs

Both Visual C and the lcc compiler include a debugger

Page 83: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

Debugging

The debugger allows us to – Execute the program one line at a time At each step see the values of all

variables and expressions Run the program up to a pre-specified

point And more…

Page 84: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

The debugger’s common features

Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line.

Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.

Page 85: Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=

The debugger’s common features (cont.)

Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point.

Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or

debug window at the bottom. The yellow arrow indicates our whereabouts

at any given moment.