C++ Tutorials For Complete Beginners - Output (Print Text) in C++ - Blog Base Media

C++ Tutorials For Complete Beginners - Output (Print Text) in C++ - Blog Base Media

Output (Print Text) in C++

Indeed! The `cout` object, accompanied by the `<<` operator, is employed to showcase values or print text in C++.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}          

Certainly! You have the freedom to include as many `cout` objects as needed in your code. However, it's important to remember that by default, the `cout` object does not automatically add a new line at the end of the output.

#include <iostream>           
using namespace std;          

int main() {                  
  cout << "Hello World!";     
  cout << "I am learning C++";
  return 0;                   
}                             

New Lines in C++ 

 To insert a new line at the end of the output in C++, you can utilize the `\n` character.

Examples:

#include <iostream>           
using namespace std;          

int main() {                  
  cout << "Hello World! \n";  
  cout << "I am learning C++";
  return 0;                   
}                             

Pro Tips: Absolutely! When you use consecutive `\n` characters in C++, it creates a blank line, resulting in an empty line in the output.

Example

#include <iostream>           
using namespace std;          

int main() {                  
  cout << "Hello World! \n\n";
  cout << "I am learning C++";
  return 0;                   
}                             

Run the the code above in your compiler and see the result.

An alternative method to insert a new line in C++ is by utilizing the endl manipulator. By using endl, a new line is added.

Example:

#include <iostream>              
using namespace std;             

int main() {                     
  cout << "Hello World!" << endl;
  cout << "I am learning C++";   
  return 0;                      
}                                

Certainly! In C++, both `\n` and `endl` are employed to create line breaks. However, `\n` is more commonly used. It represents the newline character, an escape sequence that shifts the cursor to the beginning of the next line, resulting in a new line.


Certainly! Here are a few other valid escape sequences used in C++:

- `\\` is an escape sequence that represents a backslash.
- `\"` is an escape sequence that represents a double quotation mark.

Comments in C++ 

Comments in C++ are utilized to clarify code and enhance its readability. They can also be employed to disable the execution of certain code segments during testing or debugging. Comments come in two forms: single-line comments and multi-line comments.

Single-line Comments in C++

In C++, single-line comments are denoted by two forward slashes (//). Any text appearing after the // and extending to the end of the line is ignored by the compiler and will not be executed.

Here's an example illustrating the usage of a single-line comment preceding a line of code:

// This is a comment   
cout << "Hello World!";

In C++, it is possible to add a single-line comment at the end of a line of code by using // followed by the comment text. The comment will extend from // to the end of the line, allowing you to provide additional explanations or context for that particular line of code.

Example

cout << "Hello World welcome to blog base media!"; // This is a comment

Multi-line Comments in C++ 

In C++, multi-line comments are indicated by /* to begin the comment block and */ to end it. Any text enclosed between these delimiters will be disregarded by the compiler and won't be executed.

Example

/* The code below will print the words Hello World!
to the screen, and it is amazing */ 
              

cout << "Hello World this is blog base media!";                            

When it comes to using comments in C++, the decision of whether to use single-line (//) or multi-line (/* */) comments is entirely up to you. Typically, single-line comments are suitable for shorter explanations, while multi-line comments are more appropriate for longer comments or blocks of text. The choice depends on personal preference and the length of the comment.

Variables in C++

In C++, variables act as containers to hold different kinds of information, such as whole numbers, decimal numbers, individual characters, text, or boolean (true/false) values.
  • In C++, the `int` data type is used to store whole numbers (integers) without any decimals. Examples of `int` values include 123, -123, 0, and so on.
  • In C++, the `double` data type is used to store floating-point numbers, which are numbers that can have decimal places. Examples of `double` values include 19.99, -19.99, 3.14159, and so on.
  • In C++, the `char` data type is used to store single characters. Each `char` value is surrounded by single quotes, such as 'a', 'B', '@', or '5'. It can represent alphabets, digits, special characters, or any other single character.
  • In C++, the `string` data type is used to store sequences of characters, which represent text. String values are surrounded by double quotes, such as "Hello World", "Blogbasemedia", or "12345". It allows you to manipulate and work with textual data in your program.
  • In C++, the `bool` data type is used to store values that have two states: `true` or `false`. It is typically used for logical operations and conditions. A `bool` variable can hold either the value `true` or `false`. For example, you can use a `bool` variable to represent the state of a condition, such as whether a condition is true or false.

Declaring (Creating) Variables in C++

To create a variable in C++, you need to specify its type and assign it a value. Here's the general syntax:

Syntax

type variableName = value;

In C++, the type represents one of the C++ data types like int, double, char, string, or bool. The variableName is the name you choose for the variable, following the rules of C++ variable naming. The equal sign (=) is used to assign a value to the variable at the time of creation.
To create a variable that can store a number, you can choose an appropriate numeric data type like int or double. Here's an example:

Here's a code to create a variable called myNum of type int and assign it the value 10:

int myNum = 10;   
cout << myNum;    

In C++, you can declare a variable without assigning a value to it initially, and then assign a value to it later in your code. Here's an example:

int myNum;        
myNum = 10;       
cout << myNum;    

When you assign a new value to a variable in C++, it replaces and overrides the previous value stored in that variable.

Example:

int myNum = 10;  // myNum is 10
myNum = 30;  // Now myNum is 10
cout << myNum;  // Outputs 30  

Other Data Types in C++

Here's an example that demonstrates the usage of different data types in C++:

int myNum = 5;               // Integer (whole number without decimals)
double myFloatNum = 5.99;    // Floating point number (with decimals)  
char myLetter = 'D';         // Character                              
string myText = "Hello";     // String (text)                          
bool myBoolean = true;       // Boolean (true or false)                

In the Data Types chapter, you will explore each individual type in-depth, gaining a comprehensive understanding of their characteristics and usage.

Display Variables in C++

In C++, the `cout` object and the `<<` operator are used together to display variables, allowing you to combine text and variable values by separating them with the `<<` operator.

Example

int myAge = 35;                           
cout << "I am " << myAge << " years old.";

Adding Variables Together in C++

In C++, you can use the `+` operator to add or concatenate variables together.

Example

int x = 5;      
int y = 6;      
int sum = x + y;
cout << sum;    

Some C++ Exercises to try

Exercise:

Let's create a variable called `myNum` and set its value to `100`.

Declaring Multiple Variables C++

 To declare multiple variables of the same type, separate them with commas.

Example

int x = 7, y = 4, z = 70
cout << x + y + z;        

Assigning a single value to multiple variables.

You can assign a single value to multiple variables simultaneously in one line.

Example

int x, y, z;
x = y = z = 70;
cout << x + y + z;


NEXT >>>  Identifiers in C++

Post a Comment

0 Comments