Download presentation
Presentation is loading. Please wait.
Published byKenneth Carroll Modified over 9 years ago
1
C++ Programming Language Day 1
2
What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection (Control flow) Syntax Day 2 – Repetition Syntax
3
Start a new project Create a new project for OS X command line tool. In the main.cpp, you will see #include int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; return 0; }
4
#include Lines beginning with a hash sign (#) are directives for the preprocessor. In this case the directive #include tells the preprocessor to include the iostream standard file. So that functions in the iostream library can be used in the program.
5
int main(int argc, const char * argv[]) This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
6
std::cout << "Hello, World!\n"; This line is a C++ statement. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.
7
return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.
8
Xcode’s color Pinks are reserved keywords used by the C++ complier. Purples are name of pre-defined functions that are either coded in your program or in the standard library file you included. Greens are comments. They have no computational effects. Reds are character strings.
9
using namespace std; All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.
10
Concept of variable Programming is not limited to only printing simple texts on screen. In order to go a little further on and to become able to write programs that perform useful tasks that really save us work. You need to know the concept of variable.
11
Concept of variable There will be many occasions in which you have some data that are required for later use. Such data need to be store on the CPU memory. And these data must provide ease of access and modification. Variable is a portion of memory to store a value.
12
Variable identifier An identifier is a name we give to a variable. A valid identifier need to fulfil these requirements – can only contains alphabets, numbers or underscore character( _ ). – must start with a alphabets or underscore. – must not be a reserved word.
13
Fundamental data type char – a single character, such as 'A' or '$' or even a keypress from the keyboard ('Esc' or 'Return') int – integer numerical value. E.g. 7, 1024 float – floating point numerical value. E.g. 3.14, 0.001 bool – boolean value. E.g. True or false
14
Declaring a variable Start with the fundamental data type, followed by a space and a valid identifier int Total; float exam_marks;
15
Declaring multiple variables When declaring multiple variables of the same data type, you can separate the identifiers with comma (,). int a, b, c; float ca1, sa1, ca2, sa2;
16
Assigning value to variable The equal symbol (=) is use to assign a value to a variable. Values on the right hand side of the = is stored into the variable on the left hand side. int a, b, c; a = 10; b = 15; char d; d = 'A';
17
Arithmetic operators +Addition -Subtraction *Multiplication /Division %Modulo
18
#include int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; int a, b, total; a = 10; b = 15; total = a+b; return 0; }
19
Compound Assignment Expressionis equivalent to value += increase;value = value + increase a += 2;a = a + 2 a *= c+1;a = a * (c+1) value ++;value increase by 1 a++;a = a+1 value --;value decrease by 1 b--;b=b-1;
20
cout By default, standard output of a program is the display (screen). The command for the output stream is cout found in iostream library. cout is used with the insertion operator (<<) To display the value of an identifier, you simply use the identifier name. To display a sentence, you enclosed the sentence with double quotes.
21
#include int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; int a, b, total; a = 10; b = 15; total = a+b; cout << "Total is " << total << endl; return 0; }
22
endl and "\n" To display text on a new line, you can use either endl or "\n" Example cout << "This is line 1.\n"; cout << "This is line 2." << endl; cout << "This is line 3." << "\n";
23
cin The standard input device is usually the keyboard. The cin stream from iostream is used. cin is used with the extractor operator (>>) cin can only process the stream after the Enter/Return key is being pressed. cin must be use with a variable. Input data must be stored in appropriate data type.
24
#include int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; int a, b, total, budget; a = 10; b = 15; total = a+b; cout << "Total is " << total << endl; cout << "Enter budget : "; cin >> budget; return 0; }
25
Selection The easiest to achieve simply conditional structure (decision making) is to use the if statements Syntax if (condition) { statements }
26
If… Else The condition is the expression to be evaluated. If the condition is true, the statement(s) in the { } block will then be executed. If there are statements to be execute when the condition is not true, else keyword is used if (condition) { statements_if_true; } else { statements_if_false; }
27
Relational and equality operators ==equal to (equivalent to) !=not equal to >greater than >=greater than and equal to <less than <=less than and equal to
28
#include int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; int a, b, total, budget; a = 10; b = 15; total = a+b; cout << "Total is " << total << endl; cout << "Enter budget : "; cin >> budget; if (total > budget) { cout << "You have exceeded the budget.\n"; } else { cout << "Your spending is within budget.\n"; } return 0; }
29
Nested ifs When there are three or more possible resolutions, nest-ifs statement structure can be used. Syntax if (condition 1) { statement1; } else if (condition 2) { statement2; } else if (condition 3) { statement3; } else { statement4; }
30
#include using namespace std; int main(int argc, const * char argv[]) { int choice; cout << "Enter a number between 1..3 : "; cin >> choice; if (choice == 1) { cout << "If the facts don't fit the theory, change the facts.\n"; } else if (choice == 2) { cout << "The good thing about science is that it's true whether or not you believe in it.\n"; } else if (choice == 3) { cout << "The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom.\n"; } else { cout << "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.\n"; } return 0; }
31
Try it out! Write a program to compute a student's grade based on the following calculation Final score = Semester 1 score *35% + Semester 2 score *65% Final score100-7574-7069-6564-6059-5554-5049-0 GradeA1A2B3B4C5C6Fail
32
Try it out! The last alphabet of the Singapore NRIC is a check digit. It is use as first line check to determine if the NRIC is legit. Write a program to compute the check digit.
33
Exercise -- NRIC 1.Separate the 7 numbers and multiple with a fixed weightage based on the position of the number. 2.Add up all the 7 values Example: 9012738 (9*7) + (2*0) + (1*3) + (2*4) + (7*5) + (3*6) + (8*7) Position1st2nd3rd4th5th6th7th Weightages7234567
34
Exercise -- NRIC If the starting alphabet of the NRIC starts with 'T' or 'G', add 4 to the total. Find the remainder of (total divide 11) 012345678910 S or T JZIHGFEDCBA F or G XWUTRQPNMLK
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.