You are on page 1of 6

In C++, I/O is done through classes and functions found in <iostream>.

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.

C++ allows us to change the meaning of standard operators in various situations.


(We'll spend a lot of time on this later.) For now, the standard shifting
operators « and » have been overloaded for use in I/O.
cout << "Hello world!\n";

« 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;

Output operations can be chained:


cout << i << " " << d << "\n";

The input operator (or extraction operator) «


cin >> i;

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:

type name ( parameter1, parameter2, ...) { statements }

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;

cout << "Do you consider yourself a hot-tempered individual? ";


cin >> 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.

Programming languages provide various control structures that allow for


more complicated execution paths.

A loop statement allows us to execute a statement or group of statements


multiple times and following is the general from of a loop statement in most
of the programming languages:
C++ programming language provides the following types of loop to handle
looping requirements. Click the following links to check their detail.

Loop Type Description

while loop Repeats a statement or group of statements while a


given condition is true. It tests the condition before
executing the loop body.

for loop Execute a sequence of statements multiple times and


abbreviates the code that manages the loop variable.

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.

The Infinite Loop:


A loop becomes infinite loop if a condition never becomes false.
The for loop is traditionally used for this purpose. Since none of the three
expressions that form the for loop are required, you can make an endless
loop by leaving the conditional expression empty.
#include <iostream>

using namespace std;

int main ()

for( ; ; )

printf("This loop will run forever.\n");

return 0;

When the conditional expression is absent, it is assumed to be true. You


may have an initialization and increment expression, but C++ programmers
more commonly use the for(;;) construct to signify an infinite loop.

NOTE: You can terminate an infinite loop by pressing Ctrl + C keys.

Hello world:

// A hello world program in C++

#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 }

You might also like