Title: Programming
1Programming
2Incremental Operators
i a ?? i i a i - a ?? i i -
a i a ?? i i a i / a ?? i i /
a i or i ?? i i 1 ?? i 1 i-- or
--i ?? i i 1 ?? i - 1
3Loops
- Used to repeat the same code segmaent over and
over again.
Block of code Some changing state
Loop while some condition holds
4Loops
- C provides some flexible ways of deciding how
many times to loop, or when to exit a loop. - for, while, do-while loops.
5while loops
- while (expression)
- statement
- The statement is executed as long as condition
is true. - The loop terminates when the condition is no
longer true.
6Example - Factorial
- int main()
-
- int i 0, n 0, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- i 1 / this is the counter /
- while (i lt n)
-
- fact i
- i
- printf("the factorial is d\n", fact)
- return 0
7Example Fibonacci series
0 1 1 2 3 5 8 13 21 34
0 1
0 1 1 2 3 5 8 13 21
0 1 1 2
0 1 1
0 1 1 2 3 5 8 13 21 34
8Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
0
1
---
Screen
0
9Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib1 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
0
1
---
Screen
0
10Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
0
1
---
Screen
0 1
11Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
0
1
1
Screen
0 1
12Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
1
Screen
0 1
13Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
1
Screen
0 1
14Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
1
Screen
0 1
15Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
1
Screen
0 1 1
16Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
2
Screen
0 1 1
17Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
1
2
Screen
0 1 1
18Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
2
2
Screen
0 1 1
19Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
2
2
Screen
0 1 1
20Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
2
2
Screen
0 1 1 2
21Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
1
2
3
Screen
0 1 1 2
22Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
2
2
3
Screen
0 1 1 2
23Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
2
3
3
Screen
0 1 1 2
24Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
2
3
3
Screen
0 1 1 2
25Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
2
3
3
Screen
0 1 1 2 3
26Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
2
3
5
Screen
0 1 1 2 3
27Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
3
3
5
Screen
0 1 1 2 3
28Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
3
5
5
Screen
0 1 1 2 3
29Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
3
5
5
Screen
0 1 1 2 3
30Fibonacci step by step
lim
fib1
fib2
fib_next
- int fib1 0, fib2 1, fib_next 0,
lim - ...
- printf("d ", fib1)
- while (fib2 lt lim)
-
- printf("d ", fib2)
- fib_next fib1 fib2
- fib1 fib2
- fib2 fib_next
-
- printf("\n")
5
3
5
5
Screen
0 1 1 2 3
31getchar
- 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)
??
32putchar
- putchar(char c) prints out the character inside
the brackets. - Requires including stdio.h
- Similar to printf.
char c putchar(c)
char c printf("c", c)
??
33Example - Copy
- int main()
-
- int c
- c getchar()
- while (c ! '\n')
-
- putchar(c)
- c getchar()
-
- return 0
-
34Exercise Low to Up
- Read input from the user until end-of-line (\n)
encountered. - Convert lowercase letters to uppercase ones.
35Solution Low to Up
- int main()
-
- int c 0
- printf ("Please enter a string ")
- c getchar()
- while (c ! '\n')
-
- if (c gt 'a' c lt 'z')
- putchar(c - 'a' 'A')
- else
- putchar(c)
- c getchar()
-
- putchar('\n')
- return 0
36break 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.
37Example what does this do?
- int main()
-
- int i 0, c
- printf("Enter a line of text\n")
- c getchar()
- while (c ! '\n')
- if (c ' ')
- break
- i
- c getchar()
-
-
38Example Counting Letters
- int main()
-
- int counter 0, c
- printf("Enter a line of text\n")
- c getchar()
- while (c ! '\n')
- if (c ' ') / found the first space - exit
the loop / - break
- counter
- c getchar()
-
- if (c ' ') / we found a space /
- printf("There are d letters before the
first space.\n", counter) - else / The loop ended without
finding spaces / - printf("There are no spaces in the input
line.\n") - return 0
39continue
- When continue is encountered, the rest of the
loop body is ignored. - The program then continues to run from the
beginning of the loop.
40What does it do?
- int main()
-
- int i
- i 1
- while (i lt 10)
-
- if (i 2 0)
- break
- printf("d\n", i)
- i
-
- printf("The End\n")
- return 0
1The End
41and this?
- int main()
-
- int i
- i 1
- while (i lt 10)
-
- if (i 2 0)
- continue
- printf("d\n", i)
- i
-
- printf("The End\n")
- return 0
Infinite Loop!
42for loops
- for (ltinitializationgt ltconditiongt ltincrementgt)
statement(s)
43Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
---
---
1
44Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
---
3
1
45Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
1
---
3
1
46Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
1
3
1
47Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
2
3
1
48Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
2
---
3
1
49Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
2
3
2
50Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
3
3
2
51Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
3
3
6
52Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
4
3
6
53Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
4
---
3
6
54Example - Factorial Using for
i
n
fact
- int main()
-
- int i, n, fact 1
- printf("Enter a number\n")
- scanf("d", n)
- for (i 1 i lt n i)
-
- fact i
-
- printf("the factorial is d\n", fact)
- return 0
4
---
3
6
55What does it do?
- int main()
-
- int i
- for (i 1 i lt 10 i)
-
- if (i 2 0)
- break
- printf("d\n", i)
-
- printf("The End\n")
- return 0
1The End
56and this?
- int main()
-
- int i
- for (i 1 i lt 10 i)
-
- if (i 2 0)
- continue
- printf("d\n", i)
-
- printf("The End\n")
- return 0
13579The End
57Nested Loops
- Loops can be nested
- For every time in the external loop we make a
complete iteration of the internal one.
58Example - Multiplication Table
- int main()
-
- int row, col
- for (row 1 row lt 10 row)
-
- for (col 1 col lt 10 col)
-
- printf("3d", row col)
-
- printf("\n")
-
- return 0
59Output
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20
- 3 6 9 12 15 18 21 24 27 30
- 4 8 12 16 20 24 28 32 36 40
- 5 10 15 20 25 30 35 40 45 50
- 6 12 18 24 30 36 42 48 54 60
- 7 14 21 28 35 42 49 56 63 70
- 8 16 24 32 40 48 56 64 72 80
- 9 18 27 36 45 54 63 72 81 90
- 10 20 30 40 50 60 70 80 90 100
60Example Reverse Triangle
- int main()
-
- int i 0, j 0
- int size 0
- printf("Please enter a size\n")
- scanf("d", size)
- for (i 0 i lt size i)
-
- for (j i j lt size j)
-
- printf("")
-
- printf("\n")
-
- return 0
Please enter a size 5
61Exercise Prime Numbers
- Write a program that prints all the prime numbers
smaller than or equal to N. - N is given by the user (input)
- How do you go over all the numbers lt N?
- How do you know if a number is prime?
62Solution Prime Numbers
- int main()
-
- int candidate 0, divisor 0, last 0
- printf("Enter a number\n")
- scanf("d", last)
- for (candidate 2 candidate lt last
candidate) -
- for (divisor 2 divisor lt candidate
divisor) -
- if (candidate divisor 0)
- break
-
- if (divisor candidate)
- printf("the number d is prime\n",
candidate) -
- return 0
63do-while loops
Statement
- do
- statement(s)
- while (condition)
- 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
condition
true
false
64Example Waiting for Legal Input
- int main()
-
- int i 0
- printf("Please enter a positive number.\n")
- do
- scanf("d", i)
- if (i lt 0)
- printf("Try again.\n")
- while (i lt 0)
- / The program continues.... /
- return 0
-