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.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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.
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.
Chapter 5: Loops and Files.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 4 Program Control Statements
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CPS120 Introduction to Computer Science Iteration (Looping)
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
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,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 05 (Part III) Control Statements: Part II.
Bordoloi and Bock Control Structures: Iterative Control.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
 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)
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
REPETITION CONTROL STRUCTURE
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
- Additional C Statements
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

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 becomes false, the loop ends and control passes to the statements following the loop. There are three kinds of loops in C++: the for loop, the while loop, and the do loop. LOOPS

THE for LOOP Syntax : for (init expr; test expr; incr expr ) { statement(s); } –init expr sets the initial value of the loop control variable which acts as a counter. –test expr is a conditional expression that is used to terminate the loop. –Incr expr is some modification of the loop control variable. –The body of the loop may have one statement or a block of statements enclosed in { }.

THE for LOOP Execution of the loop is as follows: –First, init expr is evaluated. –Then test expr is tested if it is true then body of the loop is executed. –After its execution, incr expr is evaluated and test expr is tested again. –The loop terminates when test expr becomes false.

#include void main( ) { clrscr ( ); int n; for (n =l; n<=10; n++) cout << n <<"\t" << n*n << “\t" << n*n*n « “\n”; } Example of a for Loop

1. We can increase as well as decrease the value of loop control variable by any amount. For example for (n=1;i<=15; n=n+3) 2. We can test a condition other than the number of iterations to terminate the loop. for (n=1; n*n<=10; n++) 3. The increment or decrement need not be an integer. for (n=50; n<=100; n=n*1.5) cout « n; VARIATIONS OF for LOOP

4. The termination condition controlling the loop may be any valid C++ expression. m=5; for (n=1; m<=35; m=++n* 5+5) cout « n « m; 5. The pieces of the loop definition need not be there. (don't omit semicolons). n=2; for (i=3; n<=20;) n = n*i;

6. The value of the control variable can also be input from the keyboard. for (n=5; n!=0 ;) { cout << "n= " << n; cout << "Enter a number "; cin >> n; } 7. We can change the value of the loop control variable inside the body of the loop. for (n=0; n<=10 ;) ++n; VARIATIONS OF for LOOP

8. Another variation of the for loop is that we can move the initialization section outside the loop. n=5; for ( ; n<=10 ;) ++n; 9. The expression1 need not be an assignment statement. It could, for example, be a cout statement. for (cout « "Enter a number “ ; n!=0 ;) cin » n; VARIATIONS OF for LOOP

10. We can create an infinite loop using the following general form of for loop for (; ;) statement; 11. The body of the loop may be empty. This type of loop is used to introduce a time delay. for (n=1; n<=100; n++) ; Notice the semicolon that terminates the for statement, for statement expects a statement in the body of the loop, which, in this case, is empty.

The comma operator extends the flexibility of for loop by allowing to include multiple expressions for any or all the expressions in the loop. #include void main ( ) { int n,m; for (n=0,m=0; n<=5; ++n, m+=2) cout « n « "\t" « m « '\n"; } THE COMMA OPERATOR

The general form of while loop is while (expression) { statement(s); } The while loop first evaluates the expression if it is true then statement(s) is executed and expression is evaluated again. If the expression is false then while loop terminates and the control passes to the statement immediately following while loop. The statement may be a single statement or a block of statements. THE while LOOP

#include void main ( ) { int no=10; while (no > 0) { cout« no « '\t" « no*no « '\t" « no*no*no « '\n"; no --; } THE while LOOP

We can use a while loop to check the validity of an input. For instance, the following while loop keeps on executing until a 'Y' or 'N' is entered. The function getchar( ) used in the loop, reads a single character from the keyboard. This function is defined in stdio.h header file while ((ch =getchar ( )) != ‘Y’ | | ch != ‘N’) THE while LOOP

The general form of the loop is do { statement(s) } while (expression); First, the statement(s) is executed and then the expression is evaluated. If the expression is true, the statement is executed again, otherwise the control passes to the statement immediately following the loop. THE do - while LOOP

Unlike for and while loops in which the condition is checked at the top of the loop, the do-while loop checks the condition at the end of the loop. A do-while loop is executed at least once. Although the braces are optional if a single statement is present, it is a good programming practice to use them to improve the readability of the program. THE do - while LOOP

# include void main ( ) { int n1, n2, sum, product; char response; do { cout << “\n Enter two numbers : “; cin >> n1 >> n2; sum = n1 + n2; product = n1 * n1; cout << sum << product << “\n”; cout << “ Press N for No and any other key to continue “; cin >>response; } while (response != ‘N’); }

A nested loop is a loop that is inside another loop. The inner loop must finish inside the outer loop that is, the body of the inner loop is fully contained in the outer loop. for(exp1; exp2; exp3) { statement; do { statement; } // end of for loop } while (exp4); // end of do loop is invalid. Why? NESTED LOOPS

for (i=1; i<=2; i++) for (j=1; j<=2; j++) cout « i « '\t" « j « '\n"; What is the output? NESTED LOOPS

Loops & Decisions Relational Operators >, =, <=, != Program statement which alter the flow of exec of program are called “Control Statements” types are Loops & Decision Loops –The for Loop Initail exp, test exp, incre exp –The while Loop Test exp

–The do while Loop The body of loop exec at least once, test exp is checked at the end Decisions –The if Statement Multiple statement in the if body –Nesting ifs Inside Loop if can be nested inside loop body

Library Function exit() –Causes the program to terminate no matter where ever it is in the listing, value 0 mean successful termination if else statement –If statement let you do one thing if exp is true if exp is false it let you do another getche() Library Function –Get character echo, declared in the conio.h, get the character from keyboard one at a time and display it on the screen

The assignment Expression a=b=c=d=10; is valid assignment expression, has lower precedence Nested if else or else if statement –This depict a decision ladder Matching the else –else is matched to the nearest if which does not have it own else The break statement: can occur in the switch and loop, causes to exit the switch and loop, control goes the statement following loop and switch

Switch vs if else: switch case variable are either character or int constant. In case of if else constructs let you use unrelated variables as complex as you like –If(SteamPressure*Factor > 56) //statement –else if (day == Thursday) // statement

Conditional Operator –Ternary operator op on three operands, test exp, exp2, exp 3, can be used whereever if else can be used –Result=(test exp)?(exp2):(exp3); –?: are conditional operators, if test exp is true, variable Result is assigned value with ?, otherwise with :

Logical Operators: logically combine the boolean variable if(x>y && y>z) operators are && logical AND, logical OR||, ! Logical Not The continue statement: when ever executed in the loop causes the con to go in the start of the loop, due to result of some unwarranted situations The goto statement : causes the control to go to the label, it is unconditional Jump

Unary!, ++, -- Arithmetic*, /, % +, - Relational!=,,>=,<= = =, Logical&& | Conditional? : Assignment=, +=, -=, *=, /=, %= OPERATOR PRECEDENCE SUMMARY