0% found this document useful (0 votes)
28 views

Lecture 01

This document provides an overview of the Computer Fundamentals and Programming II course for the 2022/2023 academic year, taught by Kareem Mahdhloom. It includes notes on C++ programming, beginning with a simple "Hello World" program example. The example code is then explained line-by-line, demonstrating basic C++ syntax and features like comments, namespaces, strings, escape sequences, and the main function. The document also provides definitions for key C++ concepts like tokens, data types, variables, operators, and input/output.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lecture 01

This document provides an overview of the Computer Fundamentals and Programming II course for the 2022/2023 academic year, taught by Kareem Mahdhloom. It includes notes on C++ programming, beginning with a simple "Hello World" program example. The example code is then explained line-by-line, demonstrating basic C++ syntax and features like comments, namespaces, strings, escape sequences, and the main function. The document also provides definitions for key C++ concepts like tokens, data types, variables, operators, and input/output.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Computer Fundamentals and Programming II

second year first semester 2022/2023

Kareem Mahdhloom

C++ PROGRAMMING:
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN
FIFTH EDITION
D.S. MALIK
General Notes on C++
C++ is immensely popular, particularly for applications that require speed and/or access
to some low-level features.

Everything in C++ is case sensitive: someName is not the same as SomeName.

Hello World
In the tradition of programmers everywhere, we’ll use a “Hello, world!” program as an entry point into
the basic features of C++

The code
Tokens:
Tokens are the minimals chunk of program that have meaning to the compiler – the smallest meaningful symbols in the
language. Our code displays all 6 kinds of tokens, though the usual use of operators is not present here
Line-By-Line Explanation
1. // indicates that everything following it until the end of the line is a comment: it is ignored by the compiler.
Another way to write a comment is to put it between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A
comment of this form may span multiple lines. Comments exist to explain non-obvious things going on in the
code.
2. Lines beginning with # are preprocessor commands, which usually change what code is actually being compiled.
#include tells the preprocessor to dump in the contents of another file, here the iostream file, which defines the
procedures for input/output.
4. int main() {...} defines the code that should execute when the program starts up. The curly braces represent
grouping of multiple commands into a block. More about this syntax in the next few lectures.

5. • cout << : This is the syntax for outputting some piece of text to the screen.
• Namespaces: In C++, identifiers can be defined within a context – sort of a directory of names – called a
namespace. When we want to access an identifier defined in a namespace, we tell the compiler to look for it in
that namespace using
the scope resolution operator (::). Here, we’re telling the compiler to look for cout in the std namespace, in which
many standard C++ identifiers are defined. A cleaner alternative is to add the following line below line 2:
using namespace std;
This line tells the compiler that it should look in the std namespace for any identifier we haven’t defined. If we do
this, we can omit the std:: prefix when writing cout. This is the recommended practice.
• Strings: A sequence of characters such as Hello, world is known as a string. A string that is specified explicitly in a
program is a string literal.
• Escape sequences: The \n indicates a newline character. It is an example of an escape sequence – a symbol used
to represent a special character in a text literal.
Here are all the C++ escape sequences which you can include in strings:
7. return 0 indicates that the program should tell the operating system it has completed successfully. This syntax will
be explained in the context of functions; for now, just include it as the last line in the main block.

Note that every statement ends with a semicolon (except preprocessor commands and blocks using {}). Forgetting
these semicolons is a common mistake among new C++ programmers.

Basic Language Features


So far our program doesn’t do very much. Let’s tweak it in various ways to demonstrate
some more interesting constructs.

Values and Statements

First, a few definitions:


• A statement is a unit of code that does something – a basic building block of a program.
• An expression is a statement that has a value – for instance, a number, a string, the sum of two numbers, etc. 4 + 2,
x - 1, and "Hello, world!\n" are all expressions.
Not every statement is an expression.
Operators
We can perform arithmetic calculations with operators. Operators act on expressions to form a new expression. For
example, we could replace "Hello, world!\n" with (4 + 2) / 3, which would cause the program to print the number 2.
In this case, the + operator acts on the expressions 4 and 2 (its operands).
Operator types:
• Mathematical: +, -, *, /, and parentheses have their usual mathematical meanings,
including using - for negation. % (the modulus operator) takes the remainder of two
numbers: 6 % 5 evaluates to 1.
• Logical: used for “and,” “or,” and so on.
• Bitwise: used to manipulate the binary representations of numbers.
Data Types
Here are the built-in datatypes we will use most often:
Variables
We might want to give a value a name so we can refer to it later. We do this using variables. A variable is a named
location in memory.
For example, say we wanted to use the value 4 + 2 multiple times. We might call it x and
use it as follows:
Input
Now that we know how to give names to values, we can have the user of the program input values. This is
demonstrated in line 6 below:

Just as cout << is the syntax for outputting values, cin >> (line 6) is the syntax for inputting values.

You might also like