Computer Programming 1 - Introduction to C++ PDF
Document Details
Uploaded by Deleted User
National College of Science and Technology
Tags
Summary
This document provides an introduction to C++ programming, including basic syntax, examples, and concepts, suitable for computer programming 1 students..
Full Transcript
NATIONAL COLLEGE OF SCIENCE & TECHNOLOGY COLLEGE OF INFORMATION TECHNOLOGY COMPUTER PROGRAMMING 1 COURSE OUTLINE: PRELIMS computer...
NATIONAL COLLEGE OF SCIENCE & TECHNOLOGY COLLEGE OF INFORMATION TECHNOLOGY COMPUTER PROGRAMMING 1 COURSE OUTLINE: PRELIMS computer 1. Text Editor 1. Programming Language - This will be used to type the code of your program. 2. Intro to C++ - Example: Notepad, Code Blocks. 3. C++ Sample Code 4. C++ Code Part 2. C++ Computer 5. C++ Basic Concept 6. Getting the Tools Compilers are used to compile your source code and 7. Printing a Text translate It Into a machine code, a code that a 8. Comments computer can actually understand. 9. Variables and Value 10. Working with Variables Sample code: Introduction to C++ PROGAMMING LANGUAGE What is Programming Language and Example of it. Programming Language - Is a standardized communication technique for expressing Instructions to a computer. - Examples: CH, C. CB, Python, Java Categories of Programming Language: - High-Level Programming Language uses a more natural and human-readable syntax which #include – strands or standard input - output makes it easier for humans to understand and stream. It declares objects that control reading from and write writing to the standard streams - Low-level Assembly Language users write code which is almost identical to the one computer using namespace std; – It means that we can use understands. names for objects and variables from the standard library. C++ Int main () – Defines the entry or the starting point of the C/C++ program code. What is C++? cout – used to display output to the screen. The “c” in C++ is a general-purpose programming language cout stands for character, and “out” stands for output. Invented in the early 1980s by Biarne Stroustrup at end1 – used to insert a new line character and flush the Bell Labs. The purpose of C++ is to precisely define a series of stream. operations that a computer can perform to return 0 – returns 0 means that the program will execute accomplish a task. Most of these operations Involve successfully and did what it was intended to do. manipulating numbers and text, but anything that the computer can physically do can be programmed In C++. Why Use C++? C++ Code Parts The various parts of the given program consists of C++ is one of the world’s most popular programming languages. the ff: C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems. The header contains everything we Ct is an object-oriented programming language needed for handling input and output. which gives a clear structure to programs and allows The line using namespace std; tells the compiler to code to be reused, lowering development costs. use the standard namespace. Namespaces are used C++ is portable and can be used to develop to organize code into logical groups. applications that can be adapted to multiple The line int main() is the main function where platforms. program execution begins. C++ ls fun and easy to learn! The next line cout « "This is my first C++ As C++ is close to C# and java. program."; causes the message "This is my first C++ program" to be displayed on the screen. Getting Started with C++ The next line return 0; terminates main() function and causes it to return the value 0 to the calling process. To start your program development with C++, first you need to Install two kinds of software on your 1 I @jersonjadee NATIONAL COLLEGE OF SCIENCE & TECHNOLOGY COLLEGE OF INFORMATION TECHNOLOGY COMPUTER PROGRAMMING 1 Main Program execution begins with the main function. Int main () cout access () indicates the beginning and end of the function, which can also be called the function’s body, The information inside the brackets indicates Make sure the Compiler is selected and click. Finish. what is the function does when executed. GNU GCCI is one of the popular compilers available for Code: Blocks. Statements On the left sidebar, expand Sources. You'll see your project, along with its source flies. Code-Blocks A block is a set of logically connected statements, surrounded automatically created a main-cpp file that includes a by opening and closing curly braces. basic Hello World program (Cat source files have cpp, cp or c extensions). For example: You can have multiple statements on a single line, as long as you remember to end each statement with a semicolon, failing to do so will result in an error. Getting the Tools Printing a Text You need both of the following components to build C++ programs. can be used as on IDE. Your first C++ Program 1. Integrated Development Environment (IDE): Provides tools You can add multiple insertion operation after cout. for writing source code. Any bext editor cout >), The variable containing the extracted data follows the operator. The following example shows how to accept user input and store it in the num variable: A variable can be assigned a value and can be used to perform operations. For example, we can create an additional variable called sum and add two variables together. Accepting User Input The following program prompts the user to input a number and stores it in the variable a: Use the + operator to add two numbers Let’s create a program to calculate and print the sum of two integers. When the program runs, it displays the message “Please enter a number”, and then wait for enter a number and press Enter, or Return. The entered number is stored in the variable a. The program will wait for as long as the user need to type 4 I @jersonjadee NATIONAL COLLEGE OF SCIENCE & TECHNOLOGY COLLEGE OF INFORMATION TECHNOLOGY COMPUTER PROGRAMMING 1 You can accept user input multiple times throughout the program: Let’s create a program that accepts the input of two numbers and prints their sum. 5 I @jersonjadee