Introduction to Algorithms and Programming COMP151

Slides:



Advertisements
Similar presentations
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.
Advertisements

The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
Start. More Loops 03/14/11 Look at geometric series example.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
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,
CS101 Computer Programming I Chapter 4 Extra Examples.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Repetition Statements while and do while loops
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 4 Loops.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Lab 4: Loops and Iteration Graham Northup
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 4 Loops Case Studies
FOR LOOPS.
CSC1201: Programming Language 2
While Loop Design ENGI 1020 Fall 2018.
Conditional Construct
Lecture Notes – Week 3 Lecture-2
Control Structures Repetition
Repetition Control Structure
Computing Fundamentals
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Control Structures Part 1
CS1201: Programming Language 2
CSC1201: Programming Language 2
Recursion.
Introduction to Algorithms and Programming
Control Structures Selection
CS1201: Programming Language 2
Introduction to Algorithms and Programming
Introduction to Algorithms and Programming COMP151
Introduction to Algorithms and Programming COMP151
Programming Fundamental
Presentation transcript:

Introduction to Algorithms and Programming COMP151 LAB 7 while loop do while loop Introduction to Algorithms and Programming COMP151

while loop Statement Condition list while (Condition) { Statement list } Statement list T F

Trace while Loop int count = 0; while (count < 2) { Initialize count int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

cout << "Welcome to C++!"; count++; } (count < 2) is true int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

cout << "Welcome to C++!"; count++; } Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

cout << "Welcome to C++!"; count++; } Increase count by 1 count is 1 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

(count < 2) is still true since count is 1 int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

cout << "Welcome to C++!"; count++; } Print Welcome to C++ int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

Increase count by 1 count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

(count < 2) is false since count is 2 now int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

The loop exits. Execute the next statement after the loop. int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

condition that will become false at some point Important Note When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. condition that will become false at some point int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; }

Write C++ program to sum the numbers from 1 to 20 using while loop Exercise 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Write C++ program to sum the numbers from 1 to 20 using while loop sum??

#include<iostream> using namespace std; int main() { int number=1,sum=0; while(number<=20) sum=sum+number; number=number+1; } cout<<"The sum is"<<sum<<endl; return(0); output: The sum is210

starting numbers of the series Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 starting numbers of the series

Write C++ program to print the Fibonacci series using while loop Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 0+1=

Write C++ program to print the Fibonacci series using while loop Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 1+1=

Write C++ program to print the Fibonacci series using while loop Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 1+3=

Write C++ program to print the Fibonacci series using while loop Exercise 2 Write C++ program to print the Fibonacci series using while loop 0 1 1 2 3 5 8 13 21 34 2+3=

Step 1 0 1 1 2 3 5 8 13 21 34 N1 N2 sum Step 2 0 1 1 2 3 5 8 13 21 34 N1 N2 sum

Step 3 0 1 1 2 3 5 8 13 21 34 N1 N2 sum Step 4 0 1 1 2 3 5 8 13 21 34 N1 N2 sum

#include<iostream> using namespace std; int main() { int n1=0,n2=1,sum=0,n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<n1<<"\t"<<n2; while(n>2) sum=n1+n2; cout<<"\t"<<sum; n1=n2; n2=sum; n=n-1; } return(0); output:

Exercise 3 Write C++ program to find the factorial of a given number using while loop

#include<iostream> using namespace std; int main() { int number,fact=1; cout<<"Enter the number"<<endl; cin>>number; while(number>0) fact=fact*number; number=number-1; } cout<<"The factorial of the number is"<<fact<<endl; return(0);

do while loop do { // Loop body; Statement(s); } while (loop-continuation-condition); Important note: do while loop guarantees at least one execution of the body

Write C++ program to sum the numbers from 1 to 20 using do while loop Exercise 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Write C++ program to sum the numbers from 1 to 20 using do while loop sum??

#include<iostream> using namespace std; int main() { int number=1,sum=0; do sum=sum+number; number=number+1; } while(number<=20); cout<<"The sum is"<<sum<<endl; return(0);

starting numbers of the series Exercise 5 Write C++ program to print the Fibonacci series using do while loop 0 1 1 2 3 5 8 13 21 34 starting numbers of the series

#include<iostream> using namespace std; int main() { int num1=0,num2=1,sum=0,n; cout<<"Enter the value for n"<<endl; cin>>n; cout<<num1<<"\t"<<num2; do sum=num1+num2; cout<<"\t"<<sum; num1=num2; num2=sum; n=n-1; } while(n>2); return(0);

Exercise 6 Write C++ program to find the factorial of a given number using do while loop

#include<iostream> using namespace std; int main() { int number,fact=1; cout<<"Enter the number"<<endl; cin>>number; do fact=fact*number; number=number-1; } while(number>0); cout<<"The factorial of the number is"<<fact<<endl; return(0);