# 2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

Size: px
Start display at page:

Download "2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator"

Transcription

1 2.11 Assignment Operators 1 Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Examples of other assignment operators include: d -= (d = d - ) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9) 2.11 Assignment Operators 2 A ssig n m e n t o p e ra to r Sa m p le e xp re ssio n Exp la n a tio n Assume: int c = 3, d = 5, e =, f = 6, g = 12; A ssig n s += c += 7 c = c to c -= d -= d = d - 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g Fig A rith m e t ic a ssig n m e n t o p e ra t o rs. 1

2 2.12 Increment and Decrement Operators Increment operator (++) can be used instead of c += 1 Decrement operator (--) can be used instead of c -= Increment and Decrement Operators Preincrement When the operator is used before the variable (++c or c) Variable is changed, then the expression it is in is evaluated. Postincrement When the operator is used after the variable (c++ or c--) Expression the variable is in executes, then the variable is changed If c = 5, then cout << ++c; prints out 6 (c is changed before cout is executed) cout << c++; prints out 5 (cout is executed before the increment. c now has the value of 6) 2

3 2.12 Increment and Decrement Operators 5 When not in an expression preincrementing and postincrementing have the same effect. For example, ++c; cout << c; and c++; cout << c; have the same effect Increment and Decrement Operators 6 O p e ra t o r C a lle d Sa m p le e xp re ssio n Exp la n a t io n ++ preincrem ent ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ postincrem ent a++ U se the current value of a in the expression in which a resides, then increment a by predecrem ent --b D ecrement b by 1, then use the new value of b in the expression in which b resides. -- postdecrement b-- U se the current value of b in the expression in which b resides, then decrement b by 1. Fig The inc re m e n t a nd d e c re m e n t o p e ra t o rs. 3

4 1 // Fig. 2.1: fig02_1.cpp 2 // Preincrementing and postincrementing 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int c; c = 5; 13 cout << c << endl; // print 5 1 cout << c++ << endl; // print 5 then postincrement 15 cout << c << endl << endl; // print c = 5; 18 cout << c << endl; // print 5 19 cout << ++c << endl; // preincrement then print 6 20 cout << c << endl; // print return 0; // successful termination 23 } Outline 1. Initialize variables 2. Postincrement variable 3. Preincrement variable Program Output Increment and Decrement Operators 8 O p e ra t o rs A sso c ia tivity Typ e () left to right parentheses static_cast<type>() left to right unary (postfix) right to left unary (prefix) * / % left to right multiplicative + - left to right additive << >> left to right insertion/extraction < <= > >= left to right relational ==!= left to right equality?: right to left conditional = += -= *= /= %= right to left assignm ent, left to right comma Fig Pre c e d e nc e o f th e o p e ra to rs e nc o un te re d so fa r in th e te xt.

5 2.13 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The condition that tests for the final value of the control variable (i.e., whether looping should continue). The increment (or decrement) by which the control variable is modified each time through the loop. Example: int counter =1; //initialization while (counter <= 10){ //repetition condition cout << counter << endl; ++counter; //increment } Essentials of Counter-Controlled Repetition The declaration int counter = 1; Names counter Declares counter to be an integer Reserves space for counter in memory Sets counter to an initial value of

6 1 // Fig. 2.16: fig02_16.cpp 2 // Counter-controlled repetition 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition 13 cout << counter << endl; 1 ++counter; // increment 15 } return 0; 18 } Outline 1.Initialize counter 2. Repetition condition 3. Increment counter Program Output 2.1 The for Repetition Structure 12 The general format when using for loops is for ( initialization; LoopContinuationTest; increment ) statement Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; Prints the integers from one to ten No semicolon after last statement 6

7 2.1 The for Repetition Structure 13 for loops can usually be rewritten as while loops: initialization; while ( loopcontinuationtest ){ statement increment; } Initialization and increment as comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl; 1 // Fig. 2.17: fig02_17.cpp 2 // Counter-controlled repetition with the for structure 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 // Initialization, repetition condition, and incrementing 11 // are all included in the for structure header for ( int counter = 1; counter <= 10; counter++ ) 1 cout << counter << endl; return 0; 17 } Outline 1.Initialization 2. Repetition condition 3. Incrementing counter Program Output 7

8 2.1 The for Repetition Structure 15 for keyword Control variable name Final value of control variable for which the condition is true for ( var counter = 1; counter <= 7; ++counter ) Initial value of control variable Increment of control variable Loop-continuation condition Fig Components of a typical for header. 2.1 The for Repetition Structure 16 Establish initial value of control variable counter = 1 Determine if final value of control variable has been reached counter <= 10 false true cout << counter << endl; Body of loop (this may be many statements) counter++ Increment the control variable Fig Flowcharting a typical for repetition structure. 8

