Control Structure: Loop

Similar documents
Fundamental of Programming (C)

Fundamentals of Programming

A Look Back at Arithmetic Operators: the Increment and Decrement

Dept. of CSE, IIT KGP

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

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

Control Structure: Selection

Decision Making and Loops

Decision Making -Branching. Class Incharge: S. Sasirekha

CS1100 Introduction to Programming

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Loops / Repetition Statements

Chapter 5: Control Structures

Structured Programming. Dr. Mohamed Khedr Lecture 9

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

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

Computer Programming. Decision Making (2) Loops

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

Functions. Arash Rafiey. September 26, 2017

DECISION CONTROL AND LOOPING STATEMENTS

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

Final Examination Semester 3 / Year 2010

C 101. Davide Vernizzi. April 29, 2011

Introduction. C provides two styles of flow control:

CS16 Exam #1 7/17/ Minutes 100 Points total

AMCAT Automata Coding Sample Questions And Answers

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

LAB 5: REPETITION STRUCTURE(LOOP)

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Chapter 3 Structured Program Development

Structured Program Development in C

Flow Chart. The diagrammatic representation shows a solution to a given problem.

C: How to Program. Week /Mar/05

LAB 5: REPETITION STRUCTURE(LOOP)

REPETITION CONTROL STRUCTURE LOGO

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

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Repetition and Loop Statements Chapter 5

Lab 2: Structured Program Development in C

Structured Program Development

Chapter 2 - Introduction to C Programming

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

Final Examination Semester 2 / Year 2010

Control Structures II. Repetition (Loops)

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

SAMPLE MIDTERM SOLUTION

for Loop Lesson 3 Outline

IS12 - Introduction to Programming Lecture 12: Loops and Tables. Relational Operators > < >= <=

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

Computer Science & Engineering 150A Problem Solving Using Computers

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Chapter 4 C Program Control

Loops / Repetition Statements

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

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

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

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Programming for Engineers Iteration

Subject: Fundamental of Computer Programming 2068

Welcome! COMP s1. Programming Fundamentals

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

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

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Programming Lecture 4

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Assoc. Prof. Dr. Tansu FİLİK

Lecture 6. Statements

Assignment: 1. (Unit-1 Flowchart and Algorithm)

EXAMINATION FOR THE BSC (HONS) INFORMATION SYSTEMS; BSC (HONS) INFORMATION TECHNOLOGY & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Repetition Structures

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

Module 4: Decision-making and forming loops

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointers and scanf() Steven R. Bagley

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

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

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

More examples for Control statements

ESC101N: Fundamentals of Computing End-sem st semester

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Building Java Programs

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Programming Lecture 4

Problem 7.4: Racetrack

Repetition Structures II

ECET 264 C Programming Language with Applications. C Program Control

ALGORITHM 2-1 Solution for Exercise 4

EECE.2160: ECE Application Programming Spring 2018

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Chapter 4: Control structures. Repetition

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CMPT 102 Introduction to Scientific Computer Programming

Transcription:

Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1

Loop Structure Condition is tested first Condition is tested later TK1913-C Programming 2

Testing Condition First Step a condition false true Step x Step y Step n TK1913-C Programming 3

Testing Condition First Step a condition Step x Step y Step n TK1913-C Programming 4

Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming 5

Testing condition later Step a Step x Step y true condition false Step n TK1913-C Programming 6

How Loops are Controlled Condition Controlled Counter Controlled 1, 2, 3, 4,, 4, 3, 2, 1 Sentinel Controlled TK1913-C Programming 7

Counter Controlled Loop counter initialvalue true test counter value Step x false Update counter Step n TK1913-C Programming 8

Counter Controlled Loop counter = initialvalue true test counter value Step x false Update counter Step n TK1913-C Programming 9

Example: Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Can you identify the input and output??? Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2,.., n5 Input example: 2 3 4 5 6 Output example: 20 TK1913-C Programming 10

