CMPT 102 Introduction to Scientific Computer Programming

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 5: Control Structures II (Repetition)
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The Repetition control structure using while 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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
EKT120 COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Quick Test What do you mean by pre-test and post-test loops in C?
CS1010 Programming Methodology
Control Structures II (Repetition)
JavaScript: Control Statements.
Control Structures Lecture 7.
Looping.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Looping and Repetition
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Chapter 4 - Program Control
Loops in C.
Repetition Control Structure
Chapter 6: Repetition Statements
Chapter 5: Control Structures II (Repetition)
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Conditional Loops Counted Loops
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 8 JavaScript: Control Statements, Part 2
Looping and Repetition
Presentation transcript:

CMPT 102 Introduction to Scientific Computer Programming Control Structures while Loops continue; and break; statements

Control Structures Three methods of processing a program In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repetition of a particular block of statement(s) © Janice Regan, CMPT 102, Sept. 2006

Basic Loops When one action is to be repeated a number of times a loop is used. Loops are repetition structures There are two common types of loops while loop or do...while loop Used to continue repetition while a condition holds Can also be used (along with a counter variable) to repeat a particular number of times for loop Specifically designed to repeat a particular number of times © Janice Regan, CMPT 102, Sept. 2006

A while Loop in C while ( condition ) { /* Series of actions to be taken */ /* each time the loop is executed */ /* loop is executed when condition is True */ action 1; action 2; } /* When condition is false execute following actions */ actions; © Janice Regan, CMPT 102, Sept. 2006

Structure of while loop condition Statement 1; Statement n; © Janice Regan, CMPT 102, Sept. 2006

Counting while statement Initial statements start = n end = m loopIndex = start; Repeats Statements m-n times Loop condition loopIndex < end F loopIndex += step; T Update statement First Statement: ….. Last Statement; © Janice Regan, CMPT 102, Sept. 2006

Example while Loop in C /* Sum the Integers from 1 to 25 inclusive */ while ( X <= 25 ) { Sum += X; X++; } printf(“%d\n”, Sum); © Janice Regan, CMPT 102, Sept. 2006

do…while Loop in C do { /*Series of actions to be taken */ /*each time the loop is executed */ /*Always executed on first pass */ /* Subsequently executed if condition is True */ action 1; action 2; } while ( condition ); /* When condition is false execute following actions */ actions; © Janice Regan, CMPT 102, Sept. 2006

Structure of do…while Loop condition Statement 1; Statement n; Loop condition © Janice Regan, CMPT 102, Sept. 2006

Counting do…while statement Initial statement start = n end = m loopIndex = start; Repeats Statements m-n times First Statement: ….. Last Statement; Update statement loopIndex += step; loopIndex < end T F Loop condition © Janice Regan, CMPT 102, Sept. 2006

Example do…while Loop X = 1; sum = 0; do { sum += X; X++; } /* Sum the Integers from 1 to 25 inclusive */ X = 1; sum = 0; do { sum += X; X++; } while ( X <= 25 ); printf(“%d”, sum); © Janice Regan, CMPT 102, Sept. 2006

Differences do vs do…while /* Sum Integers from n to m */ n = 4: m = 3: sum = 0; do { sum += n; n++; } while ( n <= m ); printf(“%d”, sum); /* Sum Integers from n to m */ n = 4: m = 3: sum = 0; while ( n <= m ) { sum += n; n++; } printf(“%d”, sum); After code sum = 4 Body of loop executes once After code sum = 0 Body of loop does not execute © Janice Regan, CMPT 102, Sept. 2006

Infinite Loops When using while or do..while loops it is possible that the loop condition is always true. If the variable or expression tested in the loop condition is not modified in the action statements within the loop, then if the condition is true for the first loop test it remains true for ever Even if the variable or expression changes it may satisfy the test condition for ever An infinite loop will cause your program to execute forever If your program is caught in an infinite loop you can terminate it by typing <CNTRL>C into your linux or cygwin command window © Janice Regan, CMPT 102, Sept. 2006

Uses of an infinite loop To make an infinite loop useful, you must have a way to exit from it It is possible to exit from a loop by using a break; statement (discussed below) If you use an infinite loop intentionally it is usually a loop you intend to execute many times until Some condition is met OR The loop has executed the maximum number of times If you use an infinite loop intentionally you should always exit after a maximum number of times through the loop, just in case the condition you expect to take you out of the loop never occurs. © Janice Regan, CMPT 102, Sept. 2006

The Break Statement: Loops Sometimes you wish to exit from a loop even if the loop condition is still true For example if your loop is reading and processing data and an error occurs that can be recognized but cannot be corrected, you may wish to print an error message and exit the loop immediately break; A C statement that causes you to exit the loop you are in Program execution will continue at the statement following the loop © Janice Regan, CMPT 102, Sept. 2006

break statement Loop condition Condition F T Statement 1; ….. T Statement n; © Janice Regan, CMPT 102, Sept. 2006

The Continue Statement: Sometimes you wish to execute only some of the statements in the body of the loop for a particular value of the loop index For example you may wish to skip the last half of the body of the loop if (loopindex/14)= = 1 continue; A C statement that causes you to go from anywhere in the loop to the update statement and loop condition Program execution will continue at the update statement of the loop © Janice Regan, CMPT 102, Sept. 2006

continue statement Condition F T Statement 1; ….. T condition2 Statement n; © Janice Regan, CMPT 102, Sept. 2006

continue in Counting while Start = n End = m loopIndex = start; Repeats Statements m-n times Initial statement Loop condition loopIndex < end F loopIndex += step; T First Statement: ….. Update statement T condition2 continue; F ….. Last Statement; © Janice Regan, CMPT 102, Sept. 2006

Example infinite Loop in C /* The user inputs a positive integer */ /* the routine prints the square of the number, */ /* to terminate the user enters -999 */ while ( 1 ) { printf("enter a positive integer " ); scanf("%d", &positiveIntNum); if(positiveIntNum < 0) if( positiveIntNum == -999) {break}; printf("error*** negative input ***\n"); continue; } printf("%d\n", positiveIntNum*positiveIntNum); © Janice Regan, CMPT 102, Sept. 2006