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.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Introduction to Computer Programming in c
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
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.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
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 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
1. Agenda for loop Short-handed notation How to use break and continue 2.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Iterations Very Useful: Ability to repeat a block of code Example:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 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 Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
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.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
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.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4 – C Program Control
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Quick Test What do you mean by pre-test and post-test loops in C?
Ch 7: JavaScript Control Statements I.
Chapter 2.2 Control Structures (Iteration)
Arrays, For loop While loop Do while loop
Looping and Repetition
Outline Altering flow of control Boolean expressions
How to develop a program?
Loops in C.
Chapter 2.2 Control Structures (Iteration)
Flow of Control.
More Loops Topics Counter-Controlled (Definite) Repetition
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
Looping and Repetition
Presentation transcript:

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 loop in which execution of the loop body depends on the value of a condition that is evaluated prior to the execution of the loop body. The general form of a while loop is while (condition) { statement; }

Example: Computation of Factorial The factorial of an integer n is defined by n! = n(n-1)(n-2)(n-3)…(3).(2).(1) as well as 0! = 1 n! = n(n-1)! Problem statement: write a program to compute the factorial of an integer n. Solution: The program begins by assigning an initial value of 1 to the variable factorial. The computation of the factorial is performed in the following while loop.

#include main() { int i, n; double factorial; printf(“\n Enter a positive integer: “); scanf(“%d”,&n); factorial = 1 ; i = 1; while ( I <= n) { factorial *= i; ++i; } printf(“\n %d factorial is %.01f”, n, factorial); }

5.2 The for Loop The simplest type of loop is one in which a series of statements is repeated and the number of repetitions is known in advance. This is sometimes referred to as a counting loop. In C the for statement allows us to repeat a sequence of statements a specified number of times. The general form of the for statement is for ( initialization; test condition; increment) { statement; }

Example: Computation of Factorials #include main() { int i, n; double factorial; printf(“\n Enter a positive integer: “); scanf(“%d”,&n); factorial = 1 ; for ( i=2: i <= n; ++i ) factorial *= i; printf(“\n %d factorial is %.0f”, n, factorial); }

5.3 The do-while Loop Both the while and the for loops are entry condition loops, that is, the expression that controls the loop is evaluated before each iteration of the loop. C also provides the do-while statement which enables us to implement as exit condition loop. In an exit condition loop the expression that controls the loop is evaluated at the end of each iteration instead of at the beginning. The general form of the do-while loop is do { statement; } while (expression);

For example: do { printf(“\n Enter a number between 0 and 100”); scanf(“%f”,&num); } while (num > 0 && num < 100); Note: If we want to get out form this loop, this expression is false. So we will be able to get out form this loop.

5.4 The break Statement The break statement can also be used with the while, do-while, and for loops where it performs a similar function. When a break statement is encountered within a loop the loop, is terminated and control is transferred to the next sequential statement following the loop. The break statement is used in situations where a special condition can cause termination of the loop. For example, consider the following code segment:

for ( I = 1; I <= 3: ++I ) { for ( j = 1; j <= 20; ++j ) { printf(“\n I = %d, j = %d”, I,j); if (j > 12) break; }

Any Questions