Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22
Outline 1 Recall from last week... 2 Recall: Output 3 Recall: Variables 4 Output Manipulators endl 5 Input 6 Flow of control: if-blocks 7 Loops for loops 8 What is a compiler? CS427 Getting started with C++ (Part 2) 2/22
Recall from last week... Please review the notes from last week. Also, please read Chapter 1 of Kelley and Pohl: A Book on C. Each program begins by including header files. In particular, we need I/O Steam: #include <iostream> Declare variables before using them. They may be local to a program block. the heart of the program is the main() function every program needs one. void is the default argument list and can be omitted. Like C, the C++ language is case-sensitive. E.g., the functions main() and Main() are not the same. Curly brackets are used the delimit a program block. Every (logical) line is terminated by a semicolon; In strings, the backslash escape character is used for output. For example \n is used to output a new line. Two forward-slashes // indicate a comment everything after them is ignored until an end-of-line is reached. CS427 Getting started with C++ (Part 2) 3/22
Recall: Output Basic components: #include <iostream> using namespace std; cout and cin The operators << and >> the identifier cout is an object representing the Standard Output Stream. The operator << is the put to operator and sends the text to Standard Output usually the terminal/console window. E.g. cout << "Hello World." << "\n"; CS427 Getting started with C++ (Part 2) 4/22
Recall: Variables Variables are used to temporarily store values (numerical, text, etc,...) and refer to them by name, rather than value. More formally, the variable s name is called the identifier. It must start with a letter or an underscore, and may contain only letters, digits and underscores. Examples: All variables must be defined before they can be used. That means, we need to tell the compiler before we use them. This can be done at any stage in the code, up to when the variable is first used. Every variable should have a type; this tells use what sort of value will be stored in it. The variables/data types we can define include Integers (positive or negative whole numbers), e.g., i n t i ; i = 1 i n t j =122; i n t k = j+i ; CS427 Getting started with C++ (Part 2) 5/22
Recall: Variables Floats these are not whole numbers. They usually have a decimal places. E.g, float pi=3.1415; Note that one can initialize (i.e., assign a value to the variable for the first time) at the time of definition. Characters single alphabetic or numeric symbols, are defined using the char keyword: char c; or char s= 7 ; Note that again we can choose to initialize the character at time of definition. Also, the character should be enclosed by single quotes. We can declare arrays or vectors as follows: int Fib[10]; This declares a integer array called Fib. To access the first element, we refer to Fib[0], to access the second: Fib[1], and to refer to the last entry: Fib[9]. In in C, all vectors in C++ are indexed from 0. CS427 Getting started with C++ (Part 2) 6/22
Recall: Variables Here is a list of common data types. Size is measured in bytes. Type Description (min) Size char character 1 int integer 4 float floating point number 4 double 16 digit (approx) float 8 bool true or false 1 See also: 01Variables.cpp CS427 Getting started with C++ (Part 2) 7/22
Recall: Variables As with C, in C++, there is a distinction between declaration and assignment, but they can be combined. Using the const modifier means the value can t be changed later. Example: Displaying the value stored in a variable is easier than in C, because we don t have to explicitly state the data type. Example: CS427 Getting started with C++ (Part 2) 8/22
Output Manipulators endl As well as passing variable names and strings to the output stream, we can also pass manipulators to change how variable values are displayed. Some require that <iomanip> be included. endl print a new line (and flush) 1 i n t main ( ) 2 { 3 i n t i, f i b [ 1 6 ] ; 4 f i b [ 0 ] = 1 ; f i b [ 1 ] = 1 ; 02Manipulators.cpp 6 cout << \n\ nwithout t h e setw m a n i p u l a t o r << e n d l ; 7 f o r ( i =0; i <=12; i ++) 8 { 9 i f ( i >= 2) f i b [ i ] = f i b [ i 1] + f i b [ i 2]; 10 cout << The << i << th << 11 F i b o n a c c i Number i s << f i b [ i ] << e n d l ; 12 } CS427 Getting started with C++ (Part 2) 9/22
Output Manipulators setw(n) will the width of a field to n. Useful for tabulating data. 02Manipulators.cpp 1 cout << \n\ nwith t h e setw m a n i p u l a t o r << e n d l ; 2 f o r ( i =0; i <=12; i ++) 3 { 4 i f ( i >= 2) f i b [ i ] = f i b [ i 1] + f i b [ i 2]; 5 cout << The << setw ( 2 ) << i << th << 6 F i b o n a c c i Number i s << setw ( 3 ) << f i b [ i ] << e n d l ; 7 } 8 r e t u r n ( 0 ) ; 9 } endl..................................................................... Other useful manipulators: setfill setprecision fixed and scientific dec, hex, oct CS427 Getting started with C++ (Part 2) 10/22
Input In C we typically used scanf to take input from the user (via the keyboard). It s syntax is a little difficult: scanf("%d", &i); We need to use an appropriate conversion character so that the inputted data is correctly interpreted (e.g., distinguish between the number 5 and the character 5 ). Also, we have to remember to pass the address of the desination variable. In C++ things can be a little simpler. The object cin is used. It is a name for the C onsole IN put. In conjunction with the operator >> (called the get from or extraction operator), it assigns data from input stream to the named variable. CS427 Getting started with C++ (Part 2) 11/22
Input 1 #i n c l u d e <i o s t r e a m > 2 #i n c l u d e <s t r i n g > 3 #i n c l u d e <iomanip> 03Input.cpp 5 u s i n g namespace s t d ; 7 i n t main ( ) 8 { 9 c o n s t double S t i r l i n g T o E u r o =1.20810; // Correct 15/01/12 10 double S t i r l i n g ; 11 cout << I n p u t amount i n S t i r l i n g : ; 12 c i n >> S t i r l i n g ; 13 cout << That i s worth << S t i r l i n g S t i r l i n g T o E u r o << 14 Euros \n ; 15 cout << That i s worth << f i x e d << s e t p r e c i s i o n ( 2 ) << 16 \u20ac << S t i r l i n g S t i r l i n g T o E u r o << e n d l ; 17 r e t u r n ( 0 ) ; 18 } CS427 Getting started with C++ (Part 2) 12/22
Input As we will see, cin is an object, with more sophisticated uses/methods than shown here. However, we will defer this discussion until we have studied something of objects and classes. CS427 Getting started with C++ (Part 2) 13/22
Flow of control: if-blocks if statements are used to conditionally execute part of your code. Structure (i): if( exprn ) { statements to execute if exprn evaluates as non-zero } else { statements if exprn evaluates as 0 } CS427 Getting started with C++ (Part 2) 14/22
Flow of control: if-blocks The argument to if() is a logical expression. Example x == 8 m == 5 y <= 1 y!= x y > 0 More complicated examples can be constructed using AND && and OR. CS427 Getting started with C++ (Part 2) 15/22
Flow of control: if-blocks 1 #i n c l u d e <i o s t r e a m > 04EvenOdd.cpp 3 u s i n g namespace s t d ; 5 i n t main ( v o i d ) 6 { 7 i n t Number ; 9 cout << P l e a s e e n t e r an i n t e g r e r : ; 10 c i n >> Number ; 12 i f ( ( Number%2) == 0) 13 cout << That i s an even number. << e n d l ; 14 e l s e 15 cout << That number i s odd. << e n d l ; 17 r e t u r n ( 0 ) ; 18 } CS427 Getting started with C++ (Part 2) 16/22
Flow of control: if-blocks More complicated examples are possible: Structure (ii): if( exp1 ) { statements to execute if exp1 is true } else if (exp2 ) { statements run if exp1 is false but exp2 is true } else { catch all statements if neither exp1 or exp2 true. } CS427 Getting started with C++ (Part 2) 17/22
Flow of control: if-blocks 05Grades.cpp 1 i n t main ( v o i d ) 2 { 3 i n t NumberGrade ; 4 char L e t t e r G r a d e ; 6 cout << P l e a s e e n t e r t h e g r a d e ( p e r c e n t a g e ) : ; 7 c i n >> NumberGrade ; 9 i f ( NumberGrade >= 70 ) 10 L e t t e r G r a d e = A ; 11 e l s e i f ( NumberGrade >= 60 ) 12 L e t t e r G r a d e = B ; 13 e l s e i f ( NumberGrade >= 50 ) 14 L e t t e r G r a d e = C ; 15 e l s e i f ( NumberGrade >= 40 ) 16 L e t t e r G r a d e = D ; 17 e l s e 18 L e t t e r G r a d e = E ; 20 cout << A s c o r e o f << NumberGrade << 21 % c o o r e s p o n d s to a << L e t t e r G r a d e <<. << e n d l ; 22 r e t u r n ( 0 ) ; 23 } CS427 Getting started with C++ (Part 2) 18/22
Flow of control: if-blocks The other main flow-of-control structures are the switch... case structures the use of the? and : operators. Exercise Find out how switch.. case works. Rewrite the Even/Odd example above using switch... case. What errors/bugs/problems are there with the Grades example? Use switch to write an improved version. CS427 Getting started with C++ (Part 2) 19/22
Loops for loops Loops in C++ are the same as in C. The most commonly used loop structure is for for(initial value; test condition; step) { // code to execute inside loop }..................................................................... Example: 06CountDown.cpp 1 i n t main ( v o i d ) 2 { 3 i n t i ; 5 f o r ( i =10; i >=1; i ) 6 cout << i <<... ; 8 cout << Zero! \ n ; 10 r e t u r n ( 0 ) ; 11 } CS427 Getting started with C++ (Part 2) 20/22
Loops for loops The other two common forms of loop in C++ are while loops do... while loops Exercise Rewrite the count down example above using a 1 while loop. 2 do... while loop. CS427 Getting started with C++ (Part 2) 21/22
What is a compiler? CS427 Getting started with C++ (Part 2) 22/22