A Hello world example
The usual way to make programs in C++ is open an editor, write some program, like the one belows:
which includes the required main()
function, compile the program and run it.
Literal programming with C++
In our parading of literal programming, we just mush open the notebook with the proper kernel, in this case the C++ implementation of the ROOT kernel. In the implementation of this kernel the declaration of the main function is no longer required. Therefore, we can focus in learn the concepts of the language without compilation issues. In this way, the equivalent "Hello world program is just"
We can assume which we are inside main function
New Line
The cout operator does not insert a line break at the end of the output. One way to print two lines is to use the endl manipulator, which will put in a line break.
New Lines
The new line character \n can be used as an alternative to endl. The backslash () is called an escape character, and indicates a "special" character.
New Lines
Two newline characters placed together result in a blank line.
Multiple New Lines
Using a single cout statement with as many instances of \n as your program requires will print out multiple lines of text.
In python could see something like
As in any other kernel, all the defined in the evaluation cells can be used later. Also ROOT is a full data analysis system so that the visualization tools are avalaible and ready to be used. We don not cover ROOT usage here.
Printing Text
Comments
You can add comments to explain what is doing the code. The compilator will ignore all in a comment.
To do a comment in C++ begin the line writing , then write the comment; for example, if we want write that a function prints "Hello world", we do:
When above code is compiled the output is like:
Take care! ###
This is only for one-line comments, if we want a multi-line comment begin writing , write the comment and then we close with . The comment can be span multiples lines not only one.
Its very important close the multi-line comment, compare these two codes:
If we write a comment with // in a comment with /* / or a comment whit / */ in a comment whit //, so we can "nest" comments.
Variables
Working with variables
You have the option to assign a value to the variable at the the time you declare it and assign a value later. You can also change the value of a variable
Copy and paste the code in an execution cell replacing the blanks to declare variable c
of type int
and then assign 7 as its value
WARNING: not yet implemented here
Copy and paste the code in an execution cell replacing the blanks to to declare sum as a variable, assign it the value 21+7, and print out its value.