4.8 Thedo while Repetition Statement Thedo while repetition statement



Similar documents
Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Informatica e Sistemi in Tempo Reale

Lecture 2 Notes: Flow of Control

6. Control Structures

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Selection Statements

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

JavaScript: Control Statements I

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Two-way selection. Branching and Looping

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Sources: On the Web: Slides will be available on:

Fundamentals of Programming

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter One Introduction to Programming

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

ALGORITHMS AND FLOWCHARTS

Object Oriented Software Design

Lecture 1 Introduction to Java

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Market Access To Taiwan. By Jane Peng TÜV Rheinland Taiwan Ltd.

CS 241 Data Organization Coding Standards

In this Chapter you ll learn:

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 8 Selection 8-1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

LC Paper No. PWSC269/15-16(01)

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Flowchart Techniques

EFL Business Writing with Task-based Learning Approach: A Case Study of Student Strategies to Overcome Difficulties

Object Oriented Software Design

MATLAB Programming. Problem 1: Sequential

Boolean Expressions & the if Statement

While and Do-While Loops Summer 2010 Margaret Reid-Miller

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Introduction to Java

Installation Guide 2-Port HD Cable KVM Switch with Audio GCS62HU PART NO. M1130

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

Statements and Control Flow

Pseudo code Tutorial and Exercises Teacher s Version

J a v a Quiz (Unit 3, Test 0 Practice)

C++FA 5.1 PRACTICE MID-TERM EXAM

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Visual Logic Instructions and Assignments

Moving from CS 61A Scheme to CS 61B Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

TIP: To access the WinRunner help system at any time, press the F1 key.

Writing Control Structures

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Chemistry I -- Final Exam

So far we have considered only numeric processing, i.e. processing of numeric data represented

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis

REGULATIONS FOR THE DEGREE OF BACHELOR OF ARTS IN ARCHITECTURAL STUDIES (BA[ArchStud])

VB.NET Programming Fundamentals

PL / SQL Basics. Chapter 3

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

1.3 Conditionals and Loops

Programming MS Excel in Visual Basic (VBA)

Total Quality Management (TQM) Quality, Success and Failure. Total Quality Management (TQM) vs. Process Reengineering (BPR)

促 進 市 場 競 爭 加 強 保 障 消 費 者

Case Study of a New Generation Call Center

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

UEE1302 (1102) F10 Introduction to Computers and Programming

