1 st Semester 2005 1 Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Computer Science 1620 Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Previously Repetition Structures While, Do-While, For.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
2 nd Semester Looping Technique FOR…DO Module5-1 Looping Technique FOR…DO อภิรักษ์ จันทร์สร้าง Aphirak Jansang
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSI 3125, Preliminaries, page 1 Control Statements.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Computer Programming
Chapter 4 – C Program Control
Unit-1 Introduction to Java
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements I
ITM 352 Flow-Control: Loops
Programming Fundamentals Lecture #6 Program Control
JavaScript: Control Statements (II)
.Net Programming with C#
Chapter 4 - Program Control
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Module8 Multi-dimensional Array
Chapter 4 - Program Control
Python While Loops.
Presentation transcript:

1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

1 st Semester /39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

1 st Semester /39 Simple C# Structure  Namespace Class  Main()Namespace Class Main() Variable & Constant Location statements WriteLine, ReadLine, if, switch, Loop Review condition statement

1 st Semester /39 Condition Statements condition if (condition) statement; condition if (condition) statement1; else statement2; ( ) switch ( ) { case : ; break; default: ; break; } Review condition statement

1 st Semester /39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

1 st Semester /39 Loop or Iteration in C# for while do…while foreach Iteration Loop statements

1 st Semester /39 C# Program with Condition Start statement1 statement2 statement3statement4 End Condition true false Loop statements

1 st Semester /39 Simple C# Program Start statement1 End statement1 Loop statements

1 st Semester /39 Simple C# Program with Loop Start x=5; statement1 x=x-1; End x>0 true false Loop statements

1 st Semester /39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

1 st Semester /39 while statement condition statement true false while (condition) statement; while statement

1 st Semester /39 while statement – more statements while (condition) { statement1; statement2; } condition statement1 true false statement2 while statement

1 st Semester /39 Example1: while statement  2 n = ???  n = 5 result=2*2*2*2*2 = 32 result=2*2*2*2*2 = 32 result=1; result = result*2; n=n-1; n>0 true false n=5; Display result See demo program!!!! prog2-loop.cs while statement

1 st Semester /39 Example2: while statement  n = 5 result = result = result=0; result = result+n; n=n-1; n>0 true false n=5; Display result See demo program!!!! prog3-loop.cs while statement

1 st Semester /39 Iteration Template  Counter Controlled  Sentinel Controlled while statement

1 st Semester /39 Counter Controlled  Control Variable – counter variable  Three component Initial counter variable condition (counter variable is still valid?) Incrementing/Decrementing counter variable while statement: counter controlled

1 st Semester /39 Example3: Count Controlled  Display 1 – 7 on screen initial counter statement; dec||inc counter condition true false See demo program!!!! prog4-loop.cs while statement: counter controlled

1 st Semester /39 Break!!!! (5 minutes) (Elevator) Neo: There is no spoon... there's a difference between knowing the path and walking the path... Morpheus: Neo, sooner or later you're going to realize, just as I did, there's a difference between knowing the path and walking the path... Trinity: Run, Neo. Run! What is he doing? Morpheus: He's beginning to believe * From The Matrix

1 st Semester /39 Iteration Template  Counter Controlled  Sentinel Controlled while statement

1 st Semester /39 Sentinel Controlled  Input driven program  #repetition cannot tell  depend on condition and input  Sentinel Value Causes loop to break while statement: sentinel controlled

1 st Semester /39 Example4: Sentinel Controlled  Find summation from user input  Exit program when user input negative number See demo program!!!! prog5-loop.cs while statement: sentinel controlled

1 st Semester /39 Break 3 Minutes “With great power comes with great responsibility” *From Spiderman

1 st Semester /39 Example 5: Display n stars See demo program!!!! prog6-loop.cs while statement: example

1 st Semester /39 Example 6: Display stars See demo!!!! prog7-loop.cs while statement: example

1 st Semester /39 Example 7: Display stars See demo!!!! prog8-loop.cs while statement: example

1 st Semester /39 Example 8: Display stars See demo!!!! prog9-loop.cs while statement: example

1 st Semester /39 Summary  Loop statements while statement do…while statement for statement  break statement