Вы находитесь на странице: 1из 38

Review

#include <iostream>
Lines beginning with a hash sign (#) are directives read and
interpreted by what is known as the preprocessor. They are
special lines interpreted before the compilation of the program
itself begins. In this case, the directive #include <iostream>,
instructs the preprocessor to include a section of standard C++
code, known as header iostream, that allows to perform
standard input and output operations, such as writing the output
of the program to the screen.
Review
Comments
Comments do not affect the operation of the program; however,
they provide an important tool to document directly within the
source code what the program does and how it operates.

C++ supports two ways of commenting code:


 // line comment
 /* block comment */

The first of them, known as line comment, discards everything


from where the pair of slash signs (//) are found up to the end of
that same line. The second one, known as block comment,
discards everything between the /* characters and the first
appearance of the */ characters, with the possibility of including
multiple lines.
Review
int main ()
This line initiates the declaration of a function. Essentially, a
function is a group of code statements which are given a name:
in this case, this gives the name "main" to the group of code
statements that follow. A name (main) and a pair of parentheses
(()), optionally including parameters.

The function named main is a special function in all C++


programs; it is the function called when the program is run. The
execution of all C++ programs begins with the main function,
regardless of where the function is actually located within the
code.
Review
{ and }
The open brace ({ ) indicates the beginning of main's function
definition, and the closing brace (}), indicates its end. Everything
between these braces is the function's body that defines what
happens when main is called. All functions use braces to indicate
the beginning and end of their definitions.
{ and } are also used to indicate the beginning and end of a
compound statement.

A blank line
Blank lines have no effect on a program. They simply improve
readability of the code.
Review
std::cout << "Hello World!";
This line is a C++ statement. A statement is an expression that can
actually produce some effect. Statements are executed in the same
order that they appear within a function's body.
This statement has three parts:
•std::cout, which identifies the standard character output device
(usually, this is the computer screen).
•The insertion operator (<<), which indicates that what follows is
inserted into std::cout.
•Finally, a sentence within quotes ("Hello world!"), is the content
inserted into the standard output.

Notice that the statement ends with a semicolon (;). This character
marks the end of the statement, just as the period ends a sentence in
English. All C++ statements must end with a semicolon character. One
of the most common syntax errors in C++ is forgetting to end a
statement with a semicolon.
Review

• Built-in data types


• Integer data type- an integer is an integral whole number without a
decimal point. These numbers are used for counting.
• Floating point data type- a floating point number has a decimal
point. Even if it has an integral value, it must include a decimal point
at the end.
• Void data type- actually refers to an object that does not have a
value of any type.
• Char data type- it is used to store value in the identifier
(variable/operand)
• Boolean data type- having two values (usually denoted true of false),
intended to represent the truth values of logic and Boolean algebra.
Review
• Derived data types
• Array- set of consecutive memory locations used to
store data.
• Function- functions allow us to group commonly used
code into a compact unit that can be used repeatedly.
• Pointers- a variable that stores the address of
another variable. It is used to allocate memory
dynamically.
• Reference- another variable type that act as an alias
or short name to another variable.
Review
Operators
• Arithmetic Operator (+, -, *, /, %)
used for arithmetic calculations
• Assignment Operator (=)
to assign value to any variable
• Relational Operator (==, !=, >, >=, <, <=)
to compare the value of two variables
• Logical Operator (&&, ‖, !)
to perform logical operations on the given two variables
• Increment and Decrement (++, --)
increment- increase the value of its operand by 1
decrement- decrease the value of its operand by 1
Review
Operators
• Bitwise Operator (&, |, ^)
perform manipulations of data at bit level
• Conditional/Ternary Operator
a shortcut for an if-else statement
• Comma Operator (,)
separators
• Shorthand Operator (+=, -=, *=, /=, %=, &&=, ‖=)
simplifies coding of certain type of assignment
• Cast Operator
to convert a value from one to another type
Review
Control structure
Conditional statements
• if statement
• if-else statement
• else if statement
• nested if-else statement
• switch statement
Iterative or looping statements
• for loop
• while loop
• do-while loop
if statement
it is used to execute an instruction or sequence/block
of instruction only if condition is fulfilled
Syntax: if (condition)
statement;

if (condition) if (condition)
simple statement; {
compound / block of
statement;
}
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"\nEnter an number : ";
cin>>num;
 
if (num > 10)
cout<<"\nYou entered "<<num<<" which is greater than 10";

if (num < 10)


cout<<"\nYou entered "<<num<<" which is less than 10";

return 0;
}

Output: Enter an integer : 8

You entered 8 which is less than 10


if- else
if-else statement is used when different block
of statements is to be executed on condition
true of false
Syntax:

if (condition)
statement; //if statement- executed when condition is true
else
statement; //else statement- executed when condition is false
if (condition)
if (condition) {
simple statement; compound statement;
else }
simple statement; else
{
compound statement;
}
#include <iostream>
using namespace std;
int main()
{
int num = 0;
cout<<"\nEnter an integer : ";
cin>>num;
 
if (number > 10)
cout<<"\nYou entered "<<num<<" which is greater than 10\n";
else
cout<<"\nYou entered "<<num<<" which is less than 10\n";

return 0;
}

Enter an integer : 8
Output:
You entered 8 which is less than 10
else if
to show a multi-way decision based on several conditions

Syntax:
if (condition_1)
{
block 1 statement;
}
else if (condition_2)
{
block 2 statement;
}
else
{
block n statement;
}
#include <iostream>
using namespace std;
int main ()
{
int score;
  Output:
cout<<"Enter score of student : ";
cin>>score;
  Enter score of student : 80
if(score <= 50)
cout<<"\nGrade D"; //50 and below
else if (mark <= 60)
Grade A
cout<<"\nGrade C"; //51-60
else if (mark <= 70)
cout<<"\nGrade B"; //61-70
else
cout<<"\nGrade A"; //above 70
 
return 0;
}
nested if-else
You can combine multiple if/if-else/if-else-if
ladders when a series of decision are
involved.
if (condition)
{
Syntax: statement;
}
else
{
if (condition)
{
statement;
}
else
{
statement;
}
}
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout<<"Enter two integers : ";
Output:
cin>>n1>>n2;

if(n1==n2) Enter two integers : 7 9


cout<<"\nResult: "<<n1<<" =" <<n2;
else Result: 9 > 7
if(n1>n2)
cout<<"\nResult: "<<n1<<" > "<<n2;
else
cout<<"\nResult: "<<n2<<" > “<<n1;

return 0;
}
switch case
It is also used when we need our
program to make a certain decision
based on a condition and then
execute accordingly.
switch (variable)
{
Syntax: case a:
statement;
break;
case b:
statement;
break;
case n:
statement;
break;
default:
statement;
}
#include <iostream> cout<<"Play game called";
using namespace std; break;
int main() case 2:
{ cout<<"Load game called";
int choice; break;
case 3:
cout<<"1. Play game\n"; cout<<"Play Multi-player game
cout<<"2. Load game\n"; called";
cout<<"3. Play multi-player\n"; break;
cout<<"4. Exit\n"; case 4:
cout<<"Selection: "; cout<<"Thanks for playing!\n";
cin>>choice; break;
cout<<"\n"; default:
cout<<"Bad input, quitting!\n";
switch (choice) }
{ return 0;
case 1: //Note- colon, not a semicolon }
Output:
1. Play game
2. Load game
3. Play muti-player
4. Exit
Selection: 1

Play game called


for loop
used to repeat a block of code.
Syntax:
for (initialization; condition; increment/decrement)
{
statement / loop body;
}
#include <iostream>
using namespace std;
int main()
{
int i;
Output:
for(i = 0; i < 10 ; i++)
cout<<i<<" ";
0123456789
return 0;
}
while loop
repeatedly executes a target statement
as long as a given condition is true
Syntax:
while (condition)
{
statement / loop body;
}
#include <iostream>
using namespace std;
int main ()
{
// local variable definition
int a = 1;
 
// while loop execution
while( a < 5 )
{
//loops comes inside this body, until condition is true
cout<<"Value of a: "<<a<<"\n";
a++;
}
 
return 0;
}

Value of a: 1
Value of a: 2
Output: Value of a: 3
Value of a: 4
do while loop
it is similar to a while loop, except that a do-
while loop is guaranteed to execute at least one
time. The conditional expression appears at the
end of the loop, so the statement/s in the loop
execute before the condition is tested.
Syntax: do
{
statement / loop body;
} while (condition);
#include <iostream>
using namespace std;
int main ()
{
// declared local operand (variable) Output:
int a = 1;
 
// do-while loop Value of a: 1
do Value of a: 2
{ Value of a: 3
cout<<"value of a: "<<a<<"\n"; Value of a: 4
a = a + 1;
} while( a < 5 );
 
return 0;
}
Input/Output
– cin- makes the program wait for input from cin.
– cout
• cout << “this is an output.”;
//prints this is an output sentence on the screen
• cout << x; -
//prints the value of x in the screen
• cout << “I am ” << age << “years old and my zip code is
”<<zipcode;
Functions
• Functions allow us to group commonly
used code into a compact unit that can
be used repeatedly.
• A function is a group of statements that
together perform a task.
Classes and objects
• Classes- it is a group of entities that share
a common properties, operations, and
behavior.
• Object- a class represents a set of object
that share a common structure and a
common behavior, whereas an object is
an instance of a class.
Arrays
An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.

That means that, for example, five values of type int can


be declared as an array without having to declare 5
different variables (each with its own identifier). Instead,
using an array, the five int values are stored in contiguous
memory locations, and all five can be accessed using the
same identifier, with the proper index.
For example, an array containing 5 integer values of
type int called N could be represented as:

N
[0] [1] [2] [3] [4] int
where each blank panel represents an element of the
array. In this case, these are values of type int. These
elements are numbered from 0 to 4, being 0 the first and
4 the last; In C++, the first element in an array is always
numbered with a zero (not a one), no matter its length.
Like a regular variable, an array must be declared
before it is used. A typical declaration for an array in C+
+ is:

type name [elements];

where type is a valid type (such as int, float...), name is


a valid identifier and the elements field (which is always
enclosed in square brackets []), specifies the length of
the array in terms of the number of elements.

Therefore, the N array, with five elements of type int,


can be declared as:
int N[5];
• NOTE: The elements field within square
brackets [], representing the number of
elements in the array, must be a constant
expression, since arrays are blocks of static
memory whose size must be determined at
compile time, before the program runs.
Initializing arrays
By default, regular arrays of local scope (for example,
those declared within a function) are left uninitialized.
This means that none of its elements are set to any
particular value; their contents are undetermined at the
point the array is declared.

But the elements in an array can be explicitly initialized to


specific values when it is declared, by enclosing those
initial values in braces {}. For example: 
int N[5] = { 26, 6, 28, 4, 27};
This statement declares an array that can be represented
like this: 26 6 28 4 27
N
[0] [1] [2] [3] [4]

Вам также может понравиться