Академический Документы
Профессиональный Документы
Культура Документы
#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.
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
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";
return 0;
}
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;
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
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.
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: