LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
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.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
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 4: Loops and Files
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition Statements while and do while loops
Bordoloi and Bock Control Structures: Iterative Control.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: 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.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
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.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
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
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Lecture 7: Repeating a Known Number of Times
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
Arrays, For loop While loop Do while loop
Loops October 10, 2017.
- Additional C Statements
Control Structures Repetition
Chapter 2.2 Control Structures (Iteration)
Dale Roberts, Lecturer IUPUI
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
CSCI 1100/1202 February 6, 2002.
break & continue Statements
Control Statements:.
Presentation transcript:

LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS

LOOP n A statement or a set of statement that is executed repeatedly is called a loop.There are three kinds of Loops in C++ for loop while loop do-while loop

for Loop n The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also know as “counter loop”. It has the following parts n Initialization n Condition/Test expression n Increment /Decrement n Body of the Loop The syntax is: 1)for ( initialization; condition; inc/dec ) { Body of the loop }

n Initialization: The value of variable(s) of assigned when control enters into the loop first time.It is optional.if it is to be omitted then a semicolon is used in its place. n Condition: The body of the loop executes as long as this condition remains true. n Increment/Decrement: the value of the variable(s) is incremented or decremented. For more than one variable they are separated by commas. This part is executed after executing the body of the loop..Its use is also optional. If it is not used then the control variable must be inc/dec inside the body of the loop. n Body of the loop: If more than 1 statement are to be executed these are enclosed in braces.

n for (int a=2;a<=16; a=a+2) cout<<“\t”<<a; Output: n int x=2; for (;x<=16; ) { cout<<“\t”<<x; x=x+2; } Output: No Semi colon

condition TRUE FALSEFLOWCHART Body of the Loop Inc/Dec Exit Initialization

while Loop n It is a conditional loop. It is used to execute a statement or a set of statements as long as the given condition remains true. n The Syntax is while(condition) { statement(s); } If the loop never ends (the condition is never false),it is said to be an infinite loop

condition TRUE FALSE FLOWCHART Body of the Loop Exit

n int n=2; while(n<10) { cout<<“\t”<<n; n=n+2; } Output: 2468 n NOTE: while(0) is always a false loop while(2) is an infinite loop

do-while Loop n It is also conditional loop. It is similar to the while loop but the condition is tested after executing the statements of the loop n The Syntax is do { statement(s); } while (condition); It is useful when the body of the loop is to be executed at least once.

condition TRUEFALSE FLOWCHART Body of the Loop Exit

# include void main() { int x=2; do { cout<<“\t”<<x; x=x+2; }while(x<10); } n Output?? 2468 Semicolon

The continue statement n It shifts the control back to the beginning of the loop.It skips the remaining statements within the loop body. continue Start of loop condition

//Factorial Program # include void main() { long int fact,n,c; cout<<“\nEnter a number: ”; cin>>n; fact =1; while(n>=1) { fact = fact*n ; n-- ; } cout<<“\nFactorial= ”<<fact; }