Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

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.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 Repetition structures Overview while statement for statement do while statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 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.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Loops and Iteration for Statements, while Statements and do-while Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Lab 4: Loops and Iteration Graham Northup
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
CS161 Introduction to Computer Science
Loop Structures.
Chapter 2.2 Control Structures (Iteration)
Lecture 07 More Repetition Richard Gesick.
LOOPS.
Lecture 4B More Repetition Richard Gesick
Chapter 8 Repetition Statements
Lecture 4A Repetition Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Module 4 Loops.
Lec 4: while loop and do-while loop
Chapter 2.2 Control Structures (Iteration)
Loop Strategies Repetition Playbook.
Computing Fundamentals
CHAPTER 21 LOOPS 1.
PROGRAM FLOWCHART Iteration Statements.
Chapter 3 Flow of Control Loops in Java.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Week 6 - Monday

 What did we talk about last time?  while loop examples  Lab 5

 Loops can go on forever if you aren’t careful int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); //supposed to print all the numbers //less than 40, but i never increases } int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); //supposed to print all the numbers //less than 40, but i never increases }

 Overflow and underflow will make some badly written loops eventually terminate int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); i--; //whoops, should have been i++ } int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); i--; //whoops, should have been i++ }

 Being off by one is a very common loop error int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; } int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; }

 If the condition isn’t true to begin with, the loop will just be skipped int n = 40; int i = 1; while( i >= 40 ) //oops, should be <= { System.out.println(i); i++; } int n = 40; int i = 1; while( i >= 40 ) //oops, should be <= { System.out.println(i); i++; }

 A misplaced semicolon can cause an empty loop body to be executed (often infinitely) int n = 40; int i = 1; while( i <= 40 ); //semicolon is wrong { System.out.println(i); i++; } int n = 40; int i = 1; while( i <= 40 ); //semicolon is wrong { System.out.println(i); i++; }

 The condition of the while loop is not followed by a semicolon  Be careful about starting and ending conditions  When in doubt, use braces  The print statement must be inside the loop in order to get printed multiple times  There’s no magic formula; you have to think it through

1. while loops  Used when you don’t know how many times you are going to need to repeat 2. for loops  Used when you do know how many times you are going to repeat 3. do-while loops  Used never  Oh, okay, they are used whenever you need to be guaranteed the loop runs at least once

 Any problem that uses loops can use any kind of loop  The choice is supposed to make things easier on the programmer  Some loops are more convenient for certain kinds of problems

 for loops are great when you know how many times a loop will run  They are the most commonly used of all loops  They are perfect for any task that needs to run, say, 100 times  A for loop has 3 parts in its header: 1. Initialization 2. Condition 3. Increment

Way to Progress Ending Point Starting Point for( init; condition; inc ) { statement1; statement2; … statementn; }

 A for loop will usually have multiple statements in its body  However, it is possible to make a for loop with only a single statement  Then, like if -statements and while -loops, the braces are optional for( init; condition; inc ) statement;

 Let’s print the numbers from 1 to 100 (again)  Remember how this was done with while : int i = 1; while( i <= 100 ) { System.out.println(i); i++; } int i = 1; while( i <= 100 ) { System.out.println(i); i++; }

 A for loop is specifically designed for this sort of thing:  The initialization and the increment are built- in for( int i = 1; i <= 100; i++ ) { System.out.println(i); } for( int i = 1; i <= 100; i++ ) { System.out.println(i); }

 Ask the user to input a positive integer n  Now, write a for loop to print out the first n odd numbers  Example: If the user enters 10, print out:

 We can do something called a Monte Carlo approximation of π  We “throw” darts at a 1 x 1 square in the upper right corner of a circle with radius 1  We count the ones that fall inside the circle and divide by the total darts thrown  That fraction is an estimation of the area of one fourth of the circle  By multiplying by 4, we approximate π y x

 They work just like while loops  The only difference is that they are guaranteed to execute at least once  Unlike a while loop, the condition isn’t checked the first time you go into the loop  Sometimes this is especially useful for getting input from the user  The syntax is a little bit different

do { statement1; statement2; … statementn; } while( condition );

 Examples with for loops and do-while loops

 Keep reading Chapter 5 of the textbook  Keep working on Project 2