The for Loop Syntax: Same as: for (expression1;condition; expression2)

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

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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
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.
1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CONTROLLING PROGRAM FLOW
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.
The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
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.
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 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Switch Statements Comparing Exact Values
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
משפטי תנאי ( לוגיקה ) קרן כליף. 2 © Keren Kalif ביחידה זו נלמד :  משפטי תנאי  משפט switch  משפט if מקוצר.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
C++ Iterative Constructs
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Variables A piece of memory set aside to store data
Chapter 2.2 Control Structures (Iteration)
לולאות קרן כליף.
Quiz Next Monday.
Announcements Exam 1 Grades Posted on Blackboard.
Iteration with While You can say that again.
Chapter 7 Additional Control Structures
Counting Loops.
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Announcements Exam 1 Grades Posted on Blackboard.
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Statements and flow control
do/while Selection Structure
CS150 Introduction to Computer Science 1
Repetition Statements (Loops) - 2
(Dreaded) Quiz 2 Next Monday.
Announcements Exam 1 Grades Posted on blackboard Average: 83.26
Presentation transcript:

The for Loop Syntax: Same as: for (expression1;condition; expression2) statement; { } Same as: expression1; while (condition) expression2;

Example for Loop int numEmployees,curNum; cout << “Enter Number of Employees: “; cin >> numEmployees; if (numEmployees > 0) { for (curNum = 0; curNum < numEmployees;curNum++) cout << “Welcome to CorpLand!” << endl; }

Looping for Input char letterEntered = ‘A’; while (letterEntered != ‘Z’) { cout << “Enter a letter: “; cin >> letterEntered; }

Looping until Flag bool noMoreData(); bool done = false; … while (!done) { // do a bunch of stuff if (noMoreData() == true) done = true; } cout << “We’re all through – thank you!”;

Nested Loops int i , j ; for (i=0; i < 10; i++) { for (j=0; j < 10; j++) cout << i << j << “ “; } cout << endl;

The do-while Loop Syntax do statement; while (condition); { }

do-while Example char letterEntered; do { cout << “Enter a letter: “; cin >> letterEntered; } while (letterEntered != ‘Z’);

What Is This? while (stringEntered != “apple”) { cout << “Enter a red fruit: “; cin >> stringEntered; } while (stringEntered != “banana”) cout << “Enter a yellow fruit: “; while (stringEntered != “pomegranate”) cout << “Enter a random fruit: “;

What Is This? for (i = 0; i < 10 ; i++) cout << “Interesting, isn’t it?” << endl; while (answer != ‘D’) { cout << “Enter an answer: “; cin >> answer; }

What Is This? void kingsChair(int loopCounter) { int num; for(num = 0; num < loopCounter; num++) cout << “AAAAAAAAAAAAAAAAAAAAAAAAAA” << endl; }