Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
0 views69 pages

C-2ND-SEM

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 69

Computer

Programming 2
Course code : IT CP12 | Prerequisite: IT CP11

Prepared by : JUNREL A. SARUEDA


Required Output / Assessment
Task Percent
Weight

Attendance 10

Examination 30

Class
60
Performance
Total: 100
Grading
Grading Criteria Percent
Attendance 10

Activity 15

Quiz 30

Major Exam 30

Total 100
What is programming?
• refers to the process of designing and building
an executable computer program to
accomplish a specific task or solve a particular
problem. It involves writing a set of
instructions in a way that a computer can
understand and execute. These instructions
are typically written in a programming
language.
What is programming
• language?
is a formal system that specifies a set of rules
and symbols for instructing a computer to
perform specific tasks. It acts as an
intermediary between human programmers
and the computer hardware, enabling humans
to communicate their instructions to the
machine in a way that is both readable and
understandable.
Programming languages can be broadly
categorized into high-level languages and low-
level languages:

High-level languages: These languages are


designed to be easy for humans to understand
and write. Examples include Python, Java, C++,
and JavaScript. High-level languages provide
abstractions and are more portable, meaning the
same code can run on different types of
hardware.
Programming languages can be broadly
categorized into high-level languages and low-
level languages:

Low-level languages: These languages are closer


to the computer's machine code and are more
difficult for humans to read and write. Examples
include Assembly language and machine code.
They offer more direct control over the hardware
but are less portable and require more effort to
program.
0
1
Introduction
C++
What is C++?
C++ is:
● cross-platform language that can be used to
create high-performance applications and
computer software
● was developed by Bjarne Stroustrup, as an
extension to the C language
● gives programmers a high level of control over
system resources and memory
Bjarne
● develop software for server and specific
Stroustrup
application; and video games

The language was updated 4 major times in 2011,


2014, 2017, and 2020 to C++11, C++14, C++17, C+
+20
Very brief history of C++
When to choose C++?
• when program performance matters, dealing
with large amount of data, multiple CPUs,
complex algorithms
• when programming language itself can help
organize your code
ex) in C++ your objects can closely model
elements of your problem
• when accessing to multiple libraries
Why C++?:
• is one of the world's most popular programming
languages
• can be found in today's operating systems,
Graphical User Interfaces, and embedded
systems
• is portable and can be used to develop
applications that can be adapted to multiple
platforms.
• is close to C, C# and Java, it makes it easy for
C vs. C++
Major difference

• The main difference between C and C++ is


that C++ support classes and objects,
while C does not.
• C++ was developed as an extension of C,
and both languages have almost the same
syntax
0
2
IDE
Integrated Development
Environment
What is an IDE?
• An integrated development environment (IDE) is
a software application that helps programmers
develop software code efficiently. It increases
developer productivity by combining capabilities
such as software editing, building, testing, and
packaging in an easy-to-use application. Just as
writers use text editors and accountants use
spreadsheets, software developers use IDEs to
make their job easier.
IDE advantages
Dev C++?
Dev-C++ is an IDE(Integrated
Development Environment) developed
by Bloodshed Software for the Windows
platform. Dev-C++ is used for writing,
compiling, and executing C++
programs. Internally it uses MinGW DevC++

(Minimalist GNU for Windows) Compiler.


It can be used to code programs in C++
as well as C language.
Features of Dev C++
Quick start
Opening a new
file
Quick start
My first program

Expected result
0
3
C++ Syntax
format or rules in scripting C++ code
C++ Syntax
Header file library

-Header files add


functionality to C++
programs.
C++ Syntax using namespace
std

-means that we can


use names for objects
and variables from the
standard library.
C++ Syntax
Blank space

-A blank line. C++


ignores white space.
But we use it to make
the code more
readable.
C++ Syntax The main block of
code

-Main program of the


project. The first block
code to execute.
C++ Syntax
cout <<

-cout << means consol


cout so, cout.
C++ Syntax
return 0

-return 0 ends the


main function.
C++ Syntax
Close curly bracket

-Do not forget to add


