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.

Slides:



Advertisements
Similar presentations
Conditionals – if - else Dr. Sajib Datta
Advertisements

If Statements & Relational Operators Programming.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
Lecture 1 The Basics (Review of Familiar Topics).
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Basic Elements of C++ Chapter 2.
Operators and Expressions
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.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
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)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
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++
C++ Programming: Basic Elements of C++.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
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++ 16 September 2008.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Operators.
LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.
1 CS161 Introduction to Computer Science Topic #6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
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.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Arithmetic Instructions. Integer and Float Conversions.
LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.
Basic Elements Skill Area 313 Part B. Lecture Overview Basic Input/Output Statements Whitespaces Expression Operators IF Statements Logical Operators.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Computing and Statistical Data Analysis Lecture 2
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Operators and Expressions
Introduction to C++ Programming
Chapter-3 Operators.
Summary Two basic concepts: variables and assignments Basic types:
Introduction to Programming – 4 Operators
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Using C++ Arithmetic Operators and Control Structures
OPERATORS in C Programming
OPERATORS in C Programming
Presentation transcript:

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 2008

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C Expressions, Statements, and Blocks

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 #include int abc (...) {... ; return... ; } int cde (...) {... ; return... ; } int main() {...; return 0; } A Typical Program function statements

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 A C++ program comprises a number of functions. A function comprises a number of statements. A statement can be as simple as Can be as complicated as A statement must end with a semicolon ; In a statement, space carries no information. Statements x = a + b; if (choice == Sunday || choice == Saturday) cout << "\nYou're already off on weekends!\n"; x = a + b; 

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 Expressions A statement comprises one or many expressions. Anything that returns a value is an expression. Examples of expressions 3.2 // Returns the value 3.2 studentPerClass // Returns a constant, say 87 x = a + b; // Here, two expressions // Return a + b and return the value of a variable x Since x = a + b is an expression (i.e. return a value), it can certainly be assigned to another variable. For example, y = x = a + b; // assign y to the value of x which is // equal to the sum of a and b

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 Operators An expression often comprises one of more operators. The assignment operator ' = ' assigns a value to a variable or constant, e.g. x = 39; Several mathematical operators are provided by C++. Let a be 3, b be 4 and x is declared as an integer, Operators Meaning +Add -Subtract *Multiply /Divide %modulus Examples x = a + b;// x = 7 x = a - b;// x = -1 x = a * b;// x = 12 x = a / b;// x = 0 (why?) x = a % b;// x = 3 (why?) 3 modulo 4 is 3

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Integer and Floating Point Divisions #include using namespace std; void intDiv(int x, int y) {int z = x/y; cout << "z: " << z << endl; } void floatDiv(int x, int y) {float a = (float)x;// old style float b = static_cast (y);// ANSI style float c = a/b; cout << "c: " << c << endl; } int main() {int x = 5, y = 3; intDiv(x,y); floatDiv(x,y); return 0; } Only give integer division result, i.e. 1 Give floating point division result, i.e Casting Ask the compiler to temporarily consider x as a floating point no., i.e. 5.0 in this case. Casting Ask the compiler to temporarily consider x as a floating point no., i.e. 5.0 in this case. x, y of main() are not x, y of intDiv(int, int)

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 For some commonly used expressions, there are some shorthands. c = c + 1;  c += 1;  c++;  ++c; //increment c = c - 1;  c -= 1;  c--;  --c; //decrement c = c + 2;  c += 2; c = c - 3;  c -= 3; c = c * 4;  c *= 4; // there is no c** or c//, c = c / 5;  c /= 5; // since it is meaningless Let c be an integer with value 5. Note that a = c++;// a = 5, c = 6 since a = c first and then c++ a = ++c;// a = 6, c = 6 since ++c first and then a = c Shorthand of Expressions

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 Mathematical operations are basically performed from left to right. It follows the normal mathematical precedence that multiplication / division first and addition / subtraction next. Hence To change the precedence, we can use parentheses. Parentheses can be nested as in normal arithmetic. Precedence and Parentheses x = * 8; // x = 29 since we evaluate 3 * 8 first x = (5 + 3) * 8; // x = 64 since we evaluate first x = 5 * ((3 + 2) + 3 * 2); // x = 5 * (5 + 6) = 55

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 Exercise 3.3a #include int main() { int myAge = 39; // initialize two integers int yourAge = 39; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; myAge++; // postfix increment ++yourAge; // prefix increment cout << "One year passes...\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; cout << "Another year passes\n"; cout << "I am: " << myAge++ << " years old.\n"; cout << "You are: " << ++yourAge << " years old\n"; cout << "Let's print it again.\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; return 0; }

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 Relational Operators Besides arithmetic operations, we can perform relational operations with C++. A number of relational operators are defined in C++. Each relational operator returns a bool result (true or false). In C++, zero is considered false, and all other values are considered true. “True” is usually represented by 1.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 Relational Operators Operators Meaning ==Equals !=Not Equals >Greater than >=Greater than or equals <Less than <=Less than or equals Examples 100 == 50;// return false 100 != 50;// return true 100 > 50;// return true 100 >= 50;// return true 100 < 50;// return false 100 <= 50;// return false

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Relational operators are often used with if statement. The if statement provides branching of the program execution depending on the conditions. int bigNumber = 10, smallNumber = 5; bool bigger = bigNumber > smallNumber; //bigger = true if (bigger) {doSomeThing(); } Applications of Relational Operators Or you can simply write: if (bigNumber > smallNumber) {doSomeThing(); } Or you can simply write: if (bigNumber > smallNumber) {doSomeThing(); }

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 Logical Operators Logical operators do logical operations for bool variables. Each logical operation returns a bool result (true or false). Operators Meaning && AND || OR ! NOT Examples expression1 && expression2 expression1 || expression2 !expression int bigNum = 10, smallNum = 5; if (bigNum 0) {doSomeThing(); } Will doSomeThing() be called??? NO

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 int bigNum = 10, smallNum = 5; if ((bigNum 0) || bigNum > smallNum) {doSomeThing(); } Will doSomeThing() be called??? int bigNum = 10, smallNum = 5; if (!(bigNum > smallNum)) {doSomeThing(); } Will doSomeThing() be called??? FT YES NO

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 # include using namespace std; int main() { int a, b, c; cout << "Enter 3 numbers: \n"; cin >> a; cin >> b; cin >> c; if (c = (a+b)); {cout << "c = a+b\n"; } return 0; } Exercise 3.3b The following program asks user to enter 3 numbers. If the sum of the first two is equal to the last one, a message is printed to the console. a.Build the project and note the result. Does it perform correctly? b.If not, fix the problem and re-build the program.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE). Teach Yourself C++ in 21 Days, Second Edition (

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 Reading Read Hours 4-5 in 24 hours