Repetition (While Loop) LAB 9

Slides:



Advertisements
Similar presentations
Exercise (1).
Advertisements

You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.
Introduction to Computing Science and Programming I
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
What is the out put #include using namespace std; void main() { int i; for(i=1;i
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
More While Loop Examples CS303E: Elements of Computers and Programming.
You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.
Why Files? ♦ the amount of data read and / or produced is huge ♦ repetitive data is needed for more than one program ♦ data may be entered by other people.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Count and add list of numbers From user input and from file.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Python break,continue and pass Statements. The break Statement: for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
1 Chapter 8: File Processing Part 1. 2 Outline  Why Files?  Opening Files  Reading from Files  Writing to Files  Working with Multiple Files  Closing.
Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Computer Programming -1-
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
REPETITION CONTROL STRUCTURE
for Repetition Structures
ALGORITHMS AND FLOWCHARTS
PROGRAM CONTROL STRUCTURE
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 07 More Repetition Richard Gesick.
Chapter 5 Repetition.
Repetition and Loop Statements
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
Structured Program Development in C
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
INC 161 , CPE 100 Computer Programming
Loop Construct.
FILE PROCESSING Opening Files Why Files?
DATA TYPES AND OPERATIONS
ICS 101 Lab 3 Hossain Arif ICS Dept
Programming Fundamental
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

Repetition (While Loop) LAB 9 ICS 101 Lab Hossain Arif ICS Dept

Repetition DO Loops Last LAB WHILE Loops This LAB

WHILE Loop Informal representation WHILE condition EXECUTE THE following BLOCK OF STATEMENTS FORTRAN does not have explicit WHILE statement

WHILE Loop WHILE Loop can be written as follows: n IF (condition) THEN Block of statements GOTO n ENDIF

Example Consider this program – calculates average of 6 numbers REAL X1, X2, X3, X4, X5, X6 REAL SUM, AVG READ*, X1 READ*, X2 READ*, X3 READ*, X4 READ*, X5 READ*, X6 SUM = X1 + X2 + X3 + X4 + X5 + X6 AVG = SUM / 6.0 PRINT*, AVG END

Example (Cont) Using WHILE loop REAL X REAL SUM, AVG INTEGER K 15 IF ( K .LT. 6 ) THEN READ*, X SUM = SUM + X K = K + 1 GOTO 15 ENDIF AVG = SUM / 6.0 PRINT*, AVG END

Example(2) REAL WEIGHT READ*, WEIGHT 11 IF ( WEIGHT .NE. -1.0 ) THEN IF ( WEIGHT .LT. 0 .OR. WEIGHT .GE. 400 ) THEN PRINT*, ‘Weight is OUT of range’ ELSEIF ( WEIGHT .LT. 65 ) THEN PRINT*, ‘LIGHT-WEIGHT’ ELSEIF ( WEIGHT .LT. 85 ) THEN PRINT*, ‘MIDDLE-WEIGHT’ ELSE PRINT*, ‘HEAVY-WEIGHT’ ENDIF GOTO 11 END

Nested WHILE Loops WHILE Loops can be nested Inner loop must start after outer loop and must finish before it You can have as many nested loops as needed

Implied Loops Used in READ and PRINT statements Implied Loop can be written as follows: READ*, (list of variables, index = initial, limit, increment) PRINT*, (list of expressions, index = initial, limit, increment) The same DO loops rules are applied

Implied Loop Example What will printed by this line: PRINT*, (K, K = 1, 10, 1) Output: 1 2 3 4 5 6 7 8 9 10

Exercise(1) Using While loop, write a FORTRAN program that calculates the summation of all odd numbers less than or equal to NUM 1 + 3 + 5 + … + NUM Ask the user for the value of NUM.