the closing curly
bracket } to actually
end the main function.
cout options:

-The using namespace std line can be


omitted and replaced with the std keyword,
followed by the :: operator
1 st
laboratory
Create activity
a program that
output the following in c+
+:
-Save your project in to
your USB and the code
will be presented next
laboratory.

!Reminder follow the


format!
Other escape sequence:
Other escape sequence:
-Horizontal tab
Other escape sequence:
-Backslash character
Other escape sequence:
-Double quote character
0
C++
4
Comments
to make it more readable your C++
code
C++ Comments
● Single-line Comments
-Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by
the compiler (will not be executed).
C++ Comments
● Multi-line Comments
-Multi-line comments start with /* and ends with */. Any text
between /* and */ will be ignored by the compiler
C++ Comments is
essential:
● Code Documentation
● Code Understanding
● Debugging
● Collaboration
● Code Maintenance
● Educational Purposes
● Automatic Documentation Generation
● Regulatory Compliance
C++ Comments is
●essential:
Code Documentation
-Comments provide a way to document your code. They
help explain the purpose of functions, classes, and
variables, making it easier for other programmers (or
even yourself in the future) to understand the code.
Well-documented code is crucial for maintainability and
collaboration
C++ Comments is
●essential:
Code Understanding
-Comments help in understanding the logic and flow of
the code. When someone reads your code, they may not
immediately grasp your thought process. Comments act
as a guide, providing insights into why certain decisions
were made or why specific approaches were taken.
C++ Comments is
●essential:
Debugging
-Comments can be useful during the debugging process.
If you encounter an issue or bug in your code,
comments can provide additional context and hints that
may help you identify and fix the problem faster.
C++ Comments is
●essential:
Collaboration
-In a collaborative environment where multiple
developers are working on the same codebase,
comments become essential for effective
communication. They allow team members to
understand each other's code and facilitate
collaboration without constant communication.
C++ Comments is
●essential:
Code Maintenance
-As software evolves, code undergoes changes.
Comments can help in understanding the historical
context of the code, explaining why certain decisions
were made, and making it easier for developers to
modify or extend the code without introducing errors.
C++ Comments is
●essential:
Educational Purposes
-Comments can serve as educational tools for new
programmers. When learning from or contributing to an
existing codebase, comments can provide valuable
insights into programming techniques, design decisions,
and best practices.
C++ Comments is
●essential:
Automatic Documentation Generation
-Many documentation tools extract comments from the
source code to generate documentation automatically.
This is commonly used in libraries and APIs to create
readable and up-to-date documentation for users.
C++ Comments is
●essential:
Regulatory Compliance
-In some industries or projects, there may be regulatory
requirements to provide detailed documentation for the
code. Comments help in meeting these compliance
standards by offering a clear explanation of the code's
functionality.

Remember!
2nd laboratory Activity
Create a program that the output the
console has horizontal tab, backslash
character and double quote character.
Ex) small personal essay, short story, local
news.

-Save your project in to your USB and the


code will be presented next laboratory.

!Reminder follow the format!


0
5
C++ Variables
Mindset nimo: sudlanan or
butanganan
C++ variables
● In programming languages, a variable is a symbolic
name or identifier that is used to represent a storage
location (memory address) paired with an associated
value. This value can be changed or varied during the
execution of a program, hence the name "variable.“
● Variables are containers for storing data values.
In C++, there are different types of variables:
-int, float, double, char, string, bool
C++ variables
● Variables play a crucial role in programming as they
allow developers to store and manipulate data in a
flexible manner. When you declare a variable in a
program, you are essentially creating a named
storage location in the computer's memory where
data can be stored and retrieved.
C++ variables
-int
>is a fundamental data type that represents integers. It
stands for "integer," and it is used to store whole
numbers WITHOUT any decimal points.
>The int data type typically represents a 32-bit signed
integer on most systems. This means it can store values
in the range of -2,147,483,648 to 2,147,483,647
(inclusive). The exact range may vary depending on the
system and compiler.
C++ variables
-int: Arithmetic operations
>You can perform various arithmetic operations on int
variables, including addition, subtraction, multiplication,
and division.
C++ variables
-int
>Use Cases:
>>int is commonly used when dealing with counting,
indexing, and arithmetic operations where whole
numbers are involved. For example, counting iterations
in loops, representing the size of arrays, and storing
simple numeric values.
C++ variables
-double
>is a fundamental data type used to represent double-
precision floating-point numbers. and it is used to store whole
numbers WITH any decimal points.
>double is a double-precision floating-point type, which means
it provides more precision compared to the float data type.
Typically, it is represented using 64 bits and conforms to the
IEEE 754 standard. It can represent a wider range of values
with greater precision. The range of values is approximately
±5.0 × 10^(-324) to ±1.7 × 10^(308), and it can store
decimal numbers with up to 15 decimal places.
C++ variables
-double: Arithmetic operations
>You can perform various arithmetic operations on
double variables, similar to int.
C++ variables

