C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Basic Program Construction
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Summary Two basic concepts: variables and assignments Basic types:
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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

#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.

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.

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.

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.

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.

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.

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.

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.

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.

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, bool – boolean value. E.g. True or false

Declaring a variable Start with the fundamental data type, followed by a space and a valid identifier int Total; float exam_marks;

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;

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

Arithmetic operators +Addition -Subtraction *Multiplication /Division %Modulo

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

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;

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.

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

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

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.

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

Selection The easiest to achieve simply conditional structure (decision making) is to use the if statements Syntax if (condition) { statements }

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

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

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

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

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

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 GradeA1A2B3B4C5C6Fail

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.

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: (9*7) + (2*0) + (1*3) + (2*4) + (7*5) + (3*6) + (8*7) Position1st2nd3rd4th5th6th7th Weightages

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) S or T JZIHGFEDCBA F or G XWUTRQPNMLK