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

Similar documents
Loops / Repetition Statements

Loops / Repetition Statements

Structured Programming. Dr. Mohamed Khedr Lecture 9

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Repetition and Loop Statements Chapter 5

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

Repetition Structures

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

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

Conditionals and Loops

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

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

C 101. Davide Vernizzi. April 29, 2011

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

CS110D: PROGRAMMING LANGUAGE I

Looping. Arizona State University 1

REPETITION CONTROL STRUCTURE LOGO

Dept. of CSE, IIT KGP

Introduction. C provides two styles of flow control:

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

Information Science 1

CS1004: Intro to CS in Java, Spring 2005

Information Science 1

Loops (while and for)

Lecture 10. Daily Puzzle

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

STUDENT LESSON A12 Iterations

Flow of Control: Loops. Chapter 4

Module 4: Decision-making and forming loops

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Lecture 7 Tao Wang 1

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Programming for Engineers Iteration

Chapter 4 C Program Control

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

Chapter 4 C Program Control

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Flow of Control: Loops

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

CS1100 Introduction to Programming

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

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

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

Repetition Structures II

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Subject: PIC Chapter 2.

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

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

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

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

Welcome! COMP s1. Programming Fundamentals

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

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

Chapter 5: Loops and Files

204111: Computer and Programming

3 The L oop Control Structure

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

DECISION CONTROL AND LOOPING STATEMENTS

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

Building Java Programs

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

fifth Solutions to Exercises 85

Control Structure: Loop

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Lab 2: Structured Program Development in C

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

Repetition CSC 121 Fall 2014 Howard Rosenthal

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

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

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

A Look Back at Arithmetic Operators: the Increment and Decrement

Fundamentals of Programming Session 13

(Refer Slide Time: 00:26)

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

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

Lecture 5 Tao Wang 1

Chapter 4: Basic C Operators

Prepared by: Shraddha Modi

Chapter 10 C Structures and Unions

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

Chapter 5: Control Structures

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

CSCE 206: Structured Programming in C++

Principles of Computer Science I

LOOPS. Repetition using the while statement

Chapter 3 Structured Program Development

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

Structured Program Development in C

Multiple-Subscripted Arrays

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Transcription:

Loops / Repetition Statements Repetition s allow us to execute a multiple times Often they are referred to as loops C has three kinds of repetition s: the while loop the for loop the do loop The programmer should choose the right kind of loop for the situation There are three loop constructs in C while loop for loop do-while loop (or do loop for short) Loops = repetition s Example 1: Fixing Bad Keyboard Input Write a program that refuses to accept a negative number as an input. The program must keep asking the user to enter a value until he/she enters a positive number. How can we do this? Example 2: Grade of several students Write a program that continuously calculates the grade of all students marks and stop when the user wants. After calculating one student s grade (from his marks the program must keep asking the user whether he likes to continue or not. How can we do this? Logic of an if if logic Logic of a while Loop The while Loop 1

The while Statement A while has the following syntax: while ( ) ; If the is, the is executed Then the is again, and if it is still, the is executed again The is executed repeatedly until the becomes The while Statement An example of a while : int count = 1; while (count <= 5) printf ( %d\n, count); count++; If the of a while loop is initially, the is never executed Therefore, the body of a while loop will execute zero or more times The while Statement Let's look at some examples of loop processing A loop can be used to maintain a running sum A sentinel value is a special input value that represents the end of input A loop can also be used for input validation, making a program more robust Some examples Print Sky is the limit! 10 times. Print Sky is the limit! 100 times. Print Sky is the limit! n times. Print first n numbers. Print odd numbers up to n. Print even numbers up to n. Print summation of first n numbers. Print summation of all odd numbers up to n. Print summation of all even numbers up to n. Logic of a for loop A for has the following syntax: initialization The initialization is executed once before the loop begins The is executed until the becomes increment for ( initialization ; ; increment ) ; The increment portion is executed at the end of each iteration 2

A for loop is functionally equivalent to the following while loop structure: initialization; while ( ) ; increment; An example of a for loop: for (int count=1; count <= 5; count++) printf ( %d\n, count); The initialization section can be used to declare a variable Like a while loop, the of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times The increment section can perform any calculation int num; for (num=100; num > 0; num -= 5) printf ( %d\n, num); A for loop is well suited for executing s a specific number of times that can be calculated or determined in advance Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the is left out, it is always considered to be, and therefore creates an infinite loop If the increment is left out, no increment operation is performed Infinite Loops The body of a while loop eventually must make the If not, it is called an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check the logic of a program to ensure that your loops will terminate normally Infinite Loops An example of an infinite loop: int count = 1; while (count <= 25) printf ( %d\n, count); count = count - 1; This loop will continue executing until interrupted (Control-C) or until an underflow error occurs 3

Logic of a do-while Loop The do Statement A do has the following syntax: do ; while ( ); The is executed once initially, and then the is The is executed repeatedly until the becomes The do Statement Comparing while and do An example of a do loop: The while Loop The do Loop int count = 1; do printf( %d\n, count); count++; while (count <= 5); The body of a do loop is executed at least once Example: Reversing Digits of a number Nested Loops Write down a program that reverses the digits of a number. For example: input: 6457 output: 7546 4

Nested Loops Similar to nested if s, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) count2 = 1; while (count2 <= 20) printf ("Here \n"); count2++; count1++; 10 * 20 = 200 Analogy for Nested Loops Inner Loop Outer Loop Example: Stars Write a program that prints the following. The total number of lines will be input to your program. * ** *** **** ***** ****** ******* ******** ********* 2004 Pearson Addison-Wesley. All rights reserved Example: Stars Write a program that prints the following. The total number of lines will be input to your program. * ** *** **** ***** ****** ******* ******** ********* 2004 Pearson Addison-Wesley. All rights reserved 5

6 * ** *** **** ***** ****** ******* ******** *********