Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Chapter 2 part #4 Operator
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CSC 107 – Programming For Science. The Week’s Goal.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Mathematical Calculations in Java Mrs. C. Furman.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Operations Chapter 4 & Section Expressions As we've seen, in a C++ program, any finite sequence of objects and operations that combine to produce.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Chapter Topics The Basics of a C++ Program Data Types
Expressions.
Programming Fundamentals
Computing Fundamentals
Basic Elements of C++.
Multiple variables can be created in one declaration
Basic Elements of C++ Chapter 2.
Operators and Expressions
Conversions of the type of the value of an expression
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

Programming Fundamentals

Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout

Today’s Lecture Expressions Boolean Data Type Data Type Conversion Operators

Expressions Any arrangement of variables, constants, and operators that specifies a computation is called an expression. For example, alpha+12 and (alpha-37)*beta/2 are expressions. When the computations specified in the expression are performed, the result is usually a value. Thus if alpha is 7,the first expression shown has the value 19.

Precedence () ^ *, / +, - Example expression: 2 * 4 / 4 + (6 + 6 / 3) ans???

Example containing Expression

OUTPUT

The using Directive A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. The directive says that all the program statements that follow are within the std namespace.

The using Directive Various program components such as cout are declared within this namespace. We can use it the other way, For example like this without using directive

Namespace Example

Type bool Variables of type bool can have only two possible values: true and false Type bool is most commonly used to hold the results of comparisons. For example: Is var1 less than var2? If so, a bool value is given the value true; If not, it’s given the value false.

Example

Variable Type Summary

Unsigned Data Types

Type Conversion

Automatic Conversions

When two operands of different types are encountered in the same expression, the lower-type variable is converted to the type of the higher-type variable.

Automatic Conversions In our example, the int value of count is converted to type float and stored in a temporary variable before being multiplied by the float variable avgWeight. The result (still of type float) is then converted to double so that it can be assigned to the double variable totalWeight.

Process of coversion 4 bytes These conversions take place invisibly

Casts In C++ Cast applies to data conversions specified by the programmer, as opposed to the automatic data conversions. Casts are also called type casts. What are casts for?

Casts Sometimes a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically or without complaining. Use the following casting operator: static_cast (expression)

Example

The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right justified within the field.

Example

OUTPUT

Example code

OUTPUT

Field width and setw Name:MadihaLiaqat Reg#:09-SE-99 Name:MadihaLiaqt Reg#:09-SE-99 Without setw manipulator

OUTPUT ??

The Remainder Operator This operator (also called the modulus operator) finds the remainder when one number is divided by another.

Arithmetic Assignment Operators

Increment Operators The ++ operator increments (adds 1 to) its argument. Normal way: Using arithmetic assignment operator Using Increment operator

Prefix and Postfix the increment operator can be used in two ways: Prefix meaning that the operator precedes the variable Postfix meaning that the operator follows the variable Example See next slide

The Decrement (--) Operator The decrement operator, --, behaves very much like the increment operator, except that it subtracts 1 from its operand. It too can be used in both prefix and postfix forms. Example count-- --count

OUTPUT ????

Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as char, int, and float or they can be user- defined classes. The comparison involves such relationships as equal to, less than, and greater than. The result of the comparison is true or false; for example, either two values are equal (true), or they’re not (false).

Complete list of C++ relational operators

Example

OUTPUT

Example Expressions

Quiz

Assuming var1 starts with the value 20, what will the following code fragment print out? cout << var1--; cout << ++var1;

Questions????