CS110D: PROGRAMMING LANGUAGE I

Similar documents
Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 3 Structured Program Development

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Fundamentals of Programming Session 7

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Chapter 3 Structured Program Development in C Part II

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

C Programming for Engineers Structured Program

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Structured Program Development in C

Quiz Determine the output of the following program:

Chapter 4: Control structures. Repetition

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Chapter 4: Control structures

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

In this chapter you will learn:

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

C++ Programming: From Problem Analysis to Program Design, Third Edition

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Outline. Program development cycle. Algorithms development and representation. Examples.

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Loops / Repetition Statements

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction. C provides two styles of flow control:

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Repetition and Loop Statements Chapter 5

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS112 Lecture: Repetition Statements

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter 4 C Program Control

Control Statements. Musa M. Ameen Computer Engineering Dept.

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

REPETITION CONTROL STRUCTURE LOGO

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Information Science 1

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Information Science 1

Looping. Arizona State University 1

Structured Program Development

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Visual Basic 2010 How to Program by Pearson Education, Inc. All Rights Reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Loops. CSE 114, Computer Science 1 Stony Brook University

Chapter 2 - Control Structures

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Multiple-Subscripted Arrays

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Programming for Engineers Iteration

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2 - Control Structures

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Control Structures II. Repetition (Loops)

Introduction to C Programming

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

LECTURE 5 Control Structures Part 2

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

CS112 Lecture: Loops

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Lecture 7 Tao Wang 1

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

CS110: PROGRAMMING LANGUAGE I

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CS1150 Principles of Computer Science Loops (Part II)

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

STUDENT LESSON A12 Iterations

Chapter 2 - Control Structures

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

JavaScript: Control Statements I

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Control Statements: Part 1

COP 1220 Introduction to Programming in C++ Course Justification

Why Is Repetition Needed?

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 5: Loops and Files

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Chapter 4 C Program Control

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Project 1. due date Sunday July 8, 2018, 12:00 noon

Transcription:

CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops

Lecture Contents Why loops?? While loops for loops do while loops Nested control structures

Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a hundred times. It would be tedious to have to write the following statement a hundred times: System.out.println("Welcome to Java!"); So, how do you solve this problem?

Solution

Iterate 5 A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions. yes Dr. Hamad, Dr. Khalifa

While loops 6 Pseudocode while the product price is less than 100 SAR buy three of them while ( product <= 100 ) product = 3 * product; Dr. Hamad, Dr. Khalifa

4.7 while Repetition Statement Repetition statement repeats an action while a condition remains true. The repetition statement s body may be a single statement or a block. Eventually, the condition will become false. At this point, the repetition terminates, and the first statement after the repetition statement executes.

Syntax

Be careful!!

4.9 Sentinel-controlled repetition Sentinel-controlled repetition is often called indefinite repetition because the number of repetitions is not known before the loop begins executing. A special value called a sentinel value (also called a signal value, a dummy value or a flag value) can be used to indicate end of data entry. A sentinel value must be chosen that cannot be confused with an acceptable input value.

Sentinel-controlled repetition Program logic Reads the first value before reaching the while. This value determines whether the program s flow of control should enter the body of the while. If the condition of the while is false, the user entered the sentinel value, so the body of the while does not execute (i.e., no grades were entered). If the condition is true, the body begins execution and processes the input. Then the loop body inputs the next value from the user before the end of the loop.

Example: Class Average Develop a class-averaging program that processes grades for an arbitrary number of students each time it is run.

Algorithm : Pseudo code

Tip!!

Program Code Variables Initialization Input Sentinel = -1

Program Code Check division by zero explicit type casting

while : Counter-Controlled Repetition 2-17 Consider: int count; count = 7; // Initialization while (count > 0) // Loop Condition { System.out.println("Hi ); // Loop Body count--; // Update expression } Loop body executes how many times? definite repetition, because the number of repetitions is known before the loop begins executing. Use sentinel values for indefinite repetition

Example: class average A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. The class average is equal to the sum of the grades divided by the number of students.

Algorithm: Pseudo Code Now, modify the code given before!!

Be careful!!

for: Counter-Controlled Repetition Initialization Condition update for (Init; Bool_Exp; Update) Body_Statement

Syntax

Example

Scope of a variable A variable s scope defines where it can be used in a program. A local variable can be used only in the method that declares it and only from the point of declaration through the end of the method.

Example Reimplement the class average program using for loops

for loops Specifies the counter-controlled-repetition details in a single line of code natural for counting The initialization, loop-continuation condition and increment can contain arithmetic expressions. Example: Assume that x = 2 and y = 10 for (int j = x; j <= 4 * x * y; j += y / x) is equivalent to the statement for (int j = 2; j <= 80; j += 5)

5.4 Examples Using the for Statement Vary the control variable from 1 to 100 in increments of 1. for ( int i = 1; i <= 100; i++ ) Vary the control variable from 100 to 1 in decrements of 1. for ( int i = 100; i >= 1; i-- ) Vary the control variable from 7 to 77 in increments of 7. for ( int i = 7; i <= 77; i += 7 )

5.4 Examples Using the for Statement Vary the control variable from 20 to 2 in decrements of 2. for ( int i = 20; i >= 2; i -= 2 ) Vary the control variable over the values 2, 5, 8, 11, 14, 17, 20. for ( int i = 2; i <= 20; i += 3 ) Vary the control variable over the values 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. for ( int i = 99; i >= 0; i -= 11 )

5.5 do while Repetition Statement the loop-continuation condition after executing the loop s body; therefore, the body always executes at least once.

do-while Loop Syntax 2-30 Dr. Hamad, Dr. Khalifa

Example Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

break and Continue break; Forces loop to exit immediately. Execution continues with the first statement after the control statement. continue; Skips rest of loop body In while and do while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

Example:

Example Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

4.10 Nested Control Statements Recall: ANY valid java statements can be inside body of loop This includes conditions and additional loop statements! Called "nested control structures Example: for (outer=0; outer<5; outer++) for (inner=7; inner>2; inner--) System.out.println(outer+, + inner);

4.10 Nested Control Statements A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, ten of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You ve been asked to write a program to summarize the results. You ve been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam or a 2 if the student failed.

Example: Your program should analyze the results of the exam as follows: Input each test result (i.e., a 1 or a 2). Display the message Enter result on the screen each time the program requests another test result. Count the number of test results of each type. Display a summary of the test results, indicating the number of students who passed and the number who failed. If more than eight students passed the exam, print the message Bonus to instructor!

Sample Runs

Algorithm: Pseudocode

Program Code Variables initialization

Program Code An if nested inside a loop

Debugging Syntax errors (e.g., when one brace in a block is left out of the program) are caught by the compiler. A logic error (e.g., when both braces in a block are left out of the program) has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing but causes it to produce incorrect results.

That s all [1] 4.7, 4.8, 4.9, 4.10, 5.3, 5.4, 5.5, 5.7