Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.

Slides:



Advertisements
Similar presentations
Dale/Weems/Headington
Advertisements

1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Chapter 01: Introduction to Computer Programming
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Fundamental Programming: Fundamental Programming Introduction to C++
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 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++.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
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
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Basic Elements of C++.
Chapter 2: Introduction to C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Introduction to the C Language
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Introduction to C++ Programming Language
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 2 part #3 C++ Input / Output
C++ Programming Basics
The Fundamentals of C++
Presentation transcript:

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea

Pre-compiler directive Opening brace Closing brace Opening brace Closing brace Structure of a C++ Program

Without namespace Hello World! Namespace std contains all the classes, objects and functions of the standard C++ library. #include int main () { std::cout << "Hello world!\n"; return 0; }

Preprocessor Directives #include “I want to use a predefined library called iostream” Always start with a ‘#’ iostream: a library for inputs (from e.g., a user) and outputs (to e.g., the monitor)

“using” Directives using namespace std; “I want to use objects in a name group ‘std’ ” Tells the compiler where to look for names in the library Can deal with the situation where two or more objects in different libraries share a same name (naming confliction). –Read Appendix N for more about namespace

main function int main() The main body of the program. Compiler first tries to locate “main()” to find where to begin the program In the form of a function –I will cover “function” soon

Comment Internal program document Not considered as a program code Start of comment End of comment Start of comment End of comment

Nested Block Comments are Invalid

Variables Named memory locations that have a type –Named: identifier –Type: needs declaration What you can do with variables –Storing data –Modifying data –Reading data

Variables and Identifiers Memory Address of memory: Hard to remember Identifier: name of address

Variables and Identifiers Memory studentID studentGrade1 studentGrade2 Identifiers

Variables and Identifiers Memory studentID studentGrade studentName Compiler keeps track of [identifier-address] table

Variables and Identifiers In program studentID_Total_Grade = studentGrade1 + studentGrade2

Naming Identifiers Allowed characters: A-Z, a-z, 0-9, _ (underscore) Not allowed to start with a digit. E.g., 3class (x), class3(o) The identifier cannot duplicate a reserved word. e.g., if, case, while… Good names  descriptive but short C++ is case sensitive; PI, Pi and pi are different.

Standard Data Types

Integer and Floating Point Types 2 or 4 Bytes 4 Bytes 2 Bytes 8 Bytes 10 Bytes 4 Bytes Size of value type depends on computer architecture

Maximum/Minimum of Integer Value Type TypeSignByteMinimum valueMaximum value short int/short signed 2 -32,76832,767 unsigned065,535 int (PC) signed 2 -32,76832,767 unsigned065,535 int (Mainframe) signed 4 -2,147,483,6482,147,483,647 unsigned04,294,967,295 long int/long signed 4 -2,147,483,6482,147,483,647 unsigned04,294,967,295

Maximum/Minimum of C++ data types

Variables Declaration

Variable Initialization Variable declaration ≠ variable initialization Should be initialized by a programmer before it is used e.g., int count;  declaration (o), initialization(x) char grade = ‘d’;  declaration (o), initialization(o)

Constants Data values that cannot be changed during program execution E.g., – –‘d’ –“Hello word” –‘\0’

To Remember A character constant is enclosed by the single quotes. (e.g. ‘a’) Use double quotes for string constants. (e.g. “Jeon, Seokhee”) bool types are treated as a number. True: non-zero. False: zero.

Standard streams A mapping between data and input/output device

More about cout width(int) function sets the width for printing a value Only works until the next insertion command comes int x = 42; cout.width(5); cout << x << ‘\n’; // Outputs 42

More about cout fill(char) function sets the fill character. The character remains as the fill character until set again. int x = 42; cout.width(5); cout.fill(‘*’); cout << x << ‘\n’; // Outputs ***42

More about cout precision (int) sets the number of significant digits of float type numbers float y = ; cout.precision(1); cout << y << '\n'; // Outputs 2e+01 cout.precision(2); cout << y << '\n'; // Outputs 23 cout.precision(3); cout << y << '\n'; // Outputs 23.1

More about cout Output Manipulators (not a function) endl - outputs a new line character, flushes output dec - sets int output to decimal hex - sets int output to hexadecimal oct - sets int output to octal #include int x = 42; cout << oct << x << endl; // Outputs 52\n cout << hex << x << endl; // Outputs 2a\n cout << dec << x << endl; // Outputs 42\n

Example codes reading (Program 2-2) #include using namespace std; int main (void) { int a; int b; int c; int sum; cout << "Welcome. This program adds\n"; cout << "three numbers. Enter three numbers\n"; cout \n"; cin >> a >> b >> c; // Numbers are now stored in a, b, and c. Add them. sum = a + b + c; cout << "\nThe total is: " << sum << "\n"; cout << "\nThank you. Have a good day.\n"; return 0; }// main Welcome. This program adds three numbers. Enter three numbers in the form: nnn nnn nnn The total is: 66 Thank you. Have a good day. Welcome. This program adds three numbers. Enter three numbers in the form: nnn nnn nnn The total is: 66 Thank you. Have a good day.

Try to understand other examples in textbook! Program 2-3 ~ 2-13