Flow of Control October 16, 2017.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
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.
CS150 Introduction to Computer Science 1
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
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.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 05 (Part III) Control Statements: Part II.
Control Structures Repetition or Iteration or Looping Part II.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Control Structures RepetitionorIterationorLooping Part I.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Sentinel Loops. while Syntax while(expression) statement.
Chapter 1.2 Introduction to C++ Programming
Review 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
REPETITION CONTROL STRUCTURE
Chapter 1.2 Introduction to C++ Programming
Chapter 5: Control Structures II (Repetition)
C++ Programming: CS150 For.
Chapter 2 Assignment and Interactive Input
Control Structures II (Repetition)
Introduction to C++ October 2, 2017.
Programming Fundamentals
Chapter 8 Repetition Statements
Conditinoal Constructs Review
For & do/while Loops.
Chapter 3 Even More Flow of Control 1
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to Programming
CS1100 Computational Engineering
Conditional Construct
Conditinoal Constructs Review
Chapter 3 Even More Flow of Control 1
Structured Program Development in C
CS150 Introduction to Computer Science 1
Looping III (do … while statement)
Chapter 5: Control Structures II (Repetition)
Fundamental Programming
Introduction to Programming
Repetition Statements (Loops) - 2
Introduction to Programming
Presentation transcript:

Flow of Control October 16, 2017

Constants A declared constant is a variable whose value cannot be changed by the program. A constant is declared using the modifier const: const int WEEKDAYS = 5; const double PI = 3.14159; const string INITIALS = "BSC"; Standard practice is to write the name of a constant in all caps.

Arithmetic Shortcuts Some shortcuts besides x++ and x--: x += 2; is the same as x = x + 2; x -= 3; is the same as x = x - 3; x *= 5; is the same as x = x * 5; x /= 4; is the same as x = x / 4; x %= 2; is the same as x = x % 2;

Switch Statements A switch statement is similar to a series of if-else statements, but in some cases is a simpler implementation. Every switch statement can be converted to an if-else statement, but not every if-else statement can be converted to a switch statement. A switch statement is declared with the syntax switch (variable), where variable is the name of a variable, followed by curly braces. Do not put a semicolon after switch (variable)! The different possibilities (branches) are denoted using case.

break Statements The break statement can also be used to exit a loop. For instance, a break statement can end a loop if improper input is detected. Consider a program that asks the user to enter 10 negative numbers. A break statement can be used to end the loop if the user enters zero or a positive number.

break Statements int number, sum = 0, count = 1; while(count <= 10) { cin >> number; if(number >= 0) cout << "ERROR! Zero or positive number entered.\n"; break; } The break; statement above terminates the loop if a nonnegative number is entered.

Sentinel Values One way to allow the user to end a loop is through the use of a sentinel value. This refers to a value added at the end of a list that is distinct from the other possible values in the list.

Sentinel Values int number, sum = 0; cout << "Please enter a list of nonnegative integers.\n" << "Place a negative integer after the list.\n"; cin >> number; while(number >= 0) { sum = sum + number; }