PIC 10A Flow control. Ernest Ryu UCLA Mathematics
|
|
- Audrey Sullivan
- 6 years ago
- Views:
Transcription
1 PIC 10A Flow control Ernest Ryu UCLA Mathematics
2 If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if (d1 <0) { cout << " Error. Negative input!" << endl ; 2
3 If-else statement An if-else statement conditionally executes one of the alternatives. # include < iostream > using namespace std ; int main () { int n; cin >> n; if (n>0) { cout << " n is positive." << endl ; else { cout << " n is not positive." << endl ; 3
4 If-else statement An if-else statement can have more than one alternatives. int main () { int n; cin >> n; if (n>0) { cout << " n is positive." << endl ; else if (n<0) { cout << " n is negative." << endl ; else { cout << " n is 0." << endl ; 4
5 Flow control The programs we had encountered had a linear sequence of instructions. However, real programs may bifurcate, repeat code, and take decisions. Control structures allow programs to follow a more complicated flow. 5
6 The type bool A Boolean (named after Prof. Boole) is a data type with two possible values: true or false. The type bool represents a Boolean value. bool is a fundamental type. (#include not needed.) A literal of type bool is written as true false No capitals. bool b = true ; cout << b << endl ; The output is 1. 6
7 Comparison operators The comparison/relational operators are >, >=, <, <=, ==, and!=. When used with integer types or floating-point types, they return a bool. They represent >,, <,, =, and. bool b = ( 454 < 324 ); cout << b << endl ; cout << ( 6.4!= 3.2 ) << endl ; The output is 0 and 1. 7
8 Comparison operators The comparison operator == is closer to = than the assignment operator =. Never use == (or!=) with floating-point numbers! double d1 = 3./15. ; double d2 =.3 / 1.5 ; cout << ( d1 == d2) << endl ; The output is 0. (Results may vary. Each CPU handles round-off errors slightly differently.) An operator does different things based on what type it is used with. On strings, comparison operators look at the lexicographical order. string s1 = " Alice "; string s2 = " Bob "; cout << ( s1 < s2) << endl ; The output is 1. 8
9 Comparison operators: warning Do not mix up = and ==. 9
10 Logical operators The Logical operators are &&,, and!. && = and = conjunction = = or = disjunction =! = not = negation = bool b1 = true ; bool b2 = false ; cout << ( b1 && b2) << endl ; cout << ( b1 b2) << endl ; cout <<! b1 << endl ; The output is 0, 1, and 0. 10
11 Logical operators: warning Use && and. & and are something else! You can t do int x = 5; cout << ( 4 <= x <= 10) << endl ; (Why?) Instead do int x = 5; cout << ( 4 <= x && x <= 10) << endl ; 11
12 while loop The while loop while (condition) { code to run loops through the code as long as the condition is true. (Running while the condition is true.) The code runs 0, 1, 2,..., or even infinitely many times. 12
13 while loop This code prints integers 0, 1,..., n-1. # include < iostream > using namespace std ; int main () { int n, i = 0; cin >> n; while (i < n) { cout << i << endl ; i += 1; The code in the loop executes n times. 13
14 while loop: example I put $100 in my bank, which gives 4% annual interest. How many years until it becomes $300? int main () { double annual_ interest, current_ money, goal ; int years = 0; cin >> annual_ interest ; cin >> current_ money ; cin >> goal ; while ( current_ money < goal ) { current_ money *= ( 1. + annual_ interest ); years += 1; cout << " You ll reach your goal after "; cout << years << " years " << endl ; 14
15 Infinite loop An infinite loop is a loop that never ends while ( true ) { cout << " This will never end." << endl ; Infinite loops (mostly) happen by accident. In the interest example, the loop never terminates if the interest is
16 Increment/decrement operator The increment/decrement operators are ++ and --. Actually, there are 4 such operators. When x is of type int, ++x pre-increment x++ post-increment --x pre-decrement x-- post-decrement ++x and x++ are similar to x += 1. (Increment the value of x by 1.) --x and x-- are similar to x -= 1. (Decrement the value of x by 1.) ++x and x++ (--x and x--) are slightly different. More on this later. 16
17 Increment/decrement operator int a = 10; while (a>0) { cout << a << endl ; a - -; Output is 10, 9,..., 1. 17
18 for loop The for loop for (intialization; condition; update) { code to run executes the initialization, loops through the code as long as the condition is true, and perform the update after each loop. It is similar to initialization while (condition) { code to run update 18
19 for loop for loops can be slightly cleaner than while loops # include < iostream > using namespace std ; int main () { int n; cin >> n; for ( int i=0; i<n; i ++) { cout << i << endl ; Output is 0, 1,..., n-1. 19
20 for loop You are running the loop for i equals 0 through n-1. for ( int i=0; i<n; i ++) {... 20
21 for loop: warning When you get the iteration index of a loop off by one, you commit an off-by-one error. If you wish to print 1, 2,..., n, the program # include < iostream > using namespace std ; int main () { int n; cin >> n; for ( int i=0; i<n; i ++) { cout << i << endl ; has an off-by-one error. The for loop should be for ( int i=1; i <=n; i ++) 21
22 do-while loop Similar to the while loop, but will run the loop at least once. # include < iostream > # include < string > using namespace std ; int main () { string s; do { cout << " Enter text : "; cin >> s; cout << " You entered : " << s << "\n"; while (s!= " goodbye "); 22
23 break The break statement exits a loop. string s; while ( true ) { cout << " Enter text : "; cin >> s; if (s == " goodbye ") { break ; cout << " You entered : " << s << "\n"; 23
24 continue The continue statement skips the remaining part of the loop and continues to the next iteration. for ( int i = 1; i <= 10; ++i) { if ( i==6 i==9 ) { continue ; cout << i << endl ; Sometimes, using continue and break makes the code cleaner. 24
25 Useful digression: Boolean algebra In Elementary algebra (the algebra you know) you learned things like a + b = b + a a (b + c) = a b + a c. Elementary algebra studies arithmetic on real numbers. Boolean algebra is the study of things like a or b = b or a a and (b or c) = (a and b) or (a and c). Boolean algebra studies logical operations on Booleans. 25
26 Useful digression: Boolean algebra The notation seems intimidating. However, it s not that hard if you think by example. a or b = b or a You must be smart or hardworking. = You must be hardworking or smart. a and (b or c) = (a and b) or (a and c) Your shirt must have long sleeves, and it must be red or green. = Your shirt must be a red long-sleeve or a green long-sleeve. 26
27 Useful digression: Boolean algebra Useful identities of Boolean algebra. Distributivity of and over or a and (b or c) = (a and b) or (a and c) Distributivity of or over and a or (b and c) = (a or b) and (a or c) 27
28 Useful digression: Boolean algebra De Morgan s law 1 not (a or b) = (not a) and (not b) This is a club for those who are smart or hard-working. He s not allowed because he s not smart and he lazy. De Morgan s law 2 not (a and b) = (not a) or (not b) To enter this establishment, you must have shoes and a shirt. If you barefoot or shirtless, you re not allowed. Again, this isn t that hard if you think by example. 28
29 Useful digression: Boolean algebra The truth table of the 3 logical operators is a b a && b a b!a false false false false true false true false true true true false false true false true true true true false 29
30 Useful digression: Boolean algebra The Venn diagram for a && (b c) b a c The shaded area also represents (a && b) (a && c). 30
31 Short-circuit evaluation In C++, && and implement short-circuit evaluation; the second bool isn t evaluated if the first bool determines the output. int numer = 4, denom = 0; if ( denom!= 0 && ( numer / denom < 7) ) { cout << " Short - circuit eval example " << endl ; This is also called minimal evaluation and McCarthy evaluation. Short-circuit evaluation is efficient and convenient: the program doesn t spend time evaluating the second expression, and we can take advantage of this feature to make code concise. 31
32 Why does this work? # include < iostream > # include < string > using namespace std ; int main () { string s; while ( true ) { cout << " Enter text : "; cin >> s; if (s == " goodbye ") break ; cout << " You entered : " << s << "\n"; 32
33 Why does this work? # include < iostream > using namespace std ; int main () { int n; cin >> n; for ( int i=0; i<n; i ++) for ( int j=0; j<n; j ++) cout << i << ", " << j << endl ; The 2 code snippets are in compliance with the rules for constructing if statements and for loops. What are these precise rules? 33
34 Why bother with precise syntax? We ll learn some precise syntax. Here s why. Generally, you don t have to know the precise syntax. Even with a vague understanding of the rules, you are capable of writing control structures. In real life, you learn the rules more precisely over time and achieve a more deeper understanding. Nothing wrong about this. There is pedagogical value to learning the precise rules upfront, especially since this is a first course in programming. Programs follow rigid instructions. When you program, the compiler, a program itself, converts your program into an executable. So the syntax of a programming language is a collection of precise and rigid set of rules. 34
35 Statements A C++ program comprises a series of statements. There are several types: declaration statements string s; int i=0; jump statements break; continue; expression statements compound statements selection statements if and if-else statement iteration statements for, while, and do-while loops 35
36 Expression statements Roughly speaking, an expression is a line of code that evaluates to something. They are things like i = 4 + j i cout << "UCLA" << endl b1 && b2 (the empty expression) Terminate an expression with ; to get an expression statement. 36
37 Null statements The null statement is the empty expression terminated with ; So the following 4 statements string s = " UCLA "; cout << s << endl ;;; are legal. The code will compile. Needless to say, 2 of the 3 expression statements are unnecessary. 37
38 Useful digression: whitespace in C++ Whitespace is the set of characters like space, tab, and newline. In C++, whitespace mostly mean nothing to the computer. So you could write your program like # include < iostream > using namespace std ; int main (){ int i;cin >>i;cout <<" Good morning. The input However, do use whitespace to make your code human-readable. 38
39 Compound statements A compound statement compounds multiple statements into one with curly braces. { statement statement... statement 39
40 if and if-else statement The precise syntax for if and if-else statement are if (expression) statement if (expression) statement else statement So if ( true ) { cout << " output 1" << endl ; cout << " output 2" << endl ; is legal since the block of code within { and compose a single compound statement. 40
41 if and if-else statement When you only have a single expression statement, { and are unnecessary. if ( true ) cout << " output 1" << endl ; else cout << " output 1" << endl ; is legal. 41
42 if and if-else statement The 2 statements if (i < n) { cout << i << endl ; ; are legal and will compile. However, the extra null statement suggests that the author doesn t understand the language (in particular about expressions and statements) terribly well. An if statement doesn t need a semicolon at the end! 42
43 while loop The precise syntax for a while loop is while (expression) statement So while ( true ) cout << " This will never end." << endl ; is a single legal (iteration) statement. 43
44 for loop The precise syntax for a for loop is for (statement expression; expression) statement So for (; true ; ) cout << " This will never end." << endl ; is a single legal (iteration) statement. 44
45 for loop The following (legal) program contains 3 statements # include < iostream > using namespace std ; int main () { int n; cin >> n; for ( int i=0; i<n; i ++) for ( int j=0; j<n; j ++) cout << i << ", " << j << endl ; What are the 3 statements? Why is the for loop legal? 45
46 do-while loop The precise syntax for a do-while loop is do statement while (expression); Unlike the other control structures, the do-while loop does require a semicolon at the end! string s; do { cout << " Enter text : "; cin >> s; cout << " You entered : " << s << "\n"; while (s!= " goodbye "); Without ; the code wouldn t compile. 46
47 The following code doesn t compile int main () { int i = 5; cout << i << endl ; int i = 10; cout << i << endl ; Why does this work? because you can t declare i a variable twice. However, this works. for ( int i=0; i<5; i ++) cout << i << endl ; for ( int i=5; i>0; i - -) cout << i << endl ; Why? 47
48 Variable scope The scope of a variable is the part of the code where the variable is available. Throughout a program, variables are created and destroyed. A destroyed variable is inaccessible. A variable (not yet destroyed) may be inaccessible for other reasons. More on this later. 48
49 Variable scope: compound statements Variables declared within a compound statement go out of scope at the end of the compount statement. int i = 0; { i = 2; int j = 5; cout << i << ", " << j << endl ; cout << i << endl ; cout << j << endl ; This won t compile. By the time the program reaches the last line, j went out of scope. 49
50 Variable scope: for loop A variable declared within the initialization statement of a for loop is in scope throughout the loop, but not after. for ( int i=0; i <5; i ++) { cout << " For loop is looping " << endl ; cout << i << endl ; cout << i << endl ; This doesn t compile: i is destroyed once the for loop is done. 50
51 Variable scope: for loop In contrast, the following does compile. int i; for (i=0; i <5; i ++) { cout << " For loop is looping " << endl ; cout << i << endl ; cout << i << endl ; 51
52 Variable scope It is often good to limit the scope of a variable. More on this later. Usually, you don t want to use generic variable names like i or str. You may later accidentally use it when you mean to create a different variable. It s fine to use the variable name i it within a for loop, as it goes out of scope after the loop. 52
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
More informationDELHI 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
More informationMaciej Sobieraj. Lecture 1
Maciej Sobieraj Lecture 1 Outline 1. Introduction to computer programming 2. Advanced flow control and data aggregates Your first program First we need to define our expectations for the program. They
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationPIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics
PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.
More informationInput And Output of C++
Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationThese are notes for the third lecture; if statements and loops.
These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern
More informationHow to approach a computational problem
How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to
More information5. Control Statements
5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements
More informationChapter 2 Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion
More informationC++ 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
More informationThe C++ Language. Arizona State University 1
The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationObject-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering
Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)
More informationChapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.
Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationComputer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite
More information8. Control statements
8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon
More informationYour first C++ program
Your first C++ program #include using namespace std; int main () cout
More informationChapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights
More informationChapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and
More informationNumerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2
Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.
More informationChapter 2. C++ Basics
Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables
More informationDiscussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;
More informationComputer 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
More informationC++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh
C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,
More informationIntroduction. 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
More informationThe following expression causes a divide by zero error:
Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct
More informationComputer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Decisions Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To be able to implement decisions using if statements To learn
More informationPIC 10A Objects/Classes
PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous
More informationCSCE 121 ENGR 112 List of Topics for Exam 1
List of Topics for Exam 1 If statements o How is an if statement constructed? o Does every if need an else? Looping o While loop! What does a while loop look like?! How do you ensure you will not have
More informationMultiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationIncrement 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
More information1 Unit 8 'for' Loops
1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More information2 nd Week Lecture Notes
2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous
More informationChapter 4. Computation. Bjarne Stroustrup.
Chapter 4 Computation Bjarne Stroustrup www.stroustrup.com/programming Abstract Today, I ll present the basics of computation. In particular, we ll discuss expressions, how to iterate over a series of
More informationRelational Operators and if. Class 10
Relational Operators and if Class 10 Data Type a data type consists of two things: Data Type a data type consists of two things: a set of values Data Type a data type consists of two things: a set of values
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationIntroduction to Programming using C++
Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know
More informationChapter 4 Introduction to Control Statements
Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?
More informationWhy 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
More informationCGS 3066: Spring 2015 JavaScript Reference
CGS 3066: Spring 2015 JavaScript Reference Can also be used as a study guide. Only covers topics discussed in class. 1 Introduction JavaScript is a scripting language produced by Netscape for use within
More informationC++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5
C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................
More informationChapter 2: Introduction to C++
Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++
More informationC++ 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
More informationChapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationChapter 4 - Notes Control Structures I (Selection)
Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by
More informationC++ 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,
More informationStarting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key
Starting Out with C++ Early Objects 9th Edition Gaddis TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/starting-c-early-objects-9thedition-gaddis-test-bank/ Starting
More informationChapter 2 C++ Fundamentals
Chapter 2 C++ Fundamentals 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Reuse existing code in your programs with #include Obtain input data from the user
More informationScientific Computing
Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course
More informationProgramming 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
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationWeek 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
More informationChapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?
Chapter 3 - Simple JavaScript - Programming Basics Lesson 1 - JavaScript: What is it and what does it look like? PP presentation JavaScript.ppt. Lab 3.1. Lesson 2 - JavaScript Comments, document.write(),
More informationBoolean Algebra Boolean Algebra
What is the result and type of the following expressions? Int x=2, y=15; float u=2.0, v=15.0; -x x+y x-y x*v y / x x/y y%x x%y u*v u/v v/u u%v x * u (x+y)*u u / (x-x) x++ u++ u = --x u = x -- u *= ++x
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationTHE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)
THE INTEGER DATA TYPES STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationChapter 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
More informationUnit 7. 'while' Loops
1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More informationLECTURE 02 INTRODUCTION TO C++
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION
More informationIntroduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1
Introduction to C++ Lecture Set 2 Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1 More Arithmetic Operators More Arithmetic Operators In the first session the basic arithmetic operators were introduced.
More informationProgramming for Electrical and Computer Engineers. Loops
Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly
More informationIntroduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2
Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB Administrative This class is currently FULL and
More informationProgramming Language. Functions. Eng. Anis Nazer First Semester
Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities
More informationChapter 4 Computation
Chapter 4 Computation Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/~hkaiser/spring_2011/csc1253.html Slides adapted from: Bjarne Stroustrup, Programming Principles and Practice using C++ Abstract
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationCPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)
Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow
More informationBy the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program
1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize
More informationText Input and Conditionals
Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()
More informationCS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12
CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project
More informationDECISION STRUCTURES: USING IF STATEMENTS IN JAVA
DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you
More informationChapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.
Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop
More informationCSCI 1061U Programming Workshop 2. C++ Basics
CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output
More informationUniversity of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.
University of Technology Laser & Optoelectronics Engineering Department C++ Lab. Fifth week Control Structures A program is usually not limited to a linear sequence of instructions. During its process
More informationMultiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More information2. First Program Stuff
CSE 232 First Midterm, Overview: 1. Getting Started 1. we got started 2. First Program Stuff 1. Compiler vs. Intepreter a. do you know all the steps to create an executable? 2. Variables are declared a.
More informationCSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York
CSc 10200 Introduc/on to Compu/ng Lecture 8 Edgardo Molina Fall 2011 City College of New York 18 The Null Statement Null statement Semicolon with nothing preceding it ; Do-nothing statement required for
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja
More informationCS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started
CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read
More informationYOLOP Language Reference Manual
YOLOP Language Reference Manual Sasha McIntosh, Jonathan Liu & Lisa Li sam2270, jl3516 and ll2768 1. Introduction YOLOP (Your Octothorpean Language for Optical Processing) is an image manipulation language
More informationFORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),
More information5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.
More information4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme
Chapter 4: 4.1 Making Decisions Relational Operators Simple Program Scheme Simple Program Scheme So far our programs follow a simple scheme Gather input from the user Perform one or more calculations Display
More informationIntro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?
Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set
More informationdownload instant at Introduction to C++
Introduction to C++ 2 Programming, Input/Output and Operators What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare High thoughts must have high language.
More informationProgramming, numerics and optimization
Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March
More information6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.
6 Functions TOPICS 6.1 Focus on Software Engineering: Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5 Passing Data by Value 6.6 Focus
More informationChapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.
Chapter 2 Flow of Control Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else
More informationint n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;
Debugging Some have said that any monkey can write a program the hard part is debugging it. While this is somewhat oversimplifying the difficult process of writing a program, it is sometimes more time
More information7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING
KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve
More informationCompiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3
Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB Compiling Programs in C++ Input and Output Streams Simple Flow
More information