Control Structures Part 2

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.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Loops and Files.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
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.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
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.
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.
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.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
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.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
The for Statement A most versatile loop
Chapter 4 Repetition Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
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.
Chapter 3: Decisions and Loops
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
Repetition Structures (Loops)
Programming Fundamentals
JavaScript: Control Statements.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Chapter 4 Repetition Structures
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.
Introduction to Functions
Chapter 8 Repetition Statements
Chapter 4 Repetition Structures
Additional Control Structures
Chapter 7 Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Objectives You should be able to describe: The while Statement
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Chapter 4 Repetition Structures
Presentation transcript:

Control Structures Part 2 Skill Area 314 Part A Materials Prepared by Dhimas Ruswanto, BMm

Lecture Overview for Loop Break Statement Continue Statement

for Loop Its format is: for (initialization; condition; increase) statement; Its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.

for Loop It works in the following way: initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.

for Loop // countdown using a for loop #include <iostream> using namespace std; int main () { for (int n=10; n>0; n--) cout << n << ", "; } cout << "FIRE!\n"; return 0;

for Loop The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs between them must be written. For example we could write: for (;n<10;) if we wanted to specify no initialization and no increase; or for (;n<10;n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before).

for Loop // Looping with for #include <iostream> using namespace std; int main() { int counter; for (counter = 0; counter < 5; counter++) cout << "Looping! "; } cout << "\nCounter: " << counter << ".\n"; return 0;

for Loop Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) is an expression separator, it serves to separate more than one expression where only one is generally expected.

for Loop For example, suppose that we wanted to initialize more than one variable in our loop: for ( n=0, i=100 ; n!=i ; n++, i-- ) { // whatever here... } This loop will execute for 50 times if neither n or i are modified within the loop: n starts with a value of 0, and i with 100, the condition is n!=i (that n is not equal to i). Because n is increased by one and i decreased by one, the loop's condition will become false after the 50th loop, when both n and i will be equal to 50.

for Loop // demonstrates multiple statements in for loops #include <iostream> using namespace std; int main() { for (int i=0, j=0; i<3; i++, j++) cout << "i: " << i << " j: " << j << endl; } return 0;

for Loop //For loops with null statements #include <iostream> using namespace std; int main() { int counter = 0; for( ; counter < 5; ) counter++; cout << "Looping! "; } cout << "\nCounter: " << counter << ".\n"; return 0;

for Loop Loops may be nested, with one loop sitting in the body of another. The inner loop will be executed in full for every execution of the outer loop.

#include <iostream> using namespace std; int main() { int rows, columns; char theChar; cout << "How many rows? "; cin >> rows; cout << "How many columns? "; cin >> columns; cout << "What character? "; cin >> theChar; for (int i = 0; i<rows; i++) for (int j = 0; j<columns; j++) cout << theChar; cout << "\n"; } return 0; for Loop

NOTE As an aside, many C++ programmers use the letters i and j as counting variables. This tradition goes all the way back to FORTRAN, in which the letters i, j, k, l, m, and n were the only legal counting variables. Other programmers prefer to use more descriptive counter variable names, such as Ctrl and Ctr2. Using i and j in for loop headers should not cause much confusion, however.

Break statement Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end #include <iostream> using namespace std; int main () { int n; for (n=10; n>0; n--) cout << n << ", "; if (n==3) cout << "countdown aborted!"; break; } return 0;

Continue statement The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration // continue loop example #include <iostream> using namespace std; int main () { for (int n=10; n>0; n--) if (n==5) continue; cout << n << ", "; } cout << "FIRE!\n"; return 0;

--- continue to next slide ---