Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.

Similar presentations


Presentation on theme: "CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding."— Presentation transcript:

1 CS201 Introduction to Computing @ Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding the syntax and semantics of the programming language ä Syntax is similar to rules of spelling and grammar: After main() you should put { ä Semantics is what a program means l Approaches of learning programming languages ä Template based Examine example programs and make analogies Like a child learns how to speak ä First learn syntax and semantics, then start by writing small programs,... Like learning a foreign language ä Which one do you prefer? ä We will follow the second method

2 CS201 Introduction to Computing @ Sabancı University 2 Towards Understanding of C++ l “Hello World” program. No biggie! #include using namespace std; // traditional first program int main() { cout << " Hello world " << endl; /* display */ return 0; } l This program must be ä typed ä compiled (hello.cpp => hello.obj) ä linked (Build) (hello.obj and any other object modules linked together => project.exe) ä executed

3 CS201 Introduction to Computing @ Sabancı University 3 Anatomy of a C++ Program #include statements make libraries of classes and functions accessible to the program ä Utility functions and tools that make the programmer’s life easier are defined in libraries ä Helps programmers develop code independently in a standard way ä Compiler needs access to interface, what the functions look like, but not to implementation of those functions This is in the #include d file ex. #include for input/output functions  all programs that use standard C++ libraries should have using namespace std;

4 CS201 Introduction to Computing @ Sabancı University 4 Anatomy of a C++ Program l Comments make programs readable by humans (and by assistants!) ä Easier maintenance ä Try to use natural language, do not repeat the code! Bad example area = pi*r*r; /* area is pi*r*r */ Better example area = pi*r*r; /* calculate area */ Using “//” make the line comment line Between /* and */ ä Compiler disregards comments ä Comments in your homework affect your grades ä In VC++, comments are in green

5 CS201 Introduction to Computing @ Sabancı University 5 Anatomy of a C++ Program Execution of the program begins with main Each program must have a main function l Execution of C++ programs is organized as a sequence of statements ä Unless otherwise stated, statements execute sequentially one after another Branching, repetition are possible (we will see them later)  The main function returns a value to the operating system or environment return 0 Why 0? Because 0 means no problem encountered!

6 CS201 Introduction to Computing @ Sabancı University 6 Rules Rules Rules l Now some syntax rules and definitions l ABC of C++ l What is an “identifier”? l Reserved words l Symbols and compound symbols l What is a “literal”? ä Types of literals l Variables and basic types l Where to use blanks, line breaks? l Where to use semicolon? l Basic Input/Output

7 CS201 Introduction to Computing @ Sabancı University 7 Identifiers l Names of elements in a program ä Names of variables, functions, classes, etc. l Sequence of letters (a.. z, A..Z), digits (0..9) underscore _ l Cannot start with a digit number1 valid number_1 valid Me_myself valid 1number not valid l Do not start with an underscore l Case sensitive Number1 and number1 are not the same l Pick meaningful names in the context of your program and improve the self-readability of the code (do not use your boy/girlfriend’s name!) l First occurrence of an identifier must be its declaration  Standard declarations are in #include files ä We will see how to declare user-defined variables, functions, etc. as time goes by

8 CS201 Introduction to Computing @ Sabancı University 8 Reserved Words (Keywords) l Special and fixed meanings ä built-in in C++ language ä no need to have libraries, etc. l Cannot be changed by programmer ä int ä return ä Full list is Table 2.1 of the textbook l You cannot use a reserved word for a user-defined identifier l In MS VC++, reserved words are automatically blue

9 CS201 Introduction to Computing @ Sabancı University 9 Symbols l Non-digit and non-letter characters with special meanings l Mostly used as operators (some examples are below, full list later) +addition, sign -subtraction, minus =assignment /division * multiplication % modulus l Compound symbols (two consecutive symbols – one meaning), examples below, full list later /* */ << >> == equality comparison

10 CS201 Introduction to Computing @ Sabancı University 10 Literals l Fixed values l Different types ä Integer 3 454 -43 +343 ä Real 3.1415 +45.44 -54.6 1.2334e3 Last one is 1.2334 times 10 to the power 3 (scientific notation) ä String Sequences of characters Within double quotes (quotes are not part of the string) Almost any characters is ok (letters, digits, symbols) " Hello my friend " " 10 is bigger " " 10 > 22 ? "

11 CS201 Introduction to Computing @ Sabancı University 11 Variables and types l Variables are used to store data that can change during program ä Memory locations of certain sizes ä Data input must be stored in a variable ä Results are stored in variables ä generally defined at the beginning of functions (no strict rule on the place of definition except being before the first use) l Basic types (more to follow) ä Integer int list of identifiers separated by comma ; int number1, age, deniz; ä Real float list of identifiers separated by comma ; float area, circumference; There is another real type called double that we will see later ä String string list of identifiers separated by comma ; string myname; number1 age Memory deniz area circumference myname

12 CS201 Introduction to Computing @ Sabancı University 12 Arithmetic Operations Operators: * - + / % l Operands: values that operator combines ä variables or literals l Combination of operators and operands is called expression l Syntax and semantics for arithmetic operations  Addition, subtraction: + and –,  for int, real (float and double) 23 + 4 x + y d – 14.0 + 23 5 - 3 + 2  Multiplication: *, for int, real (float and double) 23 * 4 y * 3.0 d * 23.1 * 4 5 – 3 * 2  Division: /, different for int and real number types ( double, float) for int operands result is the integer part of division 21 / 4 is 5 21 / 4.0 is 5.25 x / y  Modulus: %, remainder of division, only for int 21 % 4 is 1 18 % 2 is 0 x % y 4

13 CS201 Introduction to Computing @ Sabancı University 13 Assignment Operator variable = expresion l To store a new value in a variable l The value of expression becomes the value of variable l Previous value of variable is lost int a, b; a = 45; b = a+54; a*b = 332; wrong syntax l Be careful about the types of left and right hand sides ä they must match ä compiler may or may not warn you Memory a 45 name value b 99

14 CS201 Introduction to Computing @ Sabancı University 14 Example Program l Write a program to calculate the area of a circle ä program first input a name and print a greeting ä input the radius ä calculate and display area l identify literals, identifiers, symbols, variables and expressions in this program #include using namespace std; // area calculation program int main() { int radius; float area; string myname; cout << "Please enter your name: "; cin >> myname; cout << "Hello " << myname << "! Welcome to my area calculation program" << endl; cout << "Please enter the radius of your circle: "; cin >> radius; area = 3.14*radius*radius; cout << "the area is: " << area << endl; return 0; }

15 CS201 Introduction to Computing @ Sabancı University 15 Issues l What happens if the user enters a real number for radius? ä wrong result ä solution: real radius l Can we combine cout << "Hello " << myname << "! Welcome to my area calculation program" << endl; cout << "Please enter the radius of your circle: "; ä Yes l Can we eliminate the variable area? ä Yes

16 CS201 Introduction to Computing @ Sabancı University 16 Where to use Blanks l You must have at least one blank ä between two words (identifiers or keywords) ä between a word and numeric literal l You cannot have a blank ä within a word ä within a compound symbol ä within a literal except string literals, in string literals blanks are blanks l At all other places ä blanks are optional l Several blanks are functionally same as single blank ä except within string literals

17 CS201 Introduction to Computing @ Sabancı University 17 Where to use Line Breaks and Semicolon l As a general rule, line breaks are possible whenever blank is possible ä exception: a string literal must start and finish on the same line l Where to use a semicolon? ä general rule: at the end of a statement ä but there are exceptions variable declaration: variable declaration is not a statement but you have to use semicolon at the end of each declaration ä it may be hard to identify the statements is #include a statement? ä an ability that will be gained over time

18 CS201 Introduction to Computing @ Sabancı University 18 Stream output l Output is an essential part of our programs cout is the standard output stream (monitor), Accessible via #include Objects are inserted onto stream with insertion operator << Objects are literals, variables or expressions ä expressions are evaluated before output  whatever exists within a string literal between " " are displayed Different objects separated by insertion operator << cout << "yadda yadda" << endl << "yadda" << endl; cout << "gross = " << 12*12 << endl; cout << 5 << " in. = "; cout << 5*2.54 << " cm. " << endl; endl means “end of line” (defined in iostream ) ä it causes the next output displayed at the beginning of the next line ä Line breaks in the program do not finish the output line in the output screen yadda gross = 144 5 in. = 12.7 cm.

19 CS201 Introduction to Computing @ Sabancı University 19 Stream Input l cin ä standard input stream (from keyboard) ä you can input only into variables  extraction operator >> is used between cin and variables cin >> variable1 >> variable2 >> variable3.......... ; int a, b, anynum; cin >> b >> anynum >> a; Data entry must be in the same order of variables in cin statement  first the value for b, then the value for anynum, then the value of a must be entered by the user using the keyboard

20 CS201 Introduction to Computing @ Sabancı University 20 Stream Input l You have to have at least one blank between any two input entry ä Multiple blanks are OK l You may input values at several lines for a single cin statement l You cannot display something using cin statement l Important note on terminology ä “reading a value” means such an input

21 CS201 Introduction to Computing @ Sabancı University 21 Stream Input – Example showing the operation


Download ppt "CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding."

Similar presentations


Ads by Google