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

Preview:

Citation preview

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

Loops

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

Block of code

Some changing state

Loop while some condition holds

Loops

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

for, while, do-while loops.

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.

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;

}

Example – fibonacci series

fibonacci.c

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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);====

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);====

Example – lower-case to upper case.

low2up.c

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

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

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

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

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

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

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');

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');

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');

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');

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');

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');

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');

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');

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');

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');

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');

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');

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');

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');

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!

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;

}

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.

Example – counting letters

break.c

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.

for loops

for loops are controlled by a counter variable.

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

{

loop body

}

initialization condition increment

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;}

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;}

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;}

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;}

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;}

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;}

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;}

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

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

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;}

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.

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;

}

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");}

}

Exercise

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

The height of the pyramid is the input.

*****

*****

****

*

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;}

Exercise

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

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;

}

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.

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;

}

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;

}

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

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;

}

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

Debugging

Debuggers are software tools designed to help find software bugs

Both Visual C and the lcc compiler include a debugger

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…

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.

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.

Recommended