9 2.15 Examples Using the for Structure 17 Examples: Vary control variable from 1 to 100 in increments of 1 for (int i = 1, i <= 100; i++) Vary control variable from 100 to 1 in decrements of -1 for (int i = 100, i >= 1; i--) Vary control variable from 7 to 77 in steps of 7 for (int i = 7, i <= 77; i += 7) 1 // Fig. 2.20: fig02_20.cpp 2 // Summation with for 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int sum = 0; for ( int number = 2; number <= 100; number += 2 ) 13 sum += number; 1 15 cout << "Sum is " << sum << endl; return 0; 18 } Outline 1. Summation with for loop 18 Sum is 2550 Program Output 9

10 1 // Fig. 2.21: fig02_21.cpp 2 // Calculating compound interest 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 9 #include <iomanip> using std::setw; 12 using std::setiosflags; 13 using std::setprecision; 1 15 #include <cmath> int main() 18 { 19 double amount, // amount on deposit 20 principal = , // starting principal 21 rate =.05; // interest rate cout << "Year" << setw( 21 ) 2 << "Amount on deposit" << endl; // set the floating-point number format 27 cout << setiosflags( ios::fixed ios::showpoint ) 28 << setprecision( 2 ); for ( int year = 1; year <= 10; year++ ) { 31 amount = principal * pow( rate, year ); 32 cout << setw( ) << year << setw( 21 ) << amount << endl; 33 } 3 35 return 0; 36 } Outline 1. Using setprecision to specify floatingpoint precision 2. Calculating compound interest using a for loop The switch Multiple-Selection Structure switch Useful when variable or expression is tested for multiple values Consists of a series of case labels and an optional default case 20 10

12 Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Outline Program Output The switch Multiple-Selection Structure 2 true case a case a action(s) break false true case b case b action(s) break false... true case z case z action(s) break false default action(s) Fig The switch multiple-selection structure with breaks. 12

13 2.17 The do/while Repetition Structure 25 The do/while repetition structure is similar to the while structure, Condition for repetition tested after the body of the loop is executed Format: do { statement } while ( condition ); action(s) Example (letting counter = 1): do { true cout << counter << " "; condition } while (++counter <= 10); false This prints the integers from 1 to 10 All actions are performed at least once. 1 // Fig. 2.2: fig02_2.cpp 2 // Using the do/while repetition structure 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int counter = 1; do { 13 cout << counter << " "; 1 } while ( ++counter <= 10 ); cout << endl; return 0; 19 } Outline 1. Initialize counter 2. Use the do/while loop 3. Repetition condition 26 13

14 2.17 The do/while Repetition Structure 27 action(s) condition true false Fig Flowcharting the do/while repetition structure The break and continue Statements 28 break Causes immediate exit from a while, for, do/while or switch structure Program execution continues with the first statement after the structure Common uses of the break statement: Escape early from a loop Skip the remainder of a switch structure 1

15 2.18 The break and continue Statements 29 continue Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop In while and do/while, the loop-continuation test is evaluated immediately after the continue statement is executed In the for structure, the increment expression is executed, then the loop-continuation test is evaluated 2.27 code 30 15

16 1 // Fig. 2.27: fig02_07.cpp 2 // Using the continue statement in a for structure 3 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 for ( int x = 1; x <= 10; x++ ) { if ( x == 5 ) 13 continue; // skip remaining code in loop 1 // only if x is cout << x << " "; 17 } cout << "\nused continue to skip printing the value 5" 20 << endl; 21 return 0; 22 } Outline 1. continue statement Used continue to skip printing the value Logical Operators 32 && (logical AND) Returns true if both conditions are true (logical OR) Returns true if either of its conditions are true! (logical NOT, logical negation) Reverses the truth/falsity of its condition Returns true when its condition is false Is a unary operator, only takes one condition 16

17 2.19 Logical Operators 33 expression1 expression2 expression1 && expression2 false false false false true false true false false true true true Fig Truth table for the && (logical AND) operator Logical Operators 3 expression1 expression2 expression1 expression2 false false false false true true true false true true true true Fig Truth table for the (logical OR) operator. 17

18 2.19 Logical Operators 35 exp ression!exp ression false true true false Fig Truth tab le fo r ope ra tor! (lo gic a l ne ga tion) Logical Operators 36 Operators Associativity Type () left to right parentheses static_cast<type>() left to right unary (postfix) right to left unary (prefix) * / % left to right multiplicative + - left to right additive << >> left to right insertion/extraction < <= > >= left to right relational ==!= left to right equality && left to right logical AND left to right logical OR?: right to left conditional = += -= *= /= %= right to left assignment, left to right comma Fig Op era tor p rec ed enc e and a ssoc ia tivity. 18

