CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer Science 1620 Loops.
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CLASS XI By Sumita Arora
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
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.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
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.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
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.
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
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.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Computer Programming -1-
Control Structures (Repetition structure) Jump Statements
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Quick Test What do you mean by pre-test and post-test loops in C?
Chapter 2.2 Control Structures (Iteration)
Looping.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 6 Decision Making and Looping
Loops in C.
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
Repetition and Loop Statements
Lab5 PROGRAMMING 1 Loop chapter4.
Computing Fundamentals
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Programming Fundamental
Presentation transcript:

CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops While Loop For Loop Do-while Loop Nested Loops Jump Statements Exercises

LOOP STATEMENTS The loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. Following is the general from of a loop statement in most of the programming languages:

PARTS OF A LOOP Initialization Expression(s) initialize(s) the loop variables in the beginning of the loop. Test Expression decides whether the loop will be executed (if test expression is true) or not (if test expression is false). Update Expression(s) update(s) the values of loop variables after every iteration of the loop. The Body-of-the-Loop contains statements to be executed repeatedly.

TYPES OF LOOPS C++ programming language provides following types of loop to handle looping requirements: Loop Type Description while loop Repeats a statement or group of statements until a given condition is true. It tests the condition before executing the loop body. for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. do...while loop Like a while statement, except that it tests the condition at the end of the loop body nested loops You can use one or more loop inside any another while, for or do..while loop.

WHILE LOOP The syntax of while statement : while (loop repetition condition) statement Loop repetition condition is the condition which controls the loop. The statement is repeated as long as the loop repetition condition is true. A loop is called an infinite loop if the loop repetition condition is always true.

Logic of a while Loop condition evaluated false statement true

EXAMPLE: cout<<“\n”<<j; #include<iostream.h> #include <stdio.h> int main(void) {       int j;       j = -5;       // while loop       while(j <= 0)       { cout<<“\n”<<j;              j= j + 1;       }       return 0; }

FOR LOOP A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false for ( initialization ; condition ; increment ) { statement; } The increment portion is executed at the end of each iteration

Logic of a for loop initialization condition evaluated false true statement true increment

EXAMPLE: OUTPUT Enter number: 3 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 //program to display table of a given number using for loop. #include<iostream.h> void main() { int n; cout<<“\n Enter number:”; cin>>n; //for loop for(int i=1;i<11;++i) cout<<“\n”<<n<<“*”<<i<<“=“<<n*i; } Enter number: 3 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30

THE FOR LOOP VARIATIONS Multiple initialization and update expressions A for loop may contain multiple initialization and/or multiple update expressions. These multiple expressions must be separated by commas. e.g. for( i=1, sum=0; i<=n; sum+=i, ++i) cout<<“\n”<<i;

Prefer prefix increment/decrement over postfix when to be used alone. for( i=1;i<n;++i) : rather than, for( i=1;i<5;i++) Reason being that when used alone ,prefix operators are faster executed than postfix. Prefer this over this

Infinite loop An infinite loop can be created by omitting the test expression as shown: for(j=25; ;--j) cout<<“an infinite for loop”; An infinite loop can also be created as: for( ; ; ) cout<<“endless for loop”;

Empty loop If a loop does not contain any statement in its loop-body, it is said to be an empty loop: for(j=25; (j) ;--j) //(j) tests for non zero value of j. If we put a semicolon after for’s parenthesis it repeats only for counting the control variable. And if we put a block of statements after such a loop, it is not a part of for loop. e.g. for(i=0;i<10;++i); { cout<<“i=“<<i<<endl; } The semicolon ends the loop here only This is not the body of the for loop. For loop is an empty loop

DO…WHILE LOOP The syntax of do-while statement in C: do statement while (loop repetition condition); The statement is first executed. If the loop repetition condition is true, the statement is repeated. Otherwise, the loop is exited.

Logic of a do…while loop statement true condition evaluated false

EXAMPLE: OUTPUT //program to display counting from 1 to 10 using do-while loop. #include<iostream.h> void main() { int i=1; //do-while loop do cout<<“\n”<<i; i++; }while(i<=10); } 1 2 3 4 5 6 7 8 9 10

NESTED LOOPS Nested loops consist of an outer loop with one or more inner loops. e.g., for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } The above loop will run for 100*50 iterations. Outer loop Inner loop

* * * * * * * * * * EXAMPLE: OUTPUT //program to display a pattern of a given character using nested loop. #include<iostream.h> void main() { int i,j; for( i=1;i<5;++i) cout<<“\n”; for(j=1;j<=i;++j) cout<<“*”; } OUTPUT * * * * * * * * * *

JUMP STATEMENTS The goto statement A goto statement can transfer the program control anywhere in the program. The target destination is marked by a label. The target label and goto must appear in the same statement. The syntax of goto statement is: goto label; … label:

The break statement The break statement enables a program to skip over part of the code. A break statement terminates the smallest enclosing while, do-while and for statements. A break statement skips the rest of the loop and jumps over to the statement following the loop. The following figures explains the working of a break statement :

for(initialize;test expression;update) { statement1; if(val>2000) break; : statement2; } statement3; WORKING OF BREAK STATEMENT IN FOR LOOP

while(test expression) { statement1; if(val>2000) break; : statement2; } statement3; WORKING OF BREAK STATEMENT IN WHILE LOOP

do { statement1; if(val>2000) break; : statement2; } while(test expression) statement3; WORKING OF BREAK STATEMENT IN DO-WHILE LOOP

The continue statement The continue statement works somewhat like the break statement. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests. Syntax: The syntax of a continue statement in C++ is: continue;

EXERCISES: MULTIPLE CHOICE QUESTIONS (MCQs) PROGRAM BASED QUESTIONS

MCQs...

1. The statement i++; is equivalent to [a] i = i + i; [b] i = i + 1; [c] i = i - 1; [d] i --; ANS: [b] NEXT QUESTION

2. What's wrong? for (int k = 2, k <=12, k++) [a] the increment should always be ++k [b] the variable must always be the letter i when using a for loop [c] there should be a semicolon at the end of the statement [d] the commas should be semicolons ANS: [d] NEXT QUESTION

3. Which looping process checks the test condition at the end of the loop? [a] for [b] while [c] do-while [d] no looping process checks the test condition at the end ANS: [c] NEXT QUESTION

4. Which looping process is best used when the number of iterations is known? [a] for [b] while [c] do-while [d] all looping processes require that the iterations be known ANS: [a] NEXT QUESTION

5. A continue statement causes execution to skip to [a] the return 0; statement [b] the first statement after the loop [c] the statement following the continue statement [d] the next iteration of the loop ANS: [d]

PROGRAM BASED QUESTIONS…

Write a program to print first n natural numbers and their sum. Write a program to calculate the factorial of an integer. Write a program that prints 1 2 4 8 16 32 64 128. Write a program to check whether the given number is palindrome or not. Write a program to generate divisors of an integer. Write a program to find whether a given number is odd or even. The program should continue as long as the user wants. Write a program to print Fibonacci series i.e.,0 1 1 2 3 5 8….

Write a program to print largest even and largest odd number from a list of numbers entered. The list terminates as soon as one enters 0(zero). Write a program to check whether the entered number is Armstrong or not. Write a program to display Pythagorean triplets up to 100. Write a program to calculate average of 10 numbers. Write programs to produce the following designs: A A B A B C A B C D A B C D E

& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &

THANK YOU