Assume input example: 2 3 4 5 6 start Input n1 n1 2 This flowchart does not use loop, hence we need to use 6 different variables Input n2 Input n3 input n4 n2 n3 n4 3 4 5 input n5 n5 6 sum n1+n2+n3+n4+n5 sum 20 output sum end TK1913-C Programming 11

Counter- Assume input Controlled example: 2 3 4 5 6 Loop counter 1, sum 0 false counter < 6 true counter 12 34 56 sum 14 20 02 59 14 02 59 + 23 45 6 61 23 54 < < 6 6 false true input n n 23 45 6 sum sum + n counter++ output sum The Uses This counter loop only is counter-controlled Increases 3 variables by 1 TK1913-C Programming 12

Decreasing Counter-Controlled Loop counter 5, sum 0 true counter > 0 input x sum sum+ x false counter-- output sum TK1913-C Programming 13

Example: Draw a flowchart for this problem; Given an exam marks as input, display the appropriate message based on the rules below: If marks is greater than 49, display PASS, otherwise display FAIL However, for input outside the 0-100 range, display WRONG INPUT and prompt the user to input again until a valid input is entered TK1913-C Programming 14

Condition-Controlled Loop Assume m=110 m=57 m 110 57 5 input m WRONG INPUT 110 57 < < 0 < 0 0 5 57 >100 110 >100 >100 true m<0 m>100 false 5 > Condition-controlled 57 49 > 49 loop with its condition being tested at the end false m>49 PASS FAIL true WRONG FAIL PASS INPUT TK1913-C Programming 15

input m m<0 m>100 true WRONG INPUT false input m Condition-controlled loop with its condition being tested first false m>49 PASS FAIL true TK1913-C Programming 16

Sentinel-Controlled Loop Draw a flowchart for a problem which: Receive a number of positive integers and display the summation and average of these integers. A negative or zero input indicate the end of input process Can you identify the input and output??? Input: A set of integers ending with a negative integer or a zero Output: Summation and Average of these integers TK1913-C Programming 17

Input Example: 30 16 42-9 Sentinel Value Output Example: Sum = 88 Average = 29.33 TK1913-C Programming 18

Now What have you understand? TK1913-C Programming 19

Try to understand sum 0 input x x>0 true sum sum+x input x false What will happen? if this statement is deleted??? What happened if these 2 statements exchange places sum sum+x input x display sum? TK1913-C Programming 20

for while do-while TK1913-C Programming 21

Loop : for Condition is tested first Loop is controlled by a counter Syntaxes for (initial value ; condition; update counter) statement; Or for (initial value ; condition; update counter) { statement; statement; } TK1913-C Programming 22

Example Write a program which does the following: Reads 5 integers and displays the sum of all integers Input example: 3 6 4 1 2 Output example: 16 TK1913-C Programming 23

Recall the flowchart counter 1, sum 0 true counter < 6 input x sum sum+ x false counter++ output sum TK1913-C Programming 24

Note the initial value of i and condition How many times does the loop get executed? i 1, sum 0 true i < 6 input x sum sum+ x i++ false output sum TK1913-C Programming 25

i 0, sum 0 How many times does the loop get executed? true i < 6 input x sum sum+ x i++ output sum false TK1913-C Programming 26

i 0, sum 0 How many times does the loop get executed? true i < 5 input x sum sum+ x i++ output sum false TK1913-C Programming 27

The C statements: int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); TK1913-C Programming 28

i 0, sum 0 true i < 5 input x sum sum+ x i++ output sum false int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); TK1913-C Programming 29