19 2.20 Confusing Equality (==) and Assignment (=) Operators These errors are damaging because they do not ordinarily cause syntax errors. Recall that any expression that produces a value can be used in control structures. Nonzero values are true, and zero values are false Example: if ( paycode == ) cout << "You get a bonus!" << endl; Checks the paycode, and if it is then a bonus is awarded If == was replaced with = if ( paycode = ) cout << "You get a bonus!" << endl; Sets paycode to is nonzero, so the expression is true and a bonus is awarded, regardless of paycode. 37 Lvalues 2.20 Confusing Equality (==) and Assignment (=) Operators Expressions that can appear on the left side of an equation Their values can be changed Variable names are a common example (as in x = ;) Rvalues Expressions that can only appear on the right side of an equation Constants, such as numbers (i.e. you cannot write = x;) Lvalues can be used as rvalues, but not vice versa 38 19

20 2.21 Structured-Programming Summary 39 Structured programming Programs are easier to understand, test, debug and, modify. Rules for structured programming Only single-entry/single-exit control structures are used Rules: 1) Begin with the simplest flowchart. 2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence. 3) Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, switch, while, do/while or for). ) Rules 2 and 3 can be applied in any order and multiple times Structured-Programming Summary 0 Rules for Form ing Struc tured Program s 1) Begin with the simplest flowchart (Fig. 2.3). 2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence. 3) Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, switch, while, do/while or for). ) Rules 2 and 3 can be applied as often as you like and in any order. Fig Rules for fo rm ing struc tured p rog ra ms. 20

21 2.21 Structured-Programming Summary 1 Fig. 2.3 The simplest flowchart Structured-Programming Summary 2 Rule 2 Rule 2 Rule 2... Fig Repeatedly applying rule 2 of Fig to the simplest flowchart. 21

22 2.21 Structured-Programming Summary 3 Rule 3 Rule 3 Rule 3 Fig Applying rule 3 of Fig to the simplest flowchart Structured-Programming Summary All programs can be broken down into Sequence Selection if, if/else, or switch Any selection can be rewritten as an if statement Repetition while, do/while or for Any repetition structure can be rewritten as a while statement 22

23 2.21 Structured-Programming Summary 5 Stacked building blocks Nested building blocks Overlapping building blocks (Illegal in structured programs) Fig Stacked, nested and overlapped building blocks Structured-Programming Summary 6 Fig An unstructured flowchart. 23

### Chapter 2 - Control Structures

Chapter 2 - Control Structures 1 2.11 Assignment Operators 2.12 Increment and Decrement Operators 2.13 Essentials of Counter-Controlled Repetition 2.1 for Repetition Structure 2.15 Examples Using the for

### Chapter 4 C Program Control

1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition

### Chapter 2 - Control Structures

Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

### Chapter 2 - Control Structures

Chapter 2 - Control Structures 1 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure 2.8 Formulating

### Chapter 4 C Program Control

Chapter C Program Control 1 Introduction 2 he Essentials of Repetition 3 Counter-Controlled Repetition he for Repetition Statement 5 he for Statement: Notes and Observations 6 Examples Using the for Statement

### C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Program Control Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline The for repetition statement switch multiple selection statement break

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad Outline 1. The if Selection Structure 2. The if/else Selection Structure 3. The switch Multiple-Selection Structure 1. The if Selection

1 5 Control Statements: Part 2 2 Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William Shakespeare The used key is

1 5 Control Statements: Part 2 2 Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William Shakespeare The used key is

### Introduction to C Programming

1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

### C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

### In this chapter you will learn:

1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2

### C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2013 Fall Outline 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.5

### Structured Program Development in C

1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed

### Control Structures (Deitel chapter 4,5)

Control Structures (Deitel chapter 4,5) 1 2 Plan Control Structures ifsingle-selection Statement if else Selection Statement while Repetition Statement for Repetition Statement do while Repetition Statement

### IS 0020 Program Design and Software Tools

1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Spring 2005 Lecture 1 Jan 6, 2005 Course Information 2 Lecture: James B D Joshi Tuesdays/Thursdays: 1:00-2:15 PM Office Hours:

### Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

### Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

### Fundamentals of Programming Session 9

Fundamentals of Programming Session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

### بسم اهلل الرمحن الرحيم

بسم اهلل الرمحن الرحيم Fundamentals of Programming C Session # 10 By: Saeed Haratian Fall 2015 Outlines Examples Using the for Statement switch Multiple-Selection Statement do while Repetition Statement

### Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division)

### Chapter 2 - Control Structures

Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

### C++ PROGRAMMING SKILLS Part 2 Programming Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures If structure While structure Do While structure Comments, Increment & Decrement operators For statement Break & Continue statements Switch structure

### Introduction to Programming

Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

### Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure

### Chapter 3 Structured Program Development

1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

### Programming for Engineers Iteration

Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

### Why Is Repetition Needed?

Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

### C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures

### LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

### C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,

CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Arrays 3. Declaring Arrays 4. Examples Using Arrays 5. Multidimensional Arrays 6. Multidimensional Arrays Examples 7. Examples Using

Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

### Control Statements. Musa M. Ameen Computer Engineering Dept.

2 Control Statements Musa M. Ameen Computer Engineering Dept. 1 OBJECTIVES In this chapter you will learn: To use basic problem-solving techniques. To develop algorithms through the process of topdown,

### Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

1 2 Introduction to C++ Programming 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the

### Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,

### BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

1 7 JavaScript: Control Statements I 2 Let s all move one place on. Lewis Carroll The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert

### c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

### o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

### Chapter 5 Control Structures: Part 2

Chapter 5 Control Structures: Part 2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Name of control variable (loop counter) Initial value of control variable Increment/decrement

### Chapter 3 - Functions

Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

### IS 0020 Program Design and Software Tools

1 Course Information 2 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 1 May 10, 2004 Lecture: James B D Joshi Mondays: 6:00-8.50 PM One (two) 15 (10) minutes break(s)

### Structured Program Development

Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

### 5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

### Chapter 3 Structured Program Development in C Part II

Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 3.7 The while Iteration Statement An iteration statement (also called

### Structured Programming. Dr. Mohamed Khedr Lecture 9

Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop

### Looping. Arizona State University 1

Looping CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 5 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### Data types, variables, constants

Data types, variables, constants Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision

### Introduction to Programming

Introduction to Programming session 5 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

### Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

### Functions and Recursion

Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

### Fundamentals of Programming Session 7

Fundamentals of Programming Session 7 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

### Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

### Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction to C++ Programming Adhi Harmoko S, M.Komp Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages Machine languages Strings of numbers giving machine

### Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

### Chapter 10 JavaScript/JScript: Control Structures II 289

IW3HTP_10.fm Page 289 Thursday, April 13, 2000 12:32 PM Chapter 10 JavaScript/JScript: Control Structures II 289 10 JavaScript/JScript: Control Structures II 1

### Chapter 3 - Functions

Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Progra m Components in C++ 3.3 Ma th Libra ry Func tions 3.4 Func tions 3.5 Func tion De finitions 3.6 Func tion Prototypes 3.7 He a de r File s 3.8

### Chapter 5: Control Structures II (Repetition)

Chapter 5: Control Structures II (Repetition) 1 Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and

### Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

### Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

### Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

COMPUTER PROGRAMMING SKILLS (4800153-3) CHAPTER 5: REPETITION STRUCTURE Second Term (1437-1438) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah Table of Contents Objectives

### Chapter 9 - JavaScript: Control Structures II

Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled Repetition 9.3 for Repetition Structure 9. Examples Using the for Structure 9.5 switch Multiple-Selection

### Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

### REPETITION CONTROL STRUCTURE LOGO

CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

### Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Islamic University of Gaza Computer Engineering Dept. C++ Programming For Industrial And Electrical Engineering By Instructor: Ruba A. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for

### C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

### Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

ELEC 330 1 Structured Programming Control Structures ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Algorithm Development Conditional Expressions Selection Statements Loops 206_C3

### STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

### Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

Control Structures Flow of control Execution sequence of program statements Repetition (Loop) Structure Control structure used to repeat a sequence of instructions in a loop The simplest loop structure

### Lecture 4 Tao Wang 1

Lecture 4 Tao Wang 1 Objectives In this chapter, you will learn about: Assignment operations Formatting numbers for program output Using mathematical library functions Symbolic constants Common programming

C How to Program, 6/e Before writing a program to solve a particular problem, it s essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. The

### Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable

### Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

### Chapter 4: Making Decisions

Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to

### Increment and the While. Class 15

Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

### CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

### Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

EEE-117 COMPUTER PROGRAMMING Control Structures Conditional Statements Today s s Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical

### Chapter 4: Making Decisions

Chapter 4: Making Decisions CSE 142 - Computer Programming I 1 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >=

### 2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

### 5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

### Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to

### There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

### DELHI PUBLIC SCHOOL TAPI

Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

### Computer Programming : C++

The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data

### CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout

### Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Control Structures Intro. Sequential execution Statements are normally executed one

### Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

### Introduction. C provides two styles of flow control:

Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

Pointers and Strings 1 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Selection Sort Using Pass-by-Reference 2

### Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

### Information Science 1

Topics covered Information Science 1 Fundamental Programming Constructs (1) Week 11 Terms and concepts from Week 10 Flow of control and conditional statements Selection structures if statement switch statement

### Information Science 1

Information Science 1 Fundamental Programming Constructs (1) Week 11 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 10 l Flow of control