-double
>Use Cases:
>>double is commonly used when you need to work
with real numbers that may have a fractional part. It's
suitable for applications where precision is important,
such as scientific calculations, financial applications, and
graphics programming.
C++ variables
-float
>float is a fundamental data type used to represent
single-precision floating-point numbers and it is used to
store whole numbers WITH any decimal points.
>!Note! the use of the ‘f’ suffix to explicitly indicate that
the number is a float. Without the suffix, a decimal
literal in C++ is treated as a double by default.
C++ variables
-float
>Precision and Range of Values:
>>float is a single-precision floating-point type, which
means it provides less precision compared to the double
data type. Typically, it is represented using 32 bits and
conforms to the IEEE 754 standard. It can represent a
range of values, but with a smaller precision. The range
is approximately ±1.18 × 10^(-38) to ±3.4 × 10^(38),
and it can store decimal numbers with up to 7 significant
digits.
C++ variables
-float: Arithmetic operations
>You can perform various arithmetic operations on float
variables.
C++ variables
-float
>Use Cases:
>>float is commonly used when memory is a critical
consideration, and a lower precision is acceptable. It is
suitable for applications where storage space is at a
premium, such as in embedded systems, mobile
devices, and some graphics programming.
C++ variables
-char
>char is a fundamental data type used to represent
characters.
>!Note! A char variable stores a single character and is
enclosed in single quotes (‘F’).
>>ASCII Representation:
>>>Internally, char values are represented using ASCII
(American Standard Code for Information Interchange)
or a similar encoding. Each character is associated with
a unique numeric value. For instance, the ASCII value for
'A' is 65, 'B' is 66, 'a' is 97, and so on.
C++ variables
-char
>Use Cases:
>>char is commonly used to represent individual
characters, such as letters, digits, and special symbols.
It's frequently used in handling strings where each
character is stored in a contiguous sequence of char
variables.
C++ variables
-bool
>bool is a fundamental data type used to represent
Boolean values, which can only take on one of two
states: true or false; and also 1 or 0.
>bool is an essential data type for expressing and
working with conditions and logical operations in C++. It
is integral to writing expressive and effective code.
C++ variables
-bool: Logical operations
>bool variables are commonly used in logical
operations. The logical operators in C++ include ‘&&’
(logical AND), ‘||’ (logical OR), and ‘!’ (logical NOT)
C++ variables
-bool: Comparison operations
>bool variables are often the result of comparison
operations. Comparison operators include ‘==‘ (equal),
‘!=‘ (not equal), ‘<‘ (less than), ‘>’ (greater than), ‘<=‘
(less than or equal to), and ‘>=‘ (greater than or equal
to).
C++ variables
-bool
>Use Cases:
>>bool is commonly used to represent conditions and
control flow in programs. It is frequently used in if
statements, loops, and other decision-making
structures.
C++ variables
-string
>string is not a built-in data type like int or char; rather,
it is a part of the Standard Template Library (STL) and is
implemented as a class. The string class provides a
convenient way to work with strings of characters.
C++ variables
-string: Declaration and
Initialization
>To use the string class, you need to include the
<string> header. You can declare and initialize a string
as follows:

You might also like