Assignment Operations

Similar documents
Introduction to Programming

Variables. Data Types.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Basic Types, Variables, Literals, Constants

Tokens, Expressions and Control Structures

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Chapter 2 - Control Structures

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

IS 0020 Program Design and Software Tools

Chapter 2 - Control Structures

Chapter 2 - Control Structures

Introduction to C++ Systems Programming

EEE145 Computer Programming

Chapter 15 - C++ As A "Better C"

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Introduction to C# Applications

Compiler Construction. Lecture 10

Programming. C++ Basics

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Programming in C++ 4. The lexical basis of C++

.Net Technologies. Components of.net Framework

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

EP241 Computer Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 2

Programming with C++ Language

CSCI 123 Introduction to Programming Concepts in C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 7. Additional Control Structures

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

std::cout << "Size of long = " << sizeof(long) << " bytes\n\n"; std::cout << "Size of char = " << sizeof(char) << " bytes\n";

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

EP578 Computing for Physicists

CS3157: Advanced Programming. Outline

3. Java - Language Constructs I

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Fundamentals of Programming

Programming for Engineers Iteration

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

LEXICAL 2 CONVENTIONS

Lecture 02 C FUNDAMENTALS

204111: Computer and Programming

Review of the C Programming Language for Principles of Operating Systems

DEPARTMENT OF MATHS, MJ COLLEGE

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C Programming Class I

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Incompatibilities / Differences between C and C++

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

W3101: Programming Languages C++ Ramana Isukapalli

UEE1302 (1102) F10: Introduction to Computers and Programming

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

CSC 1214: Object-Oriented Programming

Fundamental of Programming (C)

6.096 Introduction to C++ January (IAP) 2009

Axivion Bauhaus Suite Technical Factsheet MISRA

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Data types, variables, constants

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Assignment: 1. (Unit-1 Flowchart and Algorithm)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Expressions and Precedence. Last updated 12/10/18

IS 0020 Program Design and Software Tools

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Review of the C Programming Language

The New C Standard (Excerpted material)

Procedures, Parameters, Values and Variables. Steven R. Bagley

Divyakant Meva. Table 1. The C++ keywords

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Introduce C# as Object Oriented programming language. Explain, tokens,

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Type Conversion. and. Statements

C#: framework overview and in-the-small features

Introduction. C provides two styles of flow control:

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Increment and the While. Class 15

This tutorial adopts a simple and practical approach to describe the concepts of C++.

C: How to Program. Week /Mar/05

Transcription:

ECE 114-4 Control Statements-2 Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering Cal Poly Pomona Electrical & Computer Engineering 1 Assignment Operations C++ provides several assignment operators Example: c=c+3; Can be also written a: c +=3; Similar: c=c*3; c *=3; c=c/3; c /=3; c=c%3; c %=3; c=c-3; c -=3; Cal Poly Pomona ECE 114-4 2 1

Increment and Decrement Operator Operator ++ ++ -- -- Called Prefix increment Postfix increment Prefix decrement Postfix decrement Sample Exp. ++a a++ --a a-- Example: a=3; b=5; x=a++ - b; a=3; b=5; x=a++ - --b; a=3; b=5; x=a++ - b++; x=-2, a=4 b=4,x=-1,a=4 X=-2,a=4,b=6 Cal Poly Pomona ECE 114-4 3 Example // Counter-controlled repetition #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int counter = 1; // initialization while ( counter <= 10 ) // repetition condition Console::WriteLine( S 0, counter.tostring() ); ++counter; // increment return 0; counter=counter+1; Cal Poly Pomona ECE 114-4 4 2

Precedence of the Operators Operators :: () ++ -- static_cast<type>() ++ -- + - * / % + - < <= > >= ==!=?: = += -= /= %= Associatively Right to left Right to left Right to left Type Scope resolution Parenthesis Unary postfix Unary cast Unary prefix Multiplicative Additive Relational Equality Condition Assignment Cal Poly Pomona ECE 114-4 5 auto Standard C++ Keywords bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit extern float for friend goto if inline int long mutable namespace new operator private protected public register rinterpret_cast return short signed sizeof static static_cast struct switch template this throw try typedef typeid typename union unsigned using virtual void volatile wchar_t while Cal Poly Pomona ECE 114-4 6 3

_alignof _declspec _except _identifier _int16 _m64 naked novtable stdcall _unhook Microsoft specific keywords for C++ _asm deprecated _fastcall _if_exitsts _int32 _m128 noinline property _super uuid _assume dllexport _finally _if_not_exists _int64 _m128d _noop _raise thread _uuidof _based dllimport _forceinline _inline _interface _m128i noreturn selectany _try/_expec t _virtualinheritence _cdecl _event _hook int8 _leave _multiple_inhe ritence nothrow _single_inherit ence _try/_finally _w64 _wchar_t Cal Poly Pomona ECE 114-4 7 MC++ keywords (also Microsoft specific) _abstarct _nogc _try_cast _box _pin _delegate _property _gc _sealed Cal Poly Pomona ECE 114-4 8 4

