Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.

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.
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
CS0004: Introduction to Programming Repetition – Do Loops.
If Statements & Relational Operators Programming.
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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
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.
Programming is instructing a computer to perform a task for you with the help of a programming language.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Basic Control Structures
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Unary Not operator !  !true = false  !false = true.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
The Repetition control structure using while loop.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Introduction to Computer Programming
Chapter 9 Repetition.
while Repetition Structure
Repetition Control Structure in C++ Program
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Engineering Problem Solving with C++, Etter/Ingber
ALGORITHMS & FLOWCHARTING II
Control Statements Kingdom of Saudi Arabia
Repetition Structures (Loops)
Programming Fundamentals
Chapter 5 Repetition.
Intro to Programming Week # 3 If-else Statement Lecture # 6
Introduction to Programming
Introduction to Algorithms and Programming COMP151
Conditional Construct
Repetition Control Structure
Summary Two basic concepts: variables and assignments Basic types:
2.6 The if/else Selection Structure
Presentation transcript:

Chapter 6

 Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence of statements either as long as or until a certain condition is true.   For…Next Loop is referred to as a counter- controlled loop

 A counter is a numeric variable.   How is the counter used in the for..next loop?  The counter is initialized or assigned an initial value. The value of the counter either increases or decreases after each pass through the loop.

 SYNTAX #1  for( j= a; j<= b; j++)  { Statement(s);}  SYNTAX #2  for( j= a; j>= b; j--)  { Statement(s);} j is the counter Statement(s) execute as long as j is lesser than or equal to b j is increased by 1 in every execution j decrements by 1 in every execution

 Where: j = the loop variable and a = the initial value of the loop variable.  J is initially assigned the value a. Next, the loop test if j is less than or equal to b when S is positive or if j is greater than or equal to b when S is negative. If so, the program executes the statements, replaces the value of j with the value j+s, and repeats the test.  This process continues until the test fails, at which time the program exits the loop and execute the statement after the loop.

 The condition must be true in order for the test to be passed.  Syntax: do{ Statements/s; }while(condition);  Example: do{ cout<<"Enter character:"; cin>>x; }while(x!='x'); Cout and cin executes as long as x is not equal to ‘x’

 The condition must be true in order for the test to be passed.  Syntax: while(condition){ Statements/s; }  Example: while(x!='x'){ cout<<"Enter character:"; cin>>x; } Cout and cin executes as long as x is not equal to ‘x’

 EXAMPLES:   int J = 0;  do{  cout<< J;  J = J + 1;  }while( J <= 4);   int J = 0;  while(J<=4)  { cout<< J;  J = J + 1;  } 

1) Draw a flowchart to read in and print 5 numbers

 2. Draw a flowchart to sum numbers 1 to 5.

 1. Given the following input 10, 12, -5, -6, 5, trace the output.

 Draw a flowchart to find the largest of three numbers A, B and C. start Read A, B, C IS B>C? IS A>B? IS A>C? Print B Print C Print A end No Yes No Print C

 #include  using namespace std;  int main()  {  int A, B, C;  cout<<"Enter 1st #:";  cin>>A;  cout<<"Enter 2nd #:";  cin>>B;  cout<<"Enter 3rd #:";  cin>>C;  if(A>B) {  if(A>C) cout<<"Highest is "<<A<<endl;  else  cout<<"HIghest is "<<C<<endl;  }  else{  if(B>C) cout<<"Highest is "<<B<<endl;  else  cout<<"Highest is "<<C<<endl;  }  system("PAUSE");  return 0;  }

 Draw a flowchart for computing factorial N(N!) Where N! = 1 x 2 x 3...N start Read N M=1 F=1 F=F*M Is M = N? M=M+1 Print F end No Yes

 #include  using namespace std;  int main()  {  int N,F,M;  M=F=1;  cout<<"Enter #:";  cin>>N;  for(M=1; M<=N; M++){  F=F*M;  if(M==N)cout<<F<<endl;  }  system("PAUSE");  return 0;  }