C++ Programming: From Problem Analysis To Program Design,: Fourth Edition
C++ Programming: From Problem Analysis To Program Design,: Fourth Edition
C++ Programming: From Problem Analysis To Program Design,: Fourth Edition
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Slide 1- 44
The Basics of a C++ Program
• Function: collection of statements; when
executed, accomplishes something
− May be predefined or standard
• Syntax: The rules of grammar that must be
followed when writing a program (keywords,
operators, symbols, and punctuation)
• Programming language: a set of rules,
symbols, and special words
• Semantic rule: meaning of the instruction
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5
Structure of a C program
Statement = Command or
instruction
Parts of a C++ Program
comment
// sample C++ program preprocessor
#include <iostream> directive
which namespace
using namespace std; to use
int main() beginning of
function named main
{ beginning of
block for main
cout << "Hello, there!"; statement output
return 0;
string
} end of block literal
for main send 0 to
operating system
Comments
• Comment is a statement that is not executed.
• This helps reader and also yourself to understand your codes and
document parts of the program.
• Two types:
− Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.
− Multiple line
/*
You can include comments that can
occupy several lines.
*/
• Special symbols
+ ?
- ,
* <=
/ !=
. ==
; >=
Slide 1- 10
Reserved Words (Keywords)
• Valid OR Invalid ?
− A
− $a
− Student8
− 3name
− student_name
− person name
− _person_name
− int
− TRUE
− what?
− FALSE
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16
Whitespaces
• Examples:
-6728
0
78
+763
• Positive integers do not need a + sign
• No commas are used within an integer
− Commas are used for separating items in a list
1-22
bool Data Type
• bool type
− Two logical values: true and false
− Manipulate logical (Boolean) expressions
− bool variables are stored as small integers
− false is represented by 0, true by 1:
• bool, true, and false are reserved words
bool allDone = true;
bool finished = false;
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
1-29
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29
Scope
1-30
A Closer Look at the / Operator
1-31
A Closer Look at the % Operator
1-32
Order of Precedence
• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
− Entire expression is evaluated according to
precedence rules
type variable_name;
• int
− Integer variables hold signed whole numbers (i.e.
with no fractional part), such as 10, – 4353, etc.
• char
− character variables hold single characters, such as 'a', '\n', '
', etc.
• char (continued)
− ASCII (American Standard Code for Information
Exchange) is used to represent
• the characters A to Z (both upper and lower case)
• the digits 0 to 9
• special characters (e.g. @, <, etc)
• special control codes
− For example,
• the character 'A' is represented by the code 65
• the character '1' is represented by the code 49
• IMPORTANT: the integer 1, the character '1' and the ASCII
code 1 represent three different things!
Variable values
1-51
Variable values
• variable = value;
- Assign the value on the right hand side to the variable on the left hand side
- Expression is evaluated and its value is assigned to the variable on the left side
In C++, = is called the assignment operator
• Example
int num_students; // declare
num_students = 22; // initialize
Variable values
#include <stdio.h>
int main ()
{
int num_students;
num_students = 22;
return 0;
}
Example
#include <stdio.h>
int main ()
{
double rate, amount; /* declare two
double variables */
amount = 12.50;
rate = 0.05;
return 0;
}
Example
#include <stdio.h>
int main ()
{
char grade = ‘A’;
return 0;
}
Example
#include <stdio.h>
int main ()
{
char pass_grade = ‘A’, fail_grade = ‘F’;
return 0;
}
Increment & Decrement Operators