Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Midterm Review Sana Odeh.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Introduction to Computers and Programming Lecture 7:
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 5 Loops.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 5: Control Structures II
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Application development with Java Lecture 6 Rina Zviel-Girshin.
Introduction to Computers and Programming Lecture 7:
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Java Fundamentals 4.
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
The for-loop and Nested loops
Control Statements Loops.
Control Statements Loops.
Review of Previous Lesson
Loops CGS3416 Spring 2019 Lecture 7.
Control Statements:.
Presentation transcript:

Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University

review What are the three parts of every loop? Where are these three parts in a for loop? Where are these three parts in a while loop? Which two parts of the for loop will always be executed? True or False: You must know at compile time how many iterations you want to execute when using a for loop?

Road map Nested for loops break continue Reading: –Liang 5: Chapter 3: 3.3.4; 3.5; 3.6 –Liang 6: Chapter 4: 4.6; 4.9 –Liang 7: Chapter 4: 4.6; 4.9

Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows = 1; rows <= 5; rows++) { for (columns=1; columns<=10; columns++) System.out.print ("*"); System.out.println (); } Outer Loop Inner Loop Output:**************************************************

Nested For Loops, Example #2 int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) System.out.print ("*"); System.out.println (); } Output:*************** Outer Loop Inner Loop

break and continue

break; We have seen the keyword break used in a switch statement: switch (userInput) { case 1: userInput++; break; } You can also use break inside of a for, while or do/while loop to immediately exit from the loop.

Example of break use public class Break { public static void main (String [] args) { int x; for ( x = 1 ; x <= 10; x++) { if (x == 5) break; System.out.println("The number is : " + x); } /* end for x = 1... */ }

continue Continue is a statement which can be used inside a for, while or do/while loop in order to skip the rest of the remaining code in the loop, and start the next iteration.

Example of continue public class Continue { public static void main (String [] args) { int x; for ( x = 1 ; x <= 10; x++) { if (x == 5) continue ; System.out.println("The number is : " + x); } /* end for x = 1... */ }

Java Keywords  2003 Prentice Hall, Inc. All rights reserved.

Problem Use a while loop with a sentinel to read in letter grades of the form A, B, C, D, F. Convert the grades to numerical equivalents, based on the following table, then print out the average grade in numerical form Num FDCBALetter

Structured Programming: Summary

Summary Sequence –Statement follow one another Selection Structures –if –if/else –if/else if/else –switch Repetition Structures –while –do/while –for

midterm Tuesday, March 10 th during class You will have the entire class period to complete the exam 5short answer(total of 15 points) 1find the errors(15 points) 1 || 2medium answer(20 points) 2write a program(total of 50 points) –Remember the difference between write the program and write the main method.

Midterm material From the book: –All the sections listed in each of the slide presentations. –Reading the case studies in the chapters is a good idea even though we did not cover all of them in class. That does not mean I will ask you to write one of those programs. The practice is good –I also suggest you practice problems from the end of each chapter. Anything in the class notes. Next class: Thursday, March 5 will be a review session where we practice exam-like questions.