for statement Example: for ( num = 1; num <= 3; num++ ) printf( %d\t, num);??? num 1 TK1913-C Programming 30

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num _ TK1913-C Programming 31

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num _ TK1913-C Programming 32

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 1 num 1 _ TK1913-C Programming 33

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 num 1 _ TK1913-C Programming 34

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 num 1 _ TK1913-C Programming 35

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 2 bil 1 2 _ TK1913-C Programming 36

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 _ TK1913-C Programming 37

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 _ TK1913-C Programming 38

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 3 num 1 2 3 _ TK1913-C Programming 39

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 4 num 1 2 3 _ TK1913-C Programming 40

for statement Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); 4 num 1 2 3 _ TK1913-C Programming 41

for while do-while TK1913-C Programming 42

Loop: while Condition is tested first Loop is controlled by condition or a counter Syntax while (condition) statement; Or while (condition) { statement; statement; } TK1913-C Programming 43

Recall this example: Given an exam marks as input, display the appropriate message based on the rules below: If marks is greater than 49, display PASS, otherwise display FAIL However, for input outside the 0-100 range, display WRONG INPUT and prompt the user to input again until a valid input is entered TK1913-C Programming 44

input m m<0 m>100 true WRONG INPUT false Exercise: Convert this flowchart to a C program input m false m>49 PASS FAIL true TK1913-C Programming 45

int marks; scanf( %d,&marks); while (marks<0) (marks>100) { printf( WRONG INPUT ); scanf( %d,&marks); } if (marks>49) { printf( PASS ); else printf( FAIL ); } Double Selection TK1913-C Programming 46

Exercise Given a set of integers with the last one being 999 Display the summation of all the integers. Input example: 1 3 23 999 Draw the flowchart for this problem Output example: Sum = 27 TK1913-C Programming 47

Sentinel-controlled loop sum=0 input x x!=999 sum sum+x input x output sum false true #include <stdio.h> void main() { int sum, x; sum = 0; scanf( %d, &x); while (x!= 999) { sum = sum + x; scanf( %d, &x); } printf( The sum : %d\n, sum); } TK1913-C Programming 48

int sum, x; sum = 0; scanf( %d, &x); while (x!= 999) { sum = sum + x; scanf( %d, &x); 13 23 999!=!= 999!= 999 } printf( \nthe sum : %d\n, sum); x 999 23 13? sum 4+23 0+1 1+3 27 01 4? _ 1 3 23 999 The sum : 27 TK1913-C Programming 49

Do-while Loop Statements in the loop are executed first (at least onc, and condition is tested last Loop is controlled by a condition or counter Syntax do { statement; statement; } while (condition); statement; TK1913-C Programming 50

do-while statement Example : printf( Input start and end value : ); scanf( %d %d, &start, &end); do { printf( %c (%d)\n, start, start); start++; } while (start <= end) ; 66 67 68 <= 67 _ Input start and end value : _ 65 67_ A (65) _ B (66) _ C (67) _??? 65 66 67 68??? 67 start end TK1913-C Programming 51

_ 0 is an even number._ Print iff even! continue statement _ 2 is an even number._ Print iff even! _ 4 is an even number._ Print iff even! Example: _ for ( i = 0; i <= 5; i++ ) { if ( i % 2 ) continue; else printf( %d is an even number., i); printf( Print iff even! \n ); } i i <= 5 i % 2 01 56 23 4 601 23 4 <= 5 <= 59 false 5 true 10 TK1913-C Programming 52

Now what can you conclude about continue? TK1913-C Programming 53

break statement Input a value between 1-7: _ 45 1 2 3YeePee! I m out of the loop! printf( Input a value between 1 7: ); scanf( %d,&value); for (i = 1; i <= 7; i++) { if ( i = = value ) break; printf( \n%d,i); } printf( YeePee! I m out of the loop!\n );??? 4 value i i <= 7 i == value 12 34 42 31 <= <= 7 7 true 23 14 == 4 false true TK1913-C Programming 54

Now what can you conclude about break? TK1913-C Programming 55

End of 1 st half of the semester Yes!! That s all? What s next??? Procedure & Function TK1913-C Programming 56