Programming Logic and Design Eighth Edi(on

Conditionals (with solutions)

The HKICPA Accounting and Business Management Case Competition Secondary School Group (Level 1)

arrays C Programming Language - Arrays

Stacks. Linear data structures

While Loop. 6. Iteration

SHAU KEI WAN GOVERNMENT SECONDARY SCHOOL

The While Loop. Objectives. Textbook. WHILE Loops

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Chapter 3 Operators and Control Flow

Keil C51 Cross Compiler

How to Write a Simple Makefile

C Programming Tutorial

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

The programming language C. sws1 1

LOOPS CHAPTER CHAPTER GOALS

Command Scripts Running scripts: include and commands

CS106A, Stanford Handout #38. Strings and Chars

Machine Translation for Academic Purposes

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Java Basics: Data Types, Variables, and Loops

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Tutorial on C Language Programming

Transcription:

1 4.8 hedo while Repetition Statement hedo while repetition statement Similar to thewhile structure Condition for repetition tested after the body of the loop is performed All actions are performed at least once ormat: do { statement statement; } while ( condition ); condition false true 2 hedo while Repetition Example Example (lettingcounter = 1): do { printf( "%d ", counter ); } while (++counter <= 10); Prints the integers from1to10 1

fig04_09.c 3 1 /* ig. 4.9: fig04_09.c 2 Using the do/while repetition statement */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main() 7 { 8 int counter = 1; /* initialize counter */ 9 10 do { 11 printf( "%d ", counter ); /* display counter */ 12 } while ( ++counter <= 10 ); /* end do...while */ 13 14 return 0; /* indicate program ended successfully */ 15 16 } /* end function main */ Program Output 1 2 3 4 5 6 7 8 9 10 4 4.9 hebreak andcontinue Statements break Causes immediate exit from awhile,for, do while or switch statement Program execution continues with the first statement after the structure Common uses of thebreak statement Escape early from a loop Skip the remainder of aswitch statement 2

1 /* ig. 4.11: fig04_11.c 2 Using the break statement in a for statement */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main() 7 { 8 int x; /* counter */ 9 10 /* loop 10 times */ 11 for ( x = 1; x <= 10; x++ ) { 12 13 /* if x is 5, terminate loop */ 14 if ( x == 5 ) { 15 break; /* break loop only if x is 5 */ 16 } /* end if */ 17 18 printf( "%d ", x ); /* display value of x */ 19 } /* end for */ 20 21 printf( "\nbroke out of loop at x == %d\n", x ); 22 23 return 0; /* indicate program ended successfully */ 24 25 } /* end function main */ 1 2 3 4 Broke out of loop at x == 5 fig04_11.c Program Output 5 6 continue hecontinue Statement Skips the remaining statements in the body of a while,for ordo while statement Proceeds with the next iteration of the loop while anddo while Loop-continuation test is evaluated immediately after thecontinue statement is executed for Increment expression is executed, then the loopcontinuation test is evaluated 3

1 /* ig. 4.12: fig04_12.c 2 Using the continue statement in a for statement */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main() 7 { 8 int x; /* counter */ 9 10 /* loop 10 times */ 11 for ( x = 1; x <= 10; x++ ) { 12 13 /* if x is 5, continue with next iteration of loop */ 14 if ( x == 5 ) { 15 continue; /* skip remaining code in loop body */ 16 } /* end if */ 17 18 printf( "%d ", x ); /* display value of x */ 19 } /* end for */ 20 21 printf( "\nused continue to skip printing the value 5\n" ); 22 23 return 0; /* indicate program ended successfully */ 24 25 } /* end function main */ 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 fig04_12.c Program Output 7 練 習 一 寫 一 個 C 語 言 程 式, 讓 使 用 者 輸 入 一 個 介 於 1 至 100 間 的 整 數 N 輸 出 1 至 N 間 ( 含 N) 所 有 不 為 3 或 5 倍 數 的 數 字 並 輸 出 這 樣 的 數 字 有 多 少 個 提 示 : 使 用 一 個 for 或 while 迴 圈 檢 查 1 至 N 間 的 所 有 整 數, 並 配 合 使 用 continue 敘 述 8 執 行 範 例 Input a number between 1 and 100: 16 1, 2, 4, 7, 8, 11, 13, 14, 16. otal 9 numbers. 4

4.10 Logical Operators 9 && ( logical AND ) Returnstrue if both conditions aretrue ( logical OR ) Returnstrue if either of its conditions aretrue! ( logical NO, logical negation ) Reverses the truth/falsity of its condition Unary operator, has one operand Useful as conditions in loops Expression true && false true false!false Result false true true he ruth able of && 10 expression1 expression2 expression1 && expression2 0 0 0 0 nonzero 0 nonzero 0 0 nonzero nonzero 1 5

he ruth able of 11 expression1 expression2 expression1 expression2 0 0 0 0 nonzero 1 nonzero 0 1 nonzero nonzero 1 he ruth able of! 12 expression! Expression 0 1 nonzero 0 6

13 4.10 Logical Operators Operators Associativity ype ++ -- + -! (type) right to left unary * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality && left to right logical AND left to right logical OR?: right to left conditional = += -= *= /= %= right to left assignment, left to right comma 4.11 Confusing Equality (==) and Assignment (=) Operators Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values aretrue, zero values are false Example using==: if ( paycode == 4 ) printf( "You get a bonus!\n" ); 14 CheckspayCode, if it is4then a bonus is awarded 7

4.11 Confusing Equality (==) and Assignment (=) Operators Example, replacing== with=: 15 if ( paycode = 4 ) printf( "You get a bonus!\n" ); his setspaycode to4 4 is nonzero, so expression istrue, and bonus awarded no matter what thepaycode was Logic error, not a syntax error 16 lvalues L-values Expressions that can appear on the left side of an equation heir values can be changed, such as variable names x = 4; 8

17 rvalues R-values Expressions that can appear on the right side of an equation Constants, such as numbers Cannot write4 = x; Must writex = 4; lvalues can be used as rvalues, but not vice versa y = x; 18 4.12 Structured-Programming Summary Se q u e n c e Sequence structure. 9

4.12 Structured-Programming Summary 19 Selection structure statement if statement (single selection) if else statement (double selection). switch (multiple selection) break break break 4.12 Structured-Programming Summary 20 Repetition structure while statement do while statement for statement 10

21 4.12 Structured Programming Structured programming Only single-entry/single-exit control structures are used Easier than unstructured programs to understand, test, debug and, modify programs 22 Rules for Structured Programming developed by programming community Rules: 1.Begin with the simplest flowchart 2.Stacking rule: Any rectangle (action) can be replaced by two rectangles (actions) in sequence 3.Nesting rule: Any rectangle (action) can be replaced by any control structure (sequence,if, if else,switch,while,do while or for) 4.Rules 2 and 3 can be applied in any order and multiple times 11

4.12 Structured-Programming Summary 23 Rule 1 - Begin with the simplest flowchart Rule 2 - Any rectangle can be replaced by two rectangles in sequence Rule 2 Rule 2 Rule 2... 4.12 Structured-Programming Summary 24 Rule 3 - Replace any rectangle with a control structure Rule 3 Rule 3 Rule 3 12

4.12 Structured-Programming Summary 25 Stacke d b uilding b lo cks Nested build ing blocks Overla pping b uilding blocks (Illega l in structured pro gra ms) 4.12 Structured-Programming Summary 26 igure 4.23 An unstructured flowchart. 13

27 4.12 Structured-Programming Summary All programs can be broken down into three controls Sequence handled automatically by compiler Selection if,if else orswitch Repetition while,do while orfor Can only be combined in two ways Nesting (rule 3) Stacking (rule 2) Any selection can be rewritten as anif statement, and any repetition can be rewritten as awhile statement 14