Lecture 1 The Basics (Review of Familiar Topics).

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Operaciones y Variables
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Fundamental Programming: Fundamental Programming Introduction to C++
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Week 8: Decisions Bryan Burlingame 21 October 2015.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
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
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
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 C++ Programming
C Operators, Operands, Expressions & Statements
Algorithms computer as the tool process – algorithm
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Using C++ Arithmetic Operators and Control Structures
An Overview of C.
DATA TYPES There are four basic data types associated with variables:
Lecture 3 More on Flow Control, More on Functions,
Presentation transcript:

Lecture 1 The Basics (Review of Familiar Topics)

Building a C++ Program A C++ Program consists of Source files Header files Source Files Usually have a.cpp or.cp extension Compiled individually Header Files Usually have a.h extension (some use of.hpp) Contain declarations which are “included” by source files. Let’s take a quick look at how a C/C++ program is built “From 10,000 feet” (I’ll omit some details to keep it simple)

Building a C++ Program MAIN.CPPMAIN.OBJMAIN.EXE MAIN.CPP is a source file Contains source code you write. MAIN.OBJ is an object file Contains object code generated by compiling MAIN.CPP MAIN.EXE is an executable file Is a “double-clickable” application that may be run. It is created by the linker which takes object code and generates executables.

Building a C++ Program MAIN.CPP Programs may be made up of multiple source files When building the program these files are compiled independently. The compiler makes note of which symbols (variables, function names) are not defined in the same file. So, when MAIN.CPP is compiled, the compiler makes a note that EventLoop() is (apparently) defined elsewhere… int main() { // real code here EventLoop(); } EVENTS.CPP void EventLoop() { // real code here }

Building a C++ Program MAIN.OBJ The compiler creates two object files. The linker is then called to build an executable out of the object files. The linker will be told which object files should be used in creating the exe It’s the linker’s job to make sure symbols referenced from one file (but not defined in that file) are found in other object files. SYMBOLS DEFINED: main(); OTHER SYMBOLS REFERENCED: EventLoop(); EVENTS.OBJ SYMBOLS DEFINED: void EventLoop();

Building a C++ Program MAIN.OBJ When generating SAMPLE.EXE, the linker will notice that “EventLoop” is not defined in MAIN.OBJ. If it can’t find that symbol in an other object file, an error will result. Otherwise, if all symbols can be found, the executable will be generated. SYMBOLS DEFINED: main(); OTHER SYMBOLS REFERENCED: EventLoop(); SYMBOLS DEFINED: void EventLoop(); EVENTS.OBJ SAMPLE.EXE

A Simple C++ Program #include // header file void main() { cout << “Hello World!” << endl; } #include -- needed to access I/O streams (console) void main() -- main function-Entry point into your program {,} -- Scope delimiters cout -- the standard output identifier (console) << -- Special operator which takes contents to the right and sends them to the left endl -- special identifier which sends a newline

Demonstration #1 Let’s compile it!

Some Simple C++ Type Declarations int j; float interestRate; char aLetter; string userName; int -- integer type: range is implementation dependent usually 32-bits -- +/- 2,147,483, bits on older systems -- +/- 32,768 float -- floating point number char -- a single character string -- more than an array of characters (a class) we’ll look at these in more detail later...

How to Assign Values main() { int j = 0; int k = 1; float pi; pi = ; } Assignment at declaration time insert an equals sign followed by an initial value Assignment of previously declared variable start with the variable name, follow with equals sign, end with value to be assigned.

Arithmetic Expressions Can be used to calculate a value to be assigned What is wrong with the division expression? When assigning values to variables, the value is always coerced to the type of the variable it is getting assigned to. main() { int j = 0,k = 1,m = 2,n,p,q,r; float f; n = j + k;// Add j and k, place in n p = n * m; // Multiply n and m, store in p q = p / 4.0;// Divide p by 4, place in q r = q - 1; // Subtract 1 from q, place in r }

Arithmetic Expressions (cont) main() { int j = 0,k = 5; j = j + 1; k = k - 5; } The same variable may appear on both sides of an assignment operator on the right hand side of the assignment operator, the variable in question represents its value prior to the execution of this statement. on the left hand side of the assignment operator, the variable receives a new value which is the result of the evaluation on the right hand side. In our example above, j ends up being “1” and k ends up being “0”.

Arithmetic Expressions (shortcuts) main() { int j = 0,k = 5; j++; // really like j = j + 1; k -= 5; // really like k = k - 5; } When incrementing an integer variable by “1”, just append a ++ to the variable name. When decrementing by “1”, just append a “--” to the variable name. When performing any other operation on a variable and stuffing the value back into the same variable, use a shortcut (like +=, -=, *=)

Arithmetic Expressions (prefix vs. postfix) main() { int j = 0,k = 0,q,r; q = j++; // Postfix operation r = ++k; // Prefix operation } When the “++” appears after a variable it is said to be a “postfix operator”) the variable isn’t incremented until all other evaluations (and assignments) have taken place When the “++” appears before a variable it is said to be a “prefix operator”) the variable is incremented before any other evaluations take place. What will the values of q & r be in the example above?

Demonstration #2 Arithmetic Expressions, Shortcuts and Pre/Postfix Operators

Control Structures--if/else statements if (expression) statement1 else statement2 expression is any expression that can be evaluated as an integer a non zero value is taken as “true”, a 0 value is taken as “false” statement1 is a statement or group of statements executed if expression evaluates to a non-zero value statement2 is a statement or group of statements executed if expression evaluates to a zero value statement2 is needed only if the the optional else keyword is present

Control Structures--if/else statements if (x = 0) cout << “It’s zero” << endl; else cout << “No, it’s not zero!” << endl; WARNING!!!!! While the “if” statement above may look perfectly fine it contains a very common flaw. The assignment operator (=) is not used to test for equality. “x=0” is an expression which evaluates to “0” along with having the side effect of storing the value 0 in the variable “x”. As an expression which evaluates to “0” it will always cause the “else” branch to be executed.

Control Structures--if/else statements if (x == 0) cout << “It’s zero” << endl; else cout << “No, it’s not zero!” << endl; This is the correct way, use the equality operator (==) What are some of the other comparison operators? (a > b), true if “a” is greater than “b” (a < b), true if “a” is less than “b” (a >= b), true if “a” is greater than or equal to “b” (a <= b), true if “a” is less than or equal to “b” (a != b), true if “a” is not equal to “b”

Control Structures--compound expressions if ((x == 0) || (y > 1)) { cout << “x is zero OR” << endl; cout << “y is greater than 1” << endl; } An expression with the logical “or” (||) operator… Evaluates to “true” if an expression on either side evaluates to “true” An expression with the logical “and” (&&) operator… Evaluates to “true” if the expressions on both sides evaluate to “true” Note the use of curly braces ({,} ) above Used to group multiple statements to be executed if the “if” statement evaluates to “true”

Demonstration #3 If/else statements

Control Structures--loops while (expression) statement(s) A while loop will continue executing as long as expression evaluates to a non-zero (true) value. How do you print your name 10 times using a while loop? int x = 0; while (x < 10) { cout << “Ron DiNapoli” << endl; x++; } Why is it (x < 10) and not (x <= 10) ?

Control Structures--loops int x; while (true) { cin >> x; if (x == 0) break; cout << “You entered the number “ << x; } A while loop can be used to loop forever by having it test for an expression which will always evaluate to a non-zero value (true) A break statement can be used to break out of such a loop when the time comes Some think that this is bad programming style, but it is frequently used.

Control Structures--do…while loops int x; do { cin >> x; if (x == 0) break; cout << “You entered the number “ << x; } while(true); A do..while loop is very similar to a regular while loop. Terminating condition is specified at the end of the loop

Demonstration #4 loops