IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.

Slides:



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

CS0004: Introduction to Programming Repetition – Do Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
A First Book of ANSI C Fourth Edition Chapter 5 Repetition.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
COMP Loop Statements Yi Hong May 21, 2015.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
PGT C Programming1 Week 4 – Repetition Structures / Loops.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Ch 7: JavaScript Control Statements I.
Iteration statement while do-while
Control Structures Lecture 7.
Looping.
Outline Altering flow of control Boolean expressions
Structured Program
A First Book of ANSI C Fourth Edition
3 Control Statements:.
Chapter 2.1 Repetition.
Computer Programming Techniques Semester 1, 1998
Objectives You should be able to describe: The while Statement
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

IT CS 200: R EPEATATION Lect. Napat Amphaiphan

T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2

Overview (Midterm) Midterm Examination 30 % Data Type, Declaration, Display Interactive Input SelectionRepetition Analyze Problem Solving Problem Solving int float double char printf() int float double char printf() scanf() if…else switch…case if…else switch…case while statement for statement do-while statement while statement for statement do-while statement

While LoopWhile Loop 1 For LoopFor Loop 2 Do While LoopDo While Loop 3 Today’s Overview 4

Introduction Flow of control refers to the order in which a program’s statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: – Normal flow of control for all programs is sequential – Selection is used to select which statements are performed next based on a condition – Repetition is used to repeat a set of statements – Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function

Introduction A section of code that is repeated is called a loop, because after the last statement in the code is executed, the program loops back to the first statement and starts another repetition through the code Each repetition is also called an iteration or pass through the loop

Basic Loop Structures – Repetition statement while statement for statement do-while statement – Loop elements Condition (relational expression: x<10, x<=Total) Initialization (i = 0; ) Alteration (count++, i++)

The while Statement The general form of the while statement is while (expression) statement; Or while (expression) { statement1; statement2; }

The while Statement (cont.) Process used by the computer in evaluating a while statement is 1. test the expression 2. if the expression has a nonzero (true) value a. execute the statement following the parentheses b. go back to step 1 (loop) else exit the while statement

The while Statement (cont.)

Output is:

The while Statement (cont.) Output is:

The while Statement (cont.) What is the Output ? Write a Flow Chart

Examples: Receive a number ‘n’ times

Examples: Accumulate a Sum Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement

Examples: Finding Average Calculating an average

scanf( ) & End of File (EOF) End of File (EOF) mark the end of the program

scanf( ) & End of File (EOF)

break Statements A break forces an immediate exit from while, switch, for, and do-while statements  Example of while loop is immediately terminated if a number greater than 76 is entered count = 0; while (count <= 10) { printf(“Enter a number: “); scanf(“%f”, &num); if (num > 76) { printf(“You lose!”); break; /* break out of the loop */ } else printf(“Keep on trucking!”); } /* break jumps to here */

continue Statements The continue applies to loops only; when a continue statement is encountered in a loop, the next iteration of the loop begins immediately while (count < 5) { printf("Enter a grade: \n"); scanf("%d", &grade); if(grade 100) { continue; } total = total + grade; count = count + 1; }

The for Statement General form for (initializing list; expression; altering list) statement; while statement count = 1; while (count <= 10) { printf(“%d”, count); ++count; } for statement for (count = 1; count <= 10; ++count) printf(“%d”, count);

The for Statement  Output is:

The for Statement: Finding Average

The do-while Statement The general form of the do statement is do statement; while (expression); do-while is a posttest loop One type of application is ideally suited for a posttest loop: – Input data validation application

The do-while Statement

The do-while Statement Validity Checks do { printf("\nEnter an ID number: "); scanf("%f", &idNum); } while (idNum 1999); Example An operator is required to enter a valid customer identification number 1000 through A number outside this range is to be rejected, and new request for a valid number made. Code provides the necessary data filter to verify the entry of a valid identification number.

Nested Loops

What is the Output ?

Loop Programming Techniques Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations

Technique 1: Selection within a Loop

Technique 2: Input Data Validation

Technique 3: Interactive Loop Control

Technique 4: Evaluating Equations

Lab Assignment In lab sheet 35