c++ programming language

34
C++ Programming Language Day 1

Upload: winter-decker

Post on 04-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

C++ Programming Language. Day 1. 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. Start a new project. Create a new project for OS X command line tool. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Programming Language

C++ Programming Language

Day 1

Page 2: C++ Programming Language

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

Page 3: C++ Programming Language

Start a new project

• Create a new project for OS X command line tool.

• In the main.cpp, you will see#include <iostream>

int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; return 0;}

Page 4: C++ Programming Language

#include <iostream>

• Lines beginning with a hash sign (#) are directives for the preprocessor.

• In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. So that functions in the iostream library can be used in the program.

Page 5: C++ Programming Language

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.

Page 6: C++ Programming Language

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.

Page 7: C++ Programming Language

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.

Page 8: C++ Programming Language

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.

Page 9: C++ Programming Language

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.

Page 10: C++ Programming Language

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.

Page 11: C++ Programming Language

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.

Page 12: C++ Programming Language

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.

Page 13: C++ Programming Language

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

Page 14: C++ Programming Language

Declaring a variable

• Start with the fundamental data type, followed by a space and a valid identifier

• int Total;• float exam_marks;

Page 15: C++ Programming Language

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;

Page 16: C++ Programming Language

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';

Page 17: C++ Programming Language

Arithmetic operators

+ Addition- Subtraction* Multiplication/ Division% Modulo

Page 18: C++ Programming Language

#include <iostream>

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;}

Page 19: C++ Programming Language

Compound Assignment

Expression is equivalent tovalue += increase; value = value + increasea += 2; a = a + 2a *= c+1; a = a * (c+1)

value ++; value increase by 1a++; a = a+1value --; value decrease by 1b--; b=b-1;

Page 20: C++ Programming Language

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.

Page 21: C++ Programming Language

#include <iostream>

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;}

Page 22: C++ Programming Language

endl and "\n"

• To display text on a new line, you can use either endl or "\n"

• Examplecout << "This is line 1.\n";cout << "This is line 2." << endl;cout << "This is line 3." << "\n";

Page 23: C++ Programming Language

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.

Page 24: C++ Programming Language

#include <iostream>

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;}

Page 25: C++ Programming Language

Selection

• The easiest to achieve simply conditional structure (decision making) is to use the if statements

• Syntaxif (condition) {

statements}

Page 26: C++ Programming Language

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 usedif (condition) {

statements_if_true;}else {

statements_if_false;}

Page 27: C++ Programming Language

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

Page 28: C++ Programming Language

#include <iostream>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;}

Page 29: C++ Programming Language

Nested ifs

• When there are three or more possible resolutions, nest-ifs statement structure can be used.

• Syntaxif (condition 1) {

statement1;} else if (condition 2) {

statement2;} else if (condition 3) {

statement3;} else {

statement4;}

Page 30: C++ Programming Language

#include <iostream>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;

}

Page 31: C++ Programming Language

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 score 100-75 74-70 69-65 64-60 59-55 54-50 49-0Grade A1 A2 B3 B4 C5 C6 Fail

Page 32: C++ Programming Language

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.

Page 33: C++ Programming Language

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)

Position 1st 2nd 3rd 4th 5th 6th 7thWeightages 7 2 3 4 5 6 7

Page 34: C++ Programming Language

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)

0 1 2 3 4 5 6 7 8 9 10

S or T J Z I H G F E D C B A

F or G X W U T R Q P N M L K