Repetition Structures II

Similar documents
Selection Structures II

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

Introduction. C provides two styles of flow control:

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

Arrays III and Enumerated Types

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

Repetition and Loop Statements Chapter 5

Lecture 14. Arrays II. Selection Sort & Parallel Arrays. CptS 121 Summer 2016 Armen Abnousi

Loops / Repetition Statements

(7-1) Modular Programming H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University

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

Lecture 7 Tao Wang 1

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

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Dept. of CSE, IIT KGP

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

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

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

Module 4: Decision-making and forming loops

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

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

CMPT 102 Introduction to Scientific Computer Programming

Chapter 5: Control Structures

A Look Back at Arithmetic Operators: the Increment and Decrement

Repetition Structures

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

Chapter 4: Control structures. Repetition

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 4: Control structures

LECTURE 5 Control Structures Part 2

Welcome! COMP s1. Programming Fundamentals

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Lecture 10. Daily Puzzle

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Flow Control. CSC215 Lecture

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Chapter 13 Control Structures

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

(4-3) Selection Structures II in C H&K Chapter 4. Instructor - Andrew S. O Fallon CptS 121 (February 2, 2018) Washington State University

Introduction to Programming

CS110D: PROGRAMMING LANGUAGE I

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

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

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

(7-2) Modular Programming II H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 22, 2019) Washington State University

Subject: PIC Chapter 2.

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

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

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

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

Structured Program Development

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

Chapter 3 Structured Program Development

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

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

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

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

DECISION CONTROL AND LOOPING STATEMENTS

(3-1) Functions II H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (September 5, 2018) Washington State University

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

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

MODULE 2: Branching and Looping

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Structured Programming. Dr. Mohamed Khedr Lecture 9

Relational operators (1)

CS1100 Introduction to Programming

Chapter 13. Control Structures

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

Laboratory 5: Implementing Loops and Loop Control Strategies

Decision Making and Loops

Loops / Repetition Statements

Loops. CSCI 112: Programming in C

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

Chapter 13 Control Structures

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

CMPT 102 Introduction to Scientific Computer Programming

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

Repetition CSC 121 Fall 2014 Howard Rosenthal

Dynamic Data Structures (II)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

Theory of control structures

Fundamentals of Programming Session 13

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

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

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

204111: Computer and Programming

Quiz Determine the output of the following program:

Repetition, Looping CS101

Iterative Languages. Scoping

REPETITION CONTROL STRUCTURE LOGO

Chapter 4. Flow of Control

CS 106 Introduction to Computer Science I

Transcription:

Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi

Types of Control Structures Sequential All programs that we have written so far The statements inside a pair of Statements runs one by one, in the order they are written Selection Allows for the execution of alternatives Runs a set of statements if a certain condition (set by the programmer) is met, otherwise skips those operations Repetition Allows for repeating the execution of some statements until a condition is met (or for a certain time). Repetition of steps of a program is called a loop.

While loops We saw before that while loop is an example of repetition structures. while ( conditional expression ) /*make an effort to change the condition value*/

For loops for is another tool providing repetition control structure. for (initialization; condition; update) /*statements you want to repeat*/

For vs while - example Print the integers in range of [2, 5]: For loop: int counter = 0; for ( counter = 2; counter < 6; counter++) printf( %d, counter); While loop: int counter = 2; while ( counter < 6 ) printf( %d, counter); counter++;

For loop Factorial problem Write a program that given the integer n, computes n! int product = 1; int product = 1; for ( i = n; i > 1; i-- ) for ( i = 2; i <= n; i++) product *= i; product *= i;

For loops conditions and update rules The condition for the loop can be any conditional expression that we have seen before (anything that can evaluate to true or false) Remember any non-zero number means true and zero means false The update rule can be any computation that alters the loop control variable. Be careful about the infinite loops!

For loops using sentinel values The user wants to enter some numbers and wants us to compute the sum, until she enters -2 (sentinel value): sum = 0; printf( please enter a number: \n) for ( scanf( %c, &response); response!= -2; scanf( %d, &response) ) sum += response; printf( please enter another number:\n );

A note about the scanf() scanf is a function, and like any other function has a return value. The return value for scanf is int and represents the number of arguments it has successfully read scanf( %c, &mychar); => when user inputs a will return 1 scanf( %d%d, &int1, &int2); => when user inputs 23 51 will return 2 This can be used for finding errors: scanf( %d%d, &int1, &int2); => when user inputs 23 a will return 1

Do-while loops For and while loops both perform a condition evaluation before the first execution of loop body Sometimes a loop must execute at least once Do-while loops first execute the body of the loop and then check if the condition is satisfied (if it is will repeat the loop body, otherwise will exit the loop) Useful for input validation (if the input value is wrong, continue asking until the user gives a correct input)

Do-while loops do /* loop body */ while (expression);

Do-while - example Ask the user to enter a choice between a, b, c, or d: do printf( please enter a choice a.. d : ); scanf( %c, &mychar); while ( mychar < a d < mychar );

Do-while - example 2 Ask the user to enter a choice between 1, 2, 3, or 4: do printf( please enter a choice 1.. 4 : ); scanf( %d, &myint); while ( myint < 1 4 < myint ); Will this work? What can go wrong?

Do-while - example 2 Ask the user to enter a choice between 1, 2, 3, or 4: do printf( please enter a choice 1.. 4 : ); scanf( %d, &myint); while ( myint < 1 4 < myint ); Will this work? What can go wrong? What if you enter a character (letter)? That will stay in the buffer and scanf will continue trying to read that with %d placeholder! Infinite loop!

Do-while - example 2 Instead this will correct the code: char mychar = 'a'; int myint = 0, status = 0; do printf("please enter a choice 1.. 4 :"); status = scanf("%d", &myint); if (status!= 1) scanf("%c", &mychar); printf("invalid character %c", mychar); do scanf("%c", &mychar); while (mychar!= '\n'); while (myint < 1 4 < myint);

Do-while - example 2 Instead this will correct the code: char mychar = 'a'; int myint = 0, status = 0; do printf("please enter a choice 1.. 4 :"); status = scanf("%d", &myint); if (status!= 1) scanf("%c", &mychar); printf("invalid character %c", mychar); do scanf("%c", &mychar); while (mychar!= '\n'); while (myint < 1 4 < myint); If status is not 1 means user didn t input an int. Then read the first character and say it is invalid Read the whole line character by character until it reaches a new line

Nested loops Loops can be nested into each other (like if statements) Every time the outer loop executes it enters the inner loop and performs all the iterations for ( j = 0; j < 3; j++ ) for ( m = (int) A ; m < (int) E ; m++) Will print: printf( \n ); printf( %c, (char) m); ABCD ABCD ABCD

Nested loops for ( i = 5; i > 0; i-- ) for ( j = 0; j < i; j++ ) printf( %-3f, j); printf( \n ); This will print

Nested loops for ( i = 4; i > 0; i-- ) for ( j = 0; j < i; j++ ) printf( %-3f, j); printf( \n ); This will print 0 1 2 3 0 1 2 0 1 0

References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8thed.), Pearson, 2016 Andy O Fallon s lecture notes for CptS121 (http://eecs.wsu.edu/~aofallon/cpts121)