CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 5: Loops and Files.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Introduction to Computer Programming in c
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 5: Control Structures II (Repetition)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1. Agenda for loop Short-handed notation How to use break and continue 2.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
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;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Overview Go over parts of quiz? Another iteration structure for loop.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Arrays, For loop While loop Do while loop
- Additional C Statements
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Chapter 4 - Program Control
Loops in C.
The University of Texas – Pan American
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
CS2011 Introduction to Programming I Loop Statements (II)
Alternate Version of STARTING OUT WITH C++ 4th Edition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

CSCI 130 for Loops Chapter 7 - A

Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated programs

Program Control Change order in which program statements are executed –logic may no longer be top down Done by using Program Control Statements –for loops –while loops

for statement Executes a block of code a predetermined number of times –number of executions is known prior to iterations being performed Counted Repetition Structure (CIS 101)

for Statement Format: for (initial; condition; increment) 1. initial evaluated (usually assignment) 2. condition evaluated (usually relational) 3.condition is false - terminate for loop condition is true - C statements within loop execute 4. increment executed - return to step 2

Pseudocode for for loop Write the pseudocode to display the first 3 numbers out to the console DOWHILE index = 1 TO 3 OUTPUT index ENDO

Example of for loop Write the C code to display the first 3 numbers out to the console //DEMONSTRATING A FOR LOOP #include void main() { int count; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }

Example of for loop Write the C code to display the first 3 numbers out to the console //DEMONSTRATING A FOR LOOP #include void main() { int count; for (count = 3; count >= 1; count --) { printf(“\n%d”, count); } }

Flexibility of for statement count = 1 for (; count < 1000; count++) IS equivalent to: int count; for (count = 1; count < 1000; count ++)

Scope of Variables Index value holds a value after loop void main() { int count; for (count = 1; count <= 3; count ++) { printf(“\n%d”, count); } }

Omission of increment void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); } }

Omission of Increment - cont’d void main() { int count; for (count = 1; count <= 3;) { printf(“\n%d”, count); count++; } }

Quick Introduction to Arrays Indexed group of storage locations Same type of variables Identified by subscript (i.e. the index) int data[1000]; –creates a storage location for 1000 integer elements in the variable data First location in an array is 0 (not 1)

Referencing elements in an array int i; int data[10]; data[0] = 3; data[4] = 7; for (i = 0; i < 10; i++) { printf(“%d “, data[i]); }

Referencing elements in an array Can use variables or integers int count = 3; data[count] is equivalent to: data[3];

Logical operators in a for loop Assume a 10 element array with random integers has been defined Search the array to determine if the input value (searchElement) is in the array: scanf(“%d”, &searchElement); for (cnt = 0; cnt < 10 && searchFlag = = ‘N’; cnt++) { if (array[cnt] == searchElement) searchFlag = ‘Y’; }

Null statement following for For loop does not necessarily have to have programming statements within the body of the loop Example: for (cnt = 0; cnt < 10; array[cnt++] = 50);

Commas in for loops Expressions can be separated by commas –Subexpressions evaluated left to right Write a for loop to copy the contents of a 100 element array called x into a 100 element array called y in the reverse order for (i = 0, j = 99; i < 100; i++, j--) { y[i] = x[j]; }

Nested for statements One loop may be embedded within another loop Nested for loops may be inefficient

Nested for example void main() { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(“X”); } printf(“\n”); } }