Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.

Slides:



Advertisements
Similar presentations
Computer Science: A Structured Programming Approach Using C1 6-7 Other Statements Related to Looping Three other C statements are related to loops: break,
Advertisements

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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
More on Recursive Recursion vs. Iteration Why Recursion?
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
1 Storage Classes, Scope, and Recursion Lecture 6.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Data Structures and Abstractions with Java, 4e Frank Carrano
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop initialization and updating ■ event and counter controlled.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Chapter 6 Questions Quick Quiz
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming Techniques
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 15 Recursion.
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
C Functions -Continue…-.
More on Recursive Recursion vs. Iteration Why Recursion?
Chapter 15 Recursion.
Quick Test What do you mean by pre-test and post-test loops in C?
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Computer Science Faculty
Programming Fundamentals
Topics discussed in this section:
Introduction to Computer Science - Alice
Java Programming: Program Design Including Data Structures
Announcements Final Exam on August 17th Wednesday at 16:00.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Looping and Repetition
Recursion Chapter 11.
MSIS 655 Advanced Business Applications Programming
Recursion Data Structures.
Topics discussed in this section:
Announcements Final Exam on August 19th Saturday at 16:00.
Chapter 6: Repetition Statements
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
do/while Selection Structure
Dr. Sampath Jayarathna Cal Poly Pomona
Looping and Repetition
Topics discussed in this section:
Presentation transcript:

Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We can use all of them for event-controlled and counter-controlled loops. Computer Science: A Structured Programming Approach Using C

C Loop Constructs Computer Science: A Structured Programming Approach Using C

For Loops Initialization of loop counters should be outside of loop. More than one initialization can occur More than one test can occur int i; int j; for (i = 1, j = 51; i <= 50 && j<75; i++, j++) Computer Science: A Structured Programming Approach Using C

Other Statements Related to Looping Three other C statements are related to loops: break, continue, and goto. The last statements, the goto, is not valid for structured programs and therefore is not discussed. Computer Science: A Structured Programming Approach Using C

break and Inner Loops Computer Science: A Structured Programming Approach Using C

The for and while as Perpetual Loops Computer Science: A Structured Programming Approach Using C

Using a break Flag Computer Science: A Structured Programming Approach Using C

The continue Statement Computer Science: A Structured Programming Approach Using C

Recursion In general, programmers use two approaches to writing repetitive algorithms. One approach uses loops; the other uses recursion. Recursion is a repetitive process in which a function calls itself. Computer Science: A Structured Programming Approach Using C

Note Every recursive call must either solve part of the problem or reduce the size of the problem. Computer Science: A Structured Programming Approach Using C

Calling a Recursive Function Computer Science: A Structured Programming Approach Using C

Towers of Hanoi—Start Position Computer Science: A Structured Programming Approach Using C

Towers of Hanoi Solution for Three Disks (Part I) Computer Science: A Structured Programming Approach Using C

Towers of Hanoi Solution for Three Disks (Part II) Computer Science: A Structured Programming Approach Using C

Towers of Hanoi Computer Science: A Structured Programming Approach Using C

Towers of Hanoi Computer Science: A Structured Programming Approach Using C

Tracing of Program, Towers of Hanoi Computer Science: A Structured Programming Approach Using C Tracing of Program, Towers of Hanoi

Static Local Variables You can include variables that persist even when a function ends. Use the word static before the declaration and what ever value it is at the end of the function will be what it is when the function starts again. Storage is allocated only once, before the program begins execution. They are known only in the function where they are declared.