Questions 1. If the originally x=3, y=2, and z=4, what is the value of each of the following expressions? a. a = z - (x + z) % 2 + 4 ; b. b = ++x * y++ - z ; c. x += y *=z -= 2 ; 2. If x=3, y=1, and z=-1, what is the value of the following expressions: a. x && y z && 0 b. x - y < z + 3 c.!(x <= y) d. x 2 * y + y < z * 2 / 3 Cal Poly Pomona ECE 114-4 9 Questions 3. Determine the values of each variable after the calculation is performed. (at the beginning all variables have the integer value 5) a. product *= x++; b. Quotient /= ++x; 4. Identfy and correct the errors in each of the following While (c<=5) product *= c; ++c; If (gender == 1) Console::WriteLine( S Man ); Else; Console::WriteLine( S Man ); Cal Poly Pomona ECE 114-4 10 5

Questions 5. What is the output of the following program #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int sum, x; x=1; sum=0; while (x<=10) sum +=x; x++; Console::WriteLine( S The sum is 0,sum.ToString() ); return 0; Cal Poly Pomona ECE 114-4 11 Flow Control for if and while statements if (test) statements; next statements; while (test) statements; next statements; test True test True False Statements; False Statements; Next Statements; Next Statements; Cal Poly Pomona ECE 114-4 12 6

The for Repetition Statement The for repetition handles all the detail of the countercontrolled repetition. for (counter initial value; condition; increment counter) for (int counter=1; counter <=10; counter++) for (exp-1; exp2; exp3) Statements; Reminder exp1; while (exp2) statements; exp3; Cal Poly Pomona ECE 114-4 13 Examples: // Counter-controlled repetition with the for structure #include "stdafx.h" #using <mscorlib.dll> using namespace System; int_tmain() // Initialization, repetition condition, and incrementing for ( int counter = 1; counter <= 10; counter++ ) Console::WriteLine( S 0, counter.tostring() ); return 0; for loop Cal Poly Pomona ECE 114-4 14 7

Example // Summation #include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() int sum=0; for ( int n = 1; n <= 10; n+=2 ) sum+=number; Console::WriteLine( S Sum is 0,sum.ToString() ); return 0; Cal Poly Pomona ECE 114-4 15 Example of for structure for (int i=1; i <=100; i++) for (int i=100; i >=1; i--) for (int i=10; i <=70; i+=10) for (int i=20; i >=5; i-=2) Counter =1; test True Counter++; Statements; False Next Statements; Cal Poly Pomona ECE 114-4 16 8

The switch Multiple-selection Statement The switch structure consists of a series of case labels, and an optional default case switch (x) case 1: y=2+z; case 2: y=z-2; break; case 4: y=2; break; default: y=0; Cal Poly Pomona ECE 114-4 17 The do while Structure The do while loop structure is similar to while structure. In the do/while structure the loopcondition is not executed until after the action is performed at least once. do statements; while (condition); Example x=0; do y=z+x; x+=2; while (x < 20); Cal Poly Pomona ECE 114-4 18 9

The break and continue Statement The break and continue statements after the flow of control. The break statement, when executed in a while, for, do while, or switch structure, causes immediate exit from structure. The continue statement, when executed in a while, for, or do/while structure, skip the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. Cal Poly Pomona ECE 114-4 19 #include "stdafx.h" #using <mscorlib.dll> using namespace System; Example: int _tmain() int x; // x declared here so it can be used after the loop for ( x = 1; x <= 10; x++ ) if ( x == 5 ) break; // skip remaining code in loop if x is 5 Console::Write( String::Concat.ToString(), S ) ); Console::WriteLine( S \nbroke out of loop at x =0, x.tostring() ); return 0; Cal Poly Pomona ECE 114-4 20 10

Logical Operators MS C++ provides logical operators that are used more complex conditions by combining simple conditions: The logical operators: && (and), (or) and,! (NOT) Example: if (gender ==1 && age >=65) ++ seniorfemales; Cal Poly Pomona ECE 114-4 21 Logical Operators Truth Table for the && Exp1 Exp2 Exp1 && Exp2 Truth Table for the Exp1 Exp2 Exp1 Exp2 Cal Poly Pomona ECE 114-4 22 11

Logical Operators C++ provides the! Operator to enable a programmer to reverse the meaning of the condition. Example: if (!(x==y)) ConsoleWriteLine( S x is not equal to y ); Exp.! Exp. Cal Poly Pomona ECE 114-4 23 Logical Operators!(x==y)!(x!=y)!(x>y)!(x>=y)!(x<y)!(x<=y) x!=y X==y x<=y x<y x>=y x>y Cal Poly Pomona ECE 114-4 24 12