Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

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.
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.
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.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
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.
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
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.
COMP Loop Statements Yi Hong May 21, 2015.
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”
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 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.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Algorithm & Flow Charts Decision Making and Looping
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
The Repetition control structure using while loop.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
JavaScript: Control Statements I
Chapter 4 Repetition Structures
Chapter 4 Repetition Structures
Looping and Repetition
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Computing Fundamentals
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Chapter 4 Repetition Structures
Programming Fundamental
Presentation transcript:

Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

LOOPS STATEMENT A statement or set of statements that is executed repeatedly is called loop. The statements in a loop are executed for a specified number of times or until some condition remain true. in C++ there are three kinds of loop statements. These are The while loop The do while loop The for loop 2

THE WHILE LOOP It is a conditional loop statement. It is used to execute a statement or set of statements as long as the given condition remains true. The syntax of the while loop is while (condition) statement; Condition: it consists of a relational expression. If it is true statements given in while loop is executed. 3

Conti…. statement: it represents the body of loop. The compound statements are written in braces { }. syntax for compound statements. while(condition) { statement-1; statement -2; } 4

Program to print statement using while loop # include Void main() {clrscr(); int a; a=1; while(a<=5) { cout<<“I am a student”<<endl; a=a+1; } cout<<“program Ends” ; getch(); } 5

Conti………… when “while” loop is executed, the computer first evaluates the given condition. If the given condition is true the statement or set of statements under while is executed. After executing the statements under while the control shifts back to while and the condition is again tested. If the given condition becomes false at any stage the execution of the 6

Conti…. body of loop is terminated. And control shifts to the statement that comes immediately after the body of the loop. The body of the loop must contain a statement so that so that the condition on the loop becomes false during execution of the loop. If the condition of the loop never becomes false, the loop never ends. It becomes an infinite. 7

Flow chart for while loop Condition Body of loop Statement after while loop false true 8

Program to calculate the sum of first ten odd numbers. also print the odd number. #include void main() { int s, n; s=0; n=1; while (n<=10) { s=s + n; cout<<n<<endl; n=n+2; } cout<<“sum =”<<s<<endl; getch(); } 9

Program to print natural numbers 1 to 10 in descending order #include void main() { int n=10; while(n!=0) cout<<n--<<endl; } 10

The “do-while” loop The “do-while ” loop is also conditional loop statement. It is like a while loop but in this loop the condition is tested after executing the statement of the loop. the syntax of the “do while ”loop. do { statement; } while(condition); 11

Conti…. #include void main() { int n; n=1; do { cout<<n<<endl; n++; } while(n<=10); getch(); } 12

Flow chart for while loop Condition Body of loop Statement after while loop false true 13

#include void main() { int marks; char name[25],address[15],op; do { clrscr(); cout<<“Enter name of student”<<endl; cin>>name; cout<<“Enter address of student”<<endl; cin>>address; cout<<“Enter marks obtained”<<endl; cin>marks; cout<<“more record [Y/N]”; cin>>op; } while(op==‘y’|| op==‘Y’); getch() } 14

The for loop The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also known as counter loop. The structure of this loop is different from both the while and do-while loop. It has following parts o initialization o condition o increment o body of loop 15

Conti…….. for (initialization;conditio;increment/decrement) for(int a=5;a<10;a++) In initialization part the value in the variable is assigned when control enters into the loop first time. The initialization part of “for loop” is optional. If this part is committed then a semicolon is used in its place. the syntax of the for loop without initialization part is for(; a<=10;a++) 16

Conti………… in condition part the test condition is given. The body of the loop executes as long as this condition remains true. for example a<=10. in increment part the value in the variable is increased. the statement under the for loop are the body of loop. If more than one statement are to be executed these are enclosed in braces 17

Write program to print first ten natural number #include void main() {int c; for (c=1;c<=10;c++) cout<<c<<endl; getch(); } this program can also be written as void main() { int c=1; for(;c<=10;) { cout<<c<<endl; c++ } 18

FLOW CHART The for loop 19 Initialize variable Body of loop Incremental expression condition exit false true

20