Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 5: Control Structures II (Repetition)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
1. Agenda for loop Short-handed notation How to use break and continue 2.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Introduction to Computer Programming
Chapter 4 – C Program Control
Chapter 4 C Program Control Part II
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements I
Loop Control Structure.
JavaScript: Control Statements (II)
MSIS 655 Advanced Business Applications Programming
- Additional C Statements
Chapter 4 - Program Control
for, do-while and switch statments
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
REPETITION STATEMENTS
A LESSON IN LOOPING What is a loop?
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Presentation transcript:

Lecture 8: Choosing the Correct Loop

do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body of the loop is performed. All actions are performed at least once. Format do { statement(s); } while ( condition ); Flowcharting the do…while repetition statement Suggestion: always include braces in a do…while statement even if the braces are not nessary.

do … while Repetition Statement Example 1 (letting counter = 1): do { printf( "%d ", counter ); } while (++counter <= 10); Prints the integers from 1 to 10 Example 2: Simple calculator using do…while statement Can now do something before testing a condition to see if we should repeat it. "code, while, code" can now do "do code, while"

Flowcharting for the simple calculator Using the while statement begin Print the calculator menu; Input the operation; false end true case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit Print the calculator menu; Input the operation; case Exit true Print messagebreak false

Flowcharting for the simple calculator Using the do…while statement begin Print the calculator menu; Input the operation; false end case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit case Exit true Print “ Thanks ” break false true

break Statement Cause immediate exit from a while, for, do…while or switch statement. Program execution continues with the first statement after the structure Common uses of the break statement Escape early from a loop Skip the remainder of a switch statement

break Statement Example break immediately ends for loop

continue Statement Skips the remaining statements in the body of a while, for or do…while statement. Proceeds with the next iteration of the loop while and do…while Loop-continuation test is evaluated immediately after the continue statement is executed The for statement Increment expression is executed, then the loop- continuation test is evaluated.

continue Statement Example continue skips to end of for loop and performs next iteration

Write a program that checks to see if a number the user inputs is a prime number or not. If the number is prime, the program should exit. Otherwise, the program should keep asking the user for a new number.  Use a do/while loop to control the looping.  Provide appropriate feedback to the user (prime or not).  Use the % operator to conduct your checks. (Hint: The % operator returns the remainder.)  Try to make the program as short as possible. Submit your completed program in the Prime Numbers dropbox. In-Class Programming Exercise Challenge: 4.17

In mathematics, a prime number is a positive integer which has exactly two distinct divisors: 1 and itself. If none of 2, 3, …,  n/2  is a divisor of n, than n is prime. If one of 2, 3, …,  n/2  is a divisor of n, than n is NOT prime. Prime Number

begin Input the number (> 2) tag==TRUE false true Determine if the number is prime and set the tag end Print the number is prime.

begin Input the number (> 2) end tag==TRUE true false divisor = 2 tag = TRUE remainder==0 divisor ++; divisor<=number/2 remainder = number % divisor tag = FALSE; break; true false Print the number is prime. true Determine if the number is prime and set the tag