Computer Programming (1) Lecture Notes PDF
Document Details
Uploaded by ImpressedCalculus8456
Seijoh University
2023
Dr. Khaled Mohammed bin Abdl
Tags
Summary
This document is a lecture on computer programming, focusing on variables, input, and memory. It explains fundamental concepts like data types, ways to declare and initialize variables, using cin for input, and different kinds of variables.
Full Transcript
Computer Programming (1) Lecture No.4: Variables , input , memory Lecturer: Dr. Khaled Mohammed bin Abdl Department of Information Technology, College of Applied Science, Seyion University Semester 1, 2023...
Computer Programming (1) Lecture No.4: Variables , input , memory Lecturer: Dr. Khaled Mohammed bin Abdl Department of Information Technology, College of Applied Science, Seyion University Semester 1, 2023 1 Content 2 Variables C++ Built in data types C++ variable declaration syntax cin statement Local and global variables Scope resolution operator (::) Variables and RAM Defining constants Typecasting Enumerated data types Variable 3 Location on computer’s memory to store data then use and change its value in a program Each variable has 1. Name (identifier) ◼ Series of letters, digits, underscores ◼ Not a keyword ◼ Start with a letter ◼ Case sensitive ◼ Meaningful 2. Type ◼ Programmer-defined Determine which of the following variables name is valid or invalid : 4 _under_bar_ z2 h22 2h 67h2 his_account_total t5 87 m928134 her_sales 3g j7 a b c C++ Built-in Data Types 5 Called fundamental types or primitives types: numeric, character, logical Data Type 6 Bool ❖ Hastwo values, true and false ❖ Manipulate logical (Boolean) expressions ❖ true and false are called logical values ❖ bool, true, and false are reserved word ❑ Char ❖ Used for characters: letters, digits, and special symbols ❖ Each character is enclosed in single quotes, examples: 'A', 'a', '0', '*', '+', '$', '&' ❖ A blank space is a character and is written ' ' with a space left between the single quotes Declaring Variables 7 All variables must be declared anywhere in program with a name and data type before they used Syntax rule: begin with a data type then variable name dataType varName; Variables of the same type can be declared in Multiple lines int num1; One line separated by int num2; int num3; commas int num1, num2, num3; Initializing Variable 8 Variables can be initialized once declared int first=13, second=10; char ch=' '; double x=12.6, y=123.456; first and second are int variables with the values 13 and 10, respectively ch is a char variable whose value is empty x and y are double variables with 12.6 and 123.456, respectively Using cin 9 Namespace, std:: ❖ Specifies using a name that belongs to “namespace” std ❖ Can be removed through use of using statements Standard output stream object, std::cin ❖ “Connected” to keyboard ❖ Defined in input/output stream header file Stream extraction operator >> ❖ Value to left (left operand) inserted into right operand ❖ Waits for user to input value then press Enter key Example, std::cin >> num1; ❖ Inserts the standard input from keyboard into variable num1 cin and cout facilitate interaction between user and program 1 // Fig. 2.5: fig02_05.cpp 2 // Addition program that display the sum of two numbers. 3 #include // allow program to perform input and output 4 5 // function main begins program execution 6 int main() 7 { 8 // variable declaration Declare integer variables. 9 int number1; // first integer to add 10 int number2; // second integer to add 11 Use and int sum; // sum of number1 stream extraction number2 12 operator with standard input 13 std::cout > number1; // read first integer from user to number1 15 Calculations can be performed in output statements: alternative for 16 std::cout > number2; // read second integer from user to number2 18 Stream manipulator std::cout