Output in c++ (cout)

185 views 9 slides Sep 04, 2016
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

How to print something on the c++ console.


Slide Content

Output in c++

Output is the things that appears on the console screen What do you think we do in order to print something on the screen?

cout << ; A key word that hast t o be used in stuff on the screen This is called a semicolon and is used to indicate the end of the statement In this space between (cout) and (;) we write the thing that we want to print

Let’s try these codes: cout << 3 ; cout << 3+4; cout << 3*4; cout << 4/2; cout << 4-3; Note: I can print more than one thing with 1 cout cout << 3 << 6 << 2; Result : 362

If we want to print a word or a sentence we have t o put it inside “ “ (double quotes). cout << “Ghada”; cout << “Hello World!”; If I want to print a single character we have t o put it inside ’ ‘ (single quotes). cout << ‘a’ ; cout << ‘A’;

We know that if I said : cout << “ Hello” <<“world” ; The result printed on screen will be :Helloworld; What if I want to make it (Hello world) with a space between the 2 words?? Answer: cout << “Hello” << “ “ << “world” ;

Anything in a “” will be printed as it is, so when I put a space between “” it will be printed as a single space between the 2 words cout << “Hello” << “_” << “ world ”; Result : Hello_world cout << “….” << “Hello” << “….” << “ world ” << “….” ; Result : …Hello…world…

cout << 3 + 4 ; cout << “ 3 + 4 ” ; Result: Result: 7 3 + 4 The operation is put inside “” so it is not treated as an operation, it is treated as a sentence which needs to be printed as it is. A normal operation. So the value is calculated and printed on the screen.

cout << “This is the first line”; cout << “This is the second line”; Try these 2 lines of code and see what the output will look like. Result: This is the first lineThis is the second line What if we want to print each statement in a separate line?? cout << “This is the first line” << endl; cout << “This is the second line”; Result: This is the first line This is the second line Prints a new line after printing the first statement
Tags