Computer Fundamentals and Programming Lecture Module 2 PDF
Document Details
Polytechnic University of the Philippines
Tags
Summary
This document is a lecture module on computer fundamentals and programming, specifically focusing on declaring variables in C++. It details different data types like integers, floats, characters, and booleans in C++. The module demonstrates how to declare these different data types and use them in a C++ program.
Full Transcript
Computer Fundamentals and Programming Lecture Module 2 Declaring Variables Objectives: At the end of this lesson, students will be able to : Students will be able to identify and explain the different data types in C++ Students will learn how to declare variables in C+...
Computer Fundamentals and Programming Lecture Module 2 Declaring Variables Objectives: At the end of this lesson, students will be able to : Students will be able to identify and explain the different data types in C++ Students will learn how to declare variables in C++, including syntax students will understand the concept of variable scope (local vs. global variables) and how it affects accessibility within a program. Students will practice using declared variables in expressions and calculations. Comments Comments are essential in programming as they help explain the code, making it easier to understand and maintain. In C++, comments can be used to describe what your code does, to leave notes for yourself or others, and to temporarily disable code during debugging. 1. Single-line Comments Single-line comments start with //. Everything following // on that line will be ignored by the compiler.. 2. Multi-line Comments Multi-line comments start with. They can span multiple lines and are useful for longer explanations. Variable A variable is a named storage location in memory that can hold a value. In C++, we declare a variable before using it, specifying its type and name. Common Data types Integer (int) Floating Point (float, double) Character (char) Boolean (bool) 1. Integer (int) The int type is used to store whole numbers. Example int age = 25; Give an example of an integer variable 2. Floating Point (float and double) float is used for single-precision floating-point numbers, while double is for double-precision. Example float price = 19.99f; // Single precision double distance = 12345.6789; // Double precision Give an example of a float variable. 3. Character (char) The char type is used to store a single character. Example: char initial = 'A'; Give an example of a character variable. 4. Boolean (bool) The bool type is used to store truth values: true or false. Example: bool isStudent = true; #include using namespace std; // Use the standard namespace int main() { // Declaring variables int numStudents = 30; // Integer variable float height = 5.9f; // Float variable double weight = 70.5; // Double variable char grade = 'A'; // Character variable // Output the values of the variables cout