Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops.

Similar presentations


Presentation on theme: "Loops."— Presentation transcript:

1 Loops

2 Variable initialization
All uninitialized values are random / garbage values! C doesn’t zero-initialize anything unless explicitly told Never use a value from an uninitialized variable Extremely important for counters, sums, products etc. You can initialize variables when declaring. Don’t initialize more than 1 value per line! (coding style) int value = 0; float value = 4.32; char value = "Hello, world!"; 2018 Risto Heinsar

3 Loops Allows repeated execution of a block of code
Loop condition determines for how long the loop runs Loop may never run – condition is false when execution reaches the loop Loop can run infinitely – the condition never becomes false Control statements Break – breaks out of the loop, condition is ignored Continue – forces the next iteration of a loop, skipping any code still to run Loop iterator – a counter to limit for how many times the loop runs It’s possible to nest loops for more complex tasks. 2018 Risto Heinsar

4 Loop condition Condition is checked before the loop body is executed:
While loop For loop Condition is checked after the loop body has executed: do while loop It’s possible for while and for loops to never run. This happens if the condition is false when code execution reaches the loop Do while loop will always run at least once! 2018 Risto Heinsar

5 while loop (checked before execution)
while (condition) { loop body } 2018 Risto Heinsar

6 do while loop (checked after execution)
{ loop body } while (condition); 2018 Risto Heinsar

7 for loop (checked before execution)
for (initialization; condition; afterthought) { loop body } 2018 Risto Heinsar

8 Math The right side of the equal sign (rvalue) is evaluated (calculated) and stored to the variable on the left (lvalue) c = a + b; c = c + b; c += b; Incrementing and decrementing variables a = a + 1; a += 1; a++; a--; NB! Don’t forget to initialize products, sums etc! 2018 Risto Heinsar

9 Examples of using loops
while do while for i = 0; while (i < 10) { printf("%d\n", i); i++; } i = 0; do { printf("%d\n", i); i++; } while (i < 10); for (i = 0; i < 10; i++) { printf("%d\n", i); } 2018 Risto Heinsar

10 Things to consider When will my loop run? Will my loop run at all?
When will my loop stop? Will my loop stop at all? Does my loop run exactly the correct amount of times? off-by-one errors 2018 Risto Heinsar

11 Home task Create a simple calendar application, that asks the user for the day of the week and time The day of the week is outputted based on the number input from user (e.g. 2 – Tuesday) The location/appointment/task is outputted based on the time specified (e.g. T 8:00 – Programming I lecture at U05-103) Advanced: write the program in a loop, so that the day and time can be queried again without exiting (rerunning) the program. Create an algorithm based on the code You made in the lab (sum) Use a modelling tool this time Make 3 versions total - with each of the loops Use swim lanes to divide your program into segments 2018 Risto Heinsar

12 Lab task Create a program that asks for the user for 5 numbers and adds them up The current sum is displayed after every addition Create the same program using three different types of loops! 2018 Risto Heinsar

13 Lab task (advanced, more points)
Using nested loops, create a filled triangle, base length 10 pcs In the end of every row, show the number of blocks in this row and total used Still boring? Mirror the triangle to start from the right …or do it both ways in a single run Ask the user for base length 2018 Risto Heinsar


Download ppt "Loops."

Similar presentations


Ads by Google