Professional Documents
Culture Documents
There are two variables (among others) defined in <iostream>. cout is used for
output, cin for input.
cout and cin are not key words in the C++ language. They are variables, instances of
classes, that have been declared in <iostream>. cout is a variable of
type ostream. cin is a variable of type istream.
« is sometimes called the output operator, sometimes the insertion operator (it inserts
something into the stream)
We don't need to tell the type of the data to be output, that happens automatically
(we'll find out how later)
int i = 7;
double d = 3.4;
cout << i;
cout << d;
cin >> d;
Note that address operators (&) are not needed here like they are with the standard C
input function scanf(). (And there was much rejoicing.)
The cin input stream can be chained as well, but usually isn't.
Note that output goes to cout, input comes from cin. It may be possible to get input
from cout or send output to cin depending on the library implementation, but it
shouldn't do anything useful.
In C++, a function is a group of statements that is given a name, and which can be called from some
point of the program. The most common syntax to define a function is:
Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an identifier, with
each parameter being separated from the next by a comma. Each parameter looks very much like a
regular variable declaration (for example:int x), and in fact acts within the function as a regular
variable which is local to the function. The purpose of parameters is to allow passing arguments to the
function from the location where it is called from.
- statements is the function's body. It is a block of statements surrounded by braces { } that specify
what the function actually does.
// function example
2 #include <iostream>
3 using namespace std;
4
5 int addition (int a, int
6 b)
7 {
8 int r;
9 r=a+b;
10 return r;
11 }
12
13 int main ()
14 {
15 int z;
16 z = addition (5,3);
17 cout << "The result is
" << z;
}
Conditional Statement:
When programming, you will ask the computer to check various kinds of situations and to
act accordingly. The computer performs various comparisons of various kinds of
statements. These statements come either from you or from the computer itself, while it
is processing internal assignments.
Let’s imagine you are writing an employment application and one question would
be, "Do you consider yourself a hot-tempered individual?" The source file of such a
program would look like this:
#include <iostream>
using namespace std;
int main()
{
char Answer;
return 0;
}
Some of the answers a user would type are y, yes, Y, Yes, YES, n, N, no, No, NO, I
don’t know, Sometimes, Why are you asking?, and What do you mean? The
variety of these different answers means that you should pay attention to how you
structure your programs, you should be clear to the users.
Loop:
There may be a situation, when you need to execute a block of code several
number of times. In general statements are executed sequentially: The first
statement in a function is executed first, followed by the second, and so on.
do...while loop Like a while statement, except that it tests the condition
at the end of the loop body
You can use one or more loop inside any another while,
nested loops
for or do..while loop.
int main ()
for( ; ; )
return 0;
Hello world:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
//Specification: Levi iLab 1 Exercise 2: Print User Name and Age
02
03
04 #include "stdafx.h"
05 #include <iostream>
06 char firstname = ' ';
07 char lastname = ' ';
08 int age = 0;
09
10 int main()
11 {
12 std::cout << "Enter your first name\n";
13 std::cout << "Enter your last name\n";
14 std::cout << "Enter your age\n";
15 std::cin >> firstname;
16 std::cin >> lastname;
17 std::cin >> age;
18 std::cout << "Your first name is:" << firstname << std::endl;
19 std::cout << "Your last name is:" << lastname << std::endl;
20 std::cout << "Your age is:" << age << std::endl;
21 return 0;
22 }