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

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

Prepared by: Shraddha Modi

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

CS110D: PROGRAMMING LANGUAGE I

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

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

Repetition Structures

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

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

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

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

Repetition and Loop Statements Chapter 5

Chapter 3 Structured Program Development

Flow Control. CSC215 Lecture

Lecture 7 Tao Wang 1

Loops / Repetition Statements

Information Science 1

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

Information Science 1

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

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

Structured Programming. Dr. Mohamed Khedr Lecture 9

REPETITION CONTROL STRUCTURE LOGO

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

Structured Program Development in C

Chapter 4 C Program Control

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Why Is Repetition Needed?

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

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

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

In this chapter you will learn:

Introduction. C provides two styles of flow control:

Repetition Structures II

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

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?

Fundamentals of Programming Session 9

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

Dept. of CSE, IIT KGP

Lecture 10. Daily Puzzle

Looping. Arizona State University 1

Structured Program Development

Loops / Repetition Statements

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Chapter 5: Control Structures II (Repetition)

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

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

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

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

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Chapter 4 C Program Control

Introduction to C Programming

Unit 3 Decision making, Looping and Arrays

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

CIS 3260 Intro to Programming in C#

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Chapter 4. Flow of Control

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

Control Statements. Musa M. Ameen Computer Engineering Dept.

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

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

بسم اهلل الرمحن الرحيم

Theory of control structures

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

Chapter 5: Control Structures

Object-Oriented Programming in Java

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Review: Repetition Structure

C Programming for Engineers Structured Program

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

STUDENT LESSON A12 Iterations

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

do, while and for Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

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

Fundamentals of Programming Session 7

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

Chapter 4: Control structures. Repetition

CS1100 Introduction to Programming

Introduction to the Java Basics: Control Flow Statements

COMP 208 Computers in Engineering

3 The L oop Control Structure

Control Structures II. Repetition (Loops)

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

For Loop. Variations on Format & Specific Examples

Control structures in C. Going beyond sequential

Quiz Determine the output of the following program:

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

Chapter 4: Control structures

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Chapter 5: Loops and Files

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

Loops (while and for)

Transcription:

INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU

Outline 2 Repetition Statement for while do.. while break and continue statements

Iteration structure 3 Iteratively execute the block of statements until the condition fails Condition true Action block false Other action

Repetition Essentials 4 Loop Execute a group of statements while the condition remains true Counter-controlled repetition Definite repetition: know how many times loop will execute Control variable used to count repetitions Sentinel-controlled repetition Indefinite repetition: the number of repetition is unknown Sentinel value indicates end of loop

Repetition Essentials 5 Initialize the control variable Condition true Action block Update control variable false Other action

Counter-controlled repetition 6 Requirements Control variable of the counter Initial value of the control variable Updating the control variable each time through the loop A condition that tests for the terminating condition

while statement 7 while (repetition condition) { statement 1; statement 2; statement n; update the control variable; No semicolon; Repeat the statements while the condition remains true

Example 8 counter = 1 Counter <= 10 true printf( %d\n, counter); counter++; false Other action

Examples 9 Example 1: print 1 10 int counter; // control variable counter = 1; // initialization While (counter <= 10) { // repetition condition printf( counter is %d\n, counter); counter++; // control variable updating

Examples 10 Example 2: print 2, 4, 6, int counter; // control variable counter = 2; // initialization While (counter <= 10) { // repetition condition printf( counter is %d\n, counter); counter += 2; // control variable updating

Infinite loop 11 Forget to update the control variable int cnt = 0; while (cnt < 10) { printf( cnt = %d\n, cnt); Empty loop int cnt = 0; while (cnt < 10) { Cnt is always equal to 0

Infinite loop 12 A sequence of instructions in a computer program which loops endlessly Example int cnt = 0; while (cnt < 10) { printf( cnt = %d\n, cnt); cnt--; Semantic error: cnt never becomes a value larger than 10

Skipping the loop 13 The initial expression is false such that the action block is never been executed Example int cnt = 10; while (cnt < 0) { printf( cnt = %d\n, cnt); cnt--; Initial value of cnt is larger than 0, so the program never enters the iterative block

Sentinel-controlled repetition 14 Do not know the exact number of iterations Usually need the user or input data to terminate the loop Example: let the user input multiple data iteratively, and input q to terminate the loop

Quiz: average 15 Let the user enter the grades of students Enter -1 to finish inputting Compute the average of grades and count the number of students who pass the exam

Quiz: Flow Chart 16 counter = 0 total = 0 Pass = 0 grade!= -1 false true scanf( d\n, &grade); pass++ if (grade >= 60) counter++; average = total / counter;

for statement 17 Format for ( initialization; repetition condition; update) { statements; Execute the for-loop statements, and then increase cnt by 1 int cnt; for (cnt = 1; cnt <= 10; cnt++) { printf( %d\n, cnt); // print 1 10 Example for (int cnt = 1; cnt <= 10; cnt++) { printf( %d\n, cnt); // print 1 10

for statement 18 Can update the control variable in other ways Example for ( j = 0; j <= 100; j += 5 ) // 0, 5, 10,, 100 for ( j = 10; j > 0; j--) // 10, 9, 8, 7,, 1 for ( j = 1; j <= 100; j *= 2) // 1, 2, 4, 8, 16,, 64

for statement 19 for loops can usually be rewritten as while loops Initialization and update Can be a list separated by common (,) Example: int i, j; for (i = 0, j = 0; i + j <= 10; i++, j++) { printf( %d\n, i + j); Loop 1: i = 0, j = 0 => print 0 Loop 2: i = 1, j = 1 => print 2 Loop 3: i = 2, j = 2 => print 4 Loop 4: i = 3, j = 3 => print 6 Loop 5: i = 4, j = 4 => print 8 Loop 6: i = 5, j = 5 => print 10

for statement 20 int i, j; for (i = 0, j = 0; i + j <= 10; i++, j++) { printf( %d\n, i + j); The above loop will repeat 6 times The above code will check the repetition condition 7 times (6 true and 1 false) After the above loop, the value of variable i is 6 and the value of variable j is 6

Infinite for loop 21 Example for ( i = 1; i <= 100; i--) // i = 1, 0, -1, -2, -3, for ( i = 1; i <= 100; i *= i) // i = 1, 1, 1, 1,

Compare for and while 22 for for (initialization; repetition condition; update) { statements; while initialization while (repetition condition) { statements; update;

Quiz: for statement 23 Use for-loop to compute 1 + 2 + 3 +. + 100 Use for-loop to compute 1 + 3 + 5 + 7 + + 99 Use for-loop to compute 1 + 2 + 4 + 8 + 16 +.. + 1024

do-while statement 24 Format Initialization do { statements update while (repetition condition); Put semicolon(;) here Example int cnt = 1; do { printf( %d\n, cnt); cnt++; while (cnt <= 10);

Compare while and do-while 25 while Check the repetition before executing loop-statements Do-while Enter loop-statements at least once Need to initialize grade to a value!= -1 grade = 0; while (grade!= -1) { scanf( %d, &grade); do { scanf( %d, &grade); while (grade!= -1)

Nested loops 26 A loop within another loop Each pass of outer loop triggers the inner loop Example for(int i = 1; i <= 10; i++) { for(int j = 1; j <= 10; j++) { printf( (%2d,%2d)\n, i, j); Use indentation to indicate inner and outer loops The above code will repeat 100 times (print 100 lines) Use braces to include a loop ( 1, 1) ( 1, 2) ( 1, 3) ( 1, 4) ( 1,10) ( 2, 1) ( 2, 2) (10, 9) (10,10)

Quiz: nested loop 27 print a multiplication table 1*1= 1, 1*2= 2, 1*3= 3, 1*4= 4, 1*5= 5, 1*6= 6, 1*7= 7, 1*8= 8, 1*9= 9 2*1= 2, 2*2= 4, 2*3= 6, 2*4= 8, 2*5=10, 2*6=12, 2*7=14, 2*8=16, 2*9=18... 9*1= 9, 9*2=18, 9*3= 27, 9*4=36, 9*5=45, 9*6=54, 9*7=63, 9*8=71, 9*9=81

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

break statement 29 Example for(initialization; repetition condition; update) { statement 1; break; statement n;

break statement 30 Example of nested loops for(initialization; repetition condition; update) { for(initialization; repetition condition; update) { statement 1; break; statement n; Skip from the nearest loop

continue statement 31 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 for Increment expression is executed, then the loopcontinuation test is evaluated

continue statement 32 Example for(initialization; repetition condition; update) { statement 1; continue; statement n;

continue statement 33 Example for (cnt = 1; cnt <= 10; cnt++) { if (cnt == 5) continue; printf( %d\n, cnt); 1 2 3 4 6 7 8 9 10 output

continue statement 34 Example of nested loops for(initialization; repetition condition; update) { for(initialization; repetition condition; update) { statement 1; continue; statement n; Go back to the beginning of the nearest loop