Loops.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
ECE Application Programming
Repetitive Structures
REPETITION CONTROL STRUCTURE
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Lecture 4B More Repetition Richard Gesick
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
LOOPS BY: LAUREN & ROMEO.
Chapter 6: Repetition Statements
CprE 185: Intro to Problem Solving (using C)
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 5: Control Structures II (Repetition)
Functions continued.
Java Programming Loops
Bubble sort.
Arrays.
CISC101 Reminders Quiz 1 marking underway.
ECE 103 Engineering Programming Chapter 18 Iteration
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
The while Looping Structure
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Loops

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

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

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

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

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

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

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

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

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

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

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

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