V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

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

Theory of control structures

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Introduction. C provides two styles of flow control:

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

Chapter 2: Functions and Control Structures

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

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

In this chapter you will learn:

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

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

REPETITION CONTROL STRUCTURE LOGO

Chapter 3 Structured Program Development

Structured Programming. Dr. Mohamed Khedr Lecture 9

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Why Is Repetition Needed?

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

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

Structured Program Development

CS110D: PROGRAMMING LANGUAGE I

Repetition Structures

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

Programming for Engineers Iteration

Control Structures II. Repetition (Loops)

Repetition and Loop Statements Chapter 5

C Programming for Engineers Structured Program

CS1100 Introduction to Programming

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

n Group of statements that are executed repeatedly while some condition remains true

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

CS 106 Introduction to Computer Science I

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

Introduction to the Java Basics: Control Flow Statements

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

DELHI PUBLIC SCHOOL TAPI

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

For Loop. Variations on Format & Specific Examples

Flow Control. CSC215 Lecture

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Repetition, Looping CS101

Control Structures of C++ Programming (2)

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

CHAPTER 9 FLOW OF CONTROL

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

Iterative Languages. Scoping

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

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

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

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

CS112 Lecture: Repetition Statements

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

SELECTION STATEMENTS:

More on control structures

Control Statements. Musa M. Ameen Computer Engineering Dept.

LECTURE 5 Control Structures Part 2

3 The L oop Control Structure

Chapter 4 C Program Control

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

Chapter 5: Control Structures

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

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Problem Solving With Loops

Unit 3 Decision making, Looping and Arrays

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

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

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

CGS 3066: Spring 2015 JavaScript Reference

Controls Structure for Repetition

YOLOP Language Reference Manual

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

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

Repetition, Looping. While Loop

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

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

Chapter 5: Control Structures II (Repetition)

A Look Back at Arithmetic Operators: the Increment and Decrement

Decision Making and Loops

Loops. CSE 114, Computer Science 1 Stony Brook University

Problem Solving and 'C' Programming

Module 4: Decision-making and forming loops

Lecture 7 Tao Wang 1

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

CS 106 Introduction to Computer Science I

204111: Computer and Programming

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

Computer Programming I - Unit 5 Lecture page 1 of 14

Subject: PIC Chapter 2.

Lecture Transcript While and Do While Statements in C++


Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

CS112 Lecture: Loops

Loops / Repetition Statements

Information Science 1

Chapter 4 C Program Control

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Transcription:

Programming in C 1 Example 1 What if we want to process three different pairs of integers? 2 Example 2 One solution is to copy and paste the necessary lines of code. Consider the following modification: What if you wanted to process four sets? Five? Six?. 3 111 Ch 05 A 1

Processing an arbitrary number of pairs We might be willing to copy and paste to process a small number of pairs of integers but How about 1,000,000 pairs of integers? The solution lies in mechanisms used to control the flow of execution In particular, the solution lies in the constructs that allow us to instruct the computer to perform a task repetitively 4 Repetition (Looping) Use looping when you want to execute a block of code several times Block of code = Body of loop C provides three types of loops while statement 1 Most flexible No restrictions for statement 2 Natural counting loop do-while statement 3 Always executes body at least once 5 The while Repetition Structure Repetition structure Programmer specifies Condition under which actions will be executed Actions to be repeated Psuedocode While there are more items on my shopping list Purchase next item and cross it off my list while loop repeated As long as condition is true Until condition becomes false 6 111 Ch 05 A 2

The while Repetition Structure The condition is tested If the condition is true, the loop body is executed and the condition is retested. When the condition is false, the loop is exited. true condition loop body next stmt false 7 The while Repetition Structure Syntax: 1 while (expression) basic block Expression = Condition to be tested Resolves to true or false Basic Block = Loop Body Reminder Basic Block: Single statement or Multiple statements enclosed in braces 8 Loop Control Variable (LCV) The loop control variable is the variable whose value controls loop repetition. For a while loop to execute properly, the loop control variable must be declared initialized tested updated in the body of the loop in such a way that the expression/condition will become false If not we will have an endless or infinite loop 9 111 Ch 05 A 3

Counter-Controlled Repetition Requires: 1. Counter variable, LCV, initialized to beginning value 2. Condition that tests for the final value of the counter (i.e., whether looping should continue) 3. Constant increment (or decrement) by which the control variable is modified each time through the loop Definite repetition Loop executes a specified number of times Number of repetitions is known 10 Example 3 11 EXECUTION CHART count count<5 repetition 0 true 1 1 true 2 2 true 3 3 true 4 4 true 5 5 false Loop Pitfalls Enter value or zero to end: 2 What is wrong with my program? It just sits there! 12 111 Ch 05 A 4

Loop Pitfalls: Misplaced semicolon Notice the ; after the while condition! Body of loop is between ) and ; Result here: INFINITE LOOP! Ctrl-c = Kill foreground process 13 The for Repetition Structure A natural 'counting' loop Steps are built into for structure! 1. Initialization 2. Loop condition test 3. Increment or decrement initialization condition false true loop body increment 14 next stmt Review: Assignment Operators Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Examples of assignment operators: a += 5 (a = a + 5) a -= 4 (a = a - 4) b *= 5 (b = b * 5) c /= 3 (c = c / 3) d %= 9 (d = d % 9) 15 111 Ch 05 A 5

Review: Pre-increment operator Pre-increment operator: ++n i) Stand alone: add 1 to n If n equals 1, then after execution of the statement the value of n will be 2. ii) In an expression: Add 1 to n and then use the new value of n in the expression. If n is initially 1, the above statement will print the value 2. After execution of printf, n will have the value 2. 16 Review: Post-increment operator Pre-increment operator: n++ i) Stand alone: add 1 to n If n equals 1, then after execution of the statement the value of n will be 2. ii) In an expression: Use the value of n in the expression and then add 1 to n. If n is initially 1, the above statement will print the value 1 and then add 1 to n. After execution, n will have the value 2. 17 Pre- and Post-decrement operator Pre- and post-decrement operators, --n, n--, behave in a similar manner Use caution when using in an expression Do not use unless you know what you are doing! 18 111 Ch 05 A 6

The for Repetition Structure Syntax: for (initialization; test; increment) basic block 2 19 for loop example Prints the integers from one to ten 20 for Loop Example How many times does loop body execute? 21 111 Ch 05 A 7

for Loop Example How many times does loop body execute? Bite 1 -- Bite 2 -- Bite 3 -- Yum! Yum! Yum! 22 The do-while Repetition Structure The do-while repetition structure is similar to the while structure Condition for repetition tested after the body of the loop is executed loop body condition true false 23 The do-while Repetition Structure Syntax: do { statements } while ( condition ); 3 24 111 Ch 05 A 8

The do-while Repetition Structure Example Prints the integers from 1 to 10 25 The do-while Repetition Structure Example Makes sure that the user enters a valid weight 26 The break Statement break Causes immediate exit from a while, for, do/while or switch structure We will use the break statement only to exit the switch structure! 27 111 Ch 05 A 9

The continue Statement continue Control passes to the next iteration We will not use the continue statement! 28 Programming in C T H E E N D 29 111 Ch 05 A 10