Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
While loops.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 5: Structured Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
The ‘while’ loop ‘round and ‘round we go.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Week 3 Part 2 Kyle Dewey.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 6 Repetition Richard Gesick.
Lecture 7: Repeating a Known Number of Times
Loop Structures.
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Looping.
Arrays, For loop While loop Do while loop
For & do/while Loops.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Example Problems for Exam#2 (covers chapters 5, 6, 7, 8, 9)
Chapter 2.2 Control Structures (Iteration)
Lab5 PROGRAMMING 1 Loop chapter4.
Relational, Logical, and Equality Operators
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
Loops.
Repetition Statements (Loops) - 2
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
Chapter 3 Flow of Control Loops in Java.
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
Presentation transcript:

Week 4 Kyle Dewey

Overview New office hour location while / do-while / for loops break / continue Termination Exam recap

Office Hour

Motivation Factorial 5! = 5 * 4 * 3 * 2 * 1 = 120 int factorial( int x ) { // return x! (x factorial) }

Factorial Need a way to say “for each integer from x down to 1, decrementing by 1 at a time” 5! = 5 * 4 * 3 * 2 * 1 = 120

while int x = 0; while ( x < 5 ) oneStatement(); while ( x < 5 ) { statementOne(); statementTwo(); }

while Semantics Loop Body Continue? Rest of Program True False

Example #1 int x = 0; while ( x < 5 ) { x++; } What does x equal at loop end?

Example #2 int x = 0; while ( x < 5 ) { x = x + 2; } What does x equal at loop end?

Example #3 int x = 0; while ( x < 5 ) { x = 10; printf( “moo” ); } What does x equal at loop end? What does this print?

Factorial Revisited A lot of different ways to implement it One possible way: Track which number we are looking at in one variable Track the result in another 5! = 5 * 4 * 3 * 2 * 1 = 120

Factorial Revisited int factorial( int fact ) { int result = fact; int num = fact - 1; while ( num > 1 ) { result = result * num; num--; } return result; } 5! = 5 * 4 * 3 * 2 * 1 = 120

Factorial Revisited int factorial( int fact ) { int result = 1; int num = 2; while( num <= fact ) { result = result * num; num++; } return result; } 5! = 1 * 2 * 3 * 4 * 5 = 120

do / while

Essentially a while loop with the condition at the end The body will always execute at least once int x = 0; do { statementOne(); statementTwo(); } while ( x > 0 ) ; Note semicolon!

Example #1 int x = 0; do { x++; } while ( x < 5 ); What does x equal at loop end?

Example #2 int x = 0; do { x++; } while ( x < 0 ); What does x equal at loop end? int x = 0; while ( x < 0 ) { x++; }

for

int x; for ( x = 0; x < 50; x++ ) oneStatement( x ); for( x = 0; x < 50; x++ ) { statementOne(); statementTwo(); }

for Semantics Continue? Loop Body Inc / Dec Initializer Rest of Program True False

Initializer Run before the loop starts int x; for ( x = 0 ; x < 50; x++ ) oneStatement( x ); Initializer Rest of Loop

Compare Run before each iteration of the loop int x; for ( x = 0; x < 50 ; x++ ) oneStatement( x ); Terminate? Loop Body Rest of Program True False

Loop Body Run during each iteration of the loop int x; for ( x = 0; x < 50; x++ ) oneStatement( x ); Terminate? Loop Body Rest of Program True False

Increment / Decrement Run after each iteration of the loop int x; for ( x = 0; x < 50; x++ ) oneStatement( x ); Loop Body Inc / Dec

Example #1 int x; for ( x = 0; x < 5; x++ ) { printf( “foobar” ); } What does this print?

Example #2 int x; for ( x = 0; x < 5; x++ ) { printf( “%i\n”, x ); } What does this print?

Example #3 int x; int y = 0; for ( x = 0; x < 5; x++ ) { y++; } What does y equal at loop end?

Example #4 int x; int y = 0; for ( x = 1; x < 4; x++ ) { y++; } What does y equal at loop end?

Example #5 int x; int y = 0; for ( x = 1; x % 3 != 0; x++ ) { y++; } What does y equal at loop end?

for Header It is not required to specify an increment, compare, or counter The semicolon still needs to be provided Can be tricker to read and understand

for Header int x; for ( x = 0; x < 5; x++ ) { printf( “moo” ); }...is effectively the same as... int x = 0; for ( ; x < 5; x++ ) { printf( “moo” ); }

for Header int x; for ( x = 0; x < 5; x++ ) { printf( “moo” ); }...is effectively the same as... int x = 0; for ( ; x < 5; ) { printf( “moo” ); x++; }

Factorial Revisited A lot of different ways to implement it One possible way: Track which number we are looking at in one variable Track the result in another 5! = 5 * 4 * 3 * 2 * 1 = 120

Factorial Revisited int factorial( int fact ) { int result = fact; int num; for( num = fact - 1; num > 1; num-- ){ result = result * num; } return result; } 5! = 5 * 4 * 3 * 2 * 1 = 120

Factorial Revisited int factorial( int fact ) { int result = 1; int num; for( num = 2; num <= fact; num++ ){ result = result * num; } return result; } 5! = 1 * 2 * 3 * 4 * 5 = 120

break break can be used to immediately exit from a loop Can make things easier when used carefully

Example #1 int x = 0; while ( x < 5 ) { break; }

Example #2 int x = 0; while ( x < 5 ) { x = x + 2; if ( x >= 5 ) break; printf( “moo” ); }

Example #3 int x; for ( x = 0; x < 5; x = x + 2 ) { if ( x >= 5 ) break; printf( “moo” ); }

continue continue causes the loop to skip the rest of the body Increments / decrements still performed for for loops Conditions will be rechecked

Example #1 int x = 0; while ( x < 10 ) { x++; continue; printf( “moo” ); }

Example #2 int x; for( x = 0; x < 10; x++ ) { if ( x % 2 == 0 ) continue; printf( “%i\n”, x ); }

Termination Consider the following: while ( 1 ) { printf( “moo\n” ); }

Termination Some loops may never terminate This may be desired...or not

Termination Useful example char* command; while ( 1 ) { printCommandPrompt(); command = readCommand(); executeCommand( command ); }

Termination Likely an error...or perhaps a clever way of checking for overflow and maximum int size int x = 0; int y = 1; while ( x < y ) { x++; y++; }

Infinite Loops Loops that never terminate are called infinite loops Usually in this context it is a bug Can be nasty to debug Complex termination conditions Is it just taking awhile?

Random Notes on Loops

Intentional Nontermination while loop: while ( 1 ) {... } for loop: for ( ;; ) {... }

for vs. while Each can be expressed in terms of the other Choose based on what seems more appropriate

while as for while ( x < 5 ) {... } Can be represented as: for (; x < 5;) {... }

for as while int x; for ( x = 0; x < 5; x++ ) {... } Can be represented as: (except for continue behavior!) int x = 0; while ( x < 5 ) {...; x++; }

Exam Recap

Difficulty Level Too hard? Too easy? Just right?

Grading Hopefully by Tuesday, if not Tuesday then Thursday Depending on how the grades turn out: No curve Curve Change weights Your grade can only improve with said changes

Exam Solutions