Document Details

LargeCapacitySnowflakeObsidian2219

Uploaded by LargeCapacitySnowflakeObsidian2219

Astana IT University

Aigerim Aibatbek

Tags

C++ programming variables data types computer programming

Summary

This Astana IT University lecture covers introductory C++ programming concepts, specifically focusing on variable declarations, data types, and memory management. The lecture includes examples, diagrams, and explanations of fundamental programming concepts. The document's structure appears to be in lecture note or presentation format, rather than a complete programming manual or past paper.

Full Transcript

INTRODUCTION TO PROGRAMMING - LECTURE 2: C++ PROGRAM BASICS (PART I) Aigerim Aibatbek - Senior Lecturer [email protected] CONTENT 1. First Program – Print on Console 2. Variables 3. How memory work 4. Variable Declaration Properties 5. Variable Initialization Properties 6....

INTRODUCTION TO PROGRAMMING - LECTURE 2: C++ PROGRAM BASICS (PART I) Aigerim Aibatbek - Senior Lecturer [email protected] CONTENT 1. First Program – Print on Console 2. Variables 3. How memory work 4. Variable Declaration Properties 5. Variable Initialization Properties 6. Variable Data Type Data Type Modifiers 7. Variable Identifier FIRST PROGRAM – PRINT ON CONSOLE FIRST PROGRAM Statement in line: 1. - is a compiler preprocessor directive that tells the compiler to include the iostream library in this program, which is needed to support console input and output. C++ library contains predefined code for developing C++ programs. 2. - is used for performing any input/output operations. It tells the compiler to use the standard (std) namespace. Namespace is mechanism to avoid naming conflicts in a large program. The names cout and endl in line 7 are defined in the iostream library in the standard namespace. v Every statement in C++ must end with a semicolon (;), known as the statement terminator (in line 2, 7 and 9). FIRST PROGRAM Statement in line: 4. - Every C++ program is executed from a main function and it appears exactly once. A function is a construct that contains statements. - The main function defined in lines 4–10 contains two statements. They are enclosed in a block that starts with a left brace, {, (line 5) and ends with a right brace, } (line 10). Everything between these braces is called the function's body. - FIRST PROGRAM Statement in line: 9. - is placed at the end of every main function to exit the program. The value indicates that the program has terminated with a successful exit. Some compilers will work fine if this statement is omitted; however, other compilers will not. v Remember, that the statements after are called dead or unreachable code, since that code will never be executed. FIRST PROGRAM Statement in line: 7. - displays a message to the console. The operator, referred to as the stream insertion operator, sends a string to the console. The statement in line 7 first outputs the string to the console, then outputs. v A string (the text “Welcome to C++!”) must be enclosed in quotation marks. 6. Line 6 is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They are not programming statements and thus are ignored by the compiler. In C++, a comment is preceded by two slashes on a line, called a line comment, or enclosed between on one or several lines, called a block comment or paragraph comment. VARIABLES Find A + B = ? Find √x = ? Calculate your monthly expenses ? VARIABLES Find 250 + 150 = ? When developing software, it is necessary to remember and store the data for further use, particularly when accessing them and performing mathematical operations or other manipulations on them. VARIABLES – HOW MEMORY WORKS Imagine you go to a concert and you need to leave your belongings in a wardrobe. A chest of drawers is available for this purpose. VARIABLES – HOW MEMORY WORKS Each drawer can hold only one element. If you want to store two things, you need to ask for two drawers. VARIABLES – HOW MEMORY WORKS You store your two things here and you’re ready for the concert! VARIABLES – HOW MEMORY WORKS This is basically how your computer’s memory works. Your computer looks like a giant set of drawers, and each drawer has an address. VARIABLES – HOW MEMORY WORKS var1 We give a name to the memory address, creating a variable, and use that name to indirectly access the corresponding memory slot. So, A variable is a named location in memory that is used to hold a value. VARIABLE DECLARATION (CREATION) int age; v In computer programming, the process of requesting a Data type Variable name memory slot from the computer is known as variable declaration. v All variables must be first declared before they can be used. v When declaring a variable, you specify its data type (e.g., int, float, char), which determines the type of data that can be stored in the variable, and an identifier (name) for the variable. VARIABLE DECLARATION PROPERTIES v If you need to declare several variables of the same type, you can do it in one single line by separating the variable names with comma. For example: int num1, num2, num3; v If a variable is declared within a specific scope or block, such as inside a function or within curly braces '{ }', it is called a local variable. These variables are accessible only from the same scope. v If a variable is declared outside of any functions or blocks, typically at the global scope, it is referred to as a global variable and they can be accessed from anywhere in the code. VARIABLE DECLARATION #include float PI = 3.14f; // Global variable declaration & initialization int main() { int age = 20; // Local variable declaration & initialization // Accessing the global and local variables std::cout

Use Quizgecko on...
Browser
Browser