Programming Fundamentals

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

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.
Computer Science 1620 Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
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.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Previously Repetition Structures While, Do-While, For.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
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.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
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.
Chapter 05 (Part II) Control Statements: Part II.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
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-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
Programming Fundamentals
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Loop Control Structure.
For & do/while Loops.
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Computing Fundamentals
Week 6 CPS125.
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
Repetition Statements (Loops) - 2
Presentation transcript:

Programming Fundamentals Lecture 13 Ms. Farah Younas

Agenda of Lecture do..while Loop Deitel & Deitel C++, How to program Chapter 5 - Control Statements: Part 2 Page 174

do…while loop The do…while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do…while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once.

do…while loop Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax of do…while loop statement(s); }while( condition ); the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

Syntax of do…while loop It’s not necessary to use braces in the do…while statement if there is only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do…while statements For example while ( condition ) do statement while ( condition );

Flow Diagram of do…while loop

do…while loop – how it works execute action if condition is true then execute action again repeat this process until condition evaluates to false. action is either a single statement or a group of statements within braces.

Flow Diagram of do…while loop

Example of do…while loop int main () { // Local variable declaration: int a = 10; // do loop execution do { cout << "value of a: " << a << endl; a = a + 1; }while( a < 20 ); return 0; }

Output of do…while loop value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

do…while loop – Activity Program to add numbers until user enters zero

do…while loop int main() { double number, sum = 0; // loop body is executed at least once do cout<<“Enter a number: "; cin>>number; sum += number; } while(number != 0.0); cout<<"Sum = “ << sum; return 0;

do…while loop – calculating factorial (N!) int main(){ int number, factorial, counter; cout << "Enter a positive integer:"; cin >> number; factorial = 1; counter = 1; do{ factorial *= counter; counter++; } while(counter <= number); cout << "The factorial of " << number<< " is " << factorial << endl; return 0;}

do…while loop – calculating factorial (N!) int main(){ int number, factorial, counter; cout << "Enter a positive integer:"; cin >> number; factorial = 1; counter = 1; do{ factorial *= counter; counter++; } while(counter <= number); cout << "The factorial of " << number<< " is " << factorial << endl; return 0;}

do…while loop – Activity Calculating power of 2 (2^N)

do…while loop – calculating power of 2 (2^N) int main(){ int number, result, counter; cout << "Enter a positive integer:"; cin >> number; result = 1; counter = 1; do{ result *= 2; counter++; }while (counter <= number); cout << "Two raised to the " << number << " power is " << result <<endl; return 0;}

do…while loop – calculating power of 2 (2^N) int main(){ int number, result, counter; cout << "Enter a positive integer:"; cin >> number; result = 1; counter = 1; do{ result *= 2; counter++; }while (counter <= number); cout << "Two raised to the " << number << " power is " << result <<endl; return 0;}

Questions??