Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp variables #5499

Merged
merged 20 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions content/c-sharp/concepts/comments/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@ CatalogContent:

A **comment** is a piece of text within a program that is not executed. It can be used to provide additional information to aid in understanding the code.

### Single-line Comments
## Why Use Comments?

For single-line comments, the compiler ignores any text after two consecutive forward slashes (`//`) on the same line.
1. **Documentation**: Help document what specific parts of the code do, making it easier for others (or yourself) to understand when revisiting the code later.
2. **Debugging**: Temporarily disable code without removing it, making debugging easier.
3. **Clarity**: Improve the readability of the code, especially in complex sections.

```cs
// Comment goes here
executing code // Comment goes here
```
## Single-line Comments

### Multi-line Comments
A **single-line comment** is a comment that occupies a single line. It starts with two forward slashes (`//`), and everything after those slashes on that line is ignored by the compiler.

Multi-line comments begin with `/*` and end with `*/`. The compiler ignores any text in between.
### Example

```cs
/*
This is all commented out.
None of it is going to run!
*/
// This is a single-line comment
Console.WriteLine("This code will execute."); // Inline comment explaining the code

```

### Example
### Multi-line Comments

The following examples show various comment styles:
A **multi-line comment** is used for comments that span multiple lines. It begins with `/*` and ends with `*/`. The compiler ignores everything in between.

```cs
// This line will denote a comment in C-sharp.
Console.WriteLine("Hello World!"); // This is a comment.
/*
This is a multi-line
comment.
This is a multi-line comment.
It can span multiple lines.
*/
Console.WriteLine("This code will also execute.");
```

### XML Comments
Expand All @@ -57,7 +54,9 @@ comment.
The following is a single-line XML comment, which uses three forward slashes (`///`):

```cs
/// XML Comment goes here
/**
XML Comments go here
*/
```

Multi-line XML comments are similar to regular multi-line comments, except that an extra asterisk `*` is used in the opening:
Expand All @@ -78,3 +77,16 @@ XML tags embedded in XML comments are used to signal a specific functionality of
/// </summary>
public class MyClass {}
```

```cs
// Another XML comment example
/// <summary>
/// This method calculates the sum of two integers.
/// </summary>
/// <param name="a">The first integer</param>
/// <param name="b">The second integer</param>
/// <returns>The sum of a and b</returns>
public int Add(int a, int b) {
return a + b;
}
```
62 changes: 58 additions & 4 deletions content/cpp/concepts/variables/variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Variables'
Description: 'A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data. To create a variable, the type must be specified and a value must be assigned to it.'
Description: 'A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data. Variables are fundamental in programming as they enable dynamic data handling.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce the description to not more than 160 characters

Subjects:
- 'Computer Science'
- 'Game Development'
Expand All @@ -12,11 +12,11 @@ CatalogContent:
- 'paths/computer-science'
---

A **variable** refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.
A **variable** refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data. Variables act as containers for storing information that can be changed or updated during the execution of a program.

## Declare a Variable

To create a variable, the type must be specified and a value must be assigned to it:
To declare a variable, the type of data the variable will hold must be specified, followed by the variable name. Optionally, a value can be assigned to the variable at the time of declaration:

```pseudo
type name = value;
Expand Down Expand Up @@ -46,7 +46,41 @@ The output would be:
I am 30 years old.
```

## Codebyte Example
## Data Types in Variables

Each variable in programming has a type, which defines the kind of data it can hold. Here are some common data types:

- **int**: for integers (whole numbers), e.g.,

```cpp
int score = 10;
```

- **float**: A single-precision floating-point number, typically occupying 4 bytes (32 bits). It offers less precision and is used when memory efficiency is more important than accuracy, e.g.,

```cpp
float pi = 3.14159;
```

- **double**: A double-precision floating-point number, typically occupying 8 bytes (64 bits). It provides more precision and is the default choice when you need to store decimal numbers in C++.

```cpp
double pi = 3.14159265358979323846;
```

- **char**: A single character, e.g.,

```cpp
char letter = 'A';
```

- **bool**: for boolean values (true or false), e.g.,

```cpp
bool isAdmin = true;
```

## Codebyte Example 1

```codebyte/cpp
#include <iostream>
Expand All @@ -56,3 +90,23 @@ int main() {
std::cout << "Tip is" << tip;
}
```

## Codebyte Example 2

```codebyte/cpp
#include <iostream>

int main() {
int age = 25;
double temperature = 36.6;
char initial = 'R';
bool isSunny = true;

std::cout << "Age: " << age << std::endl;
std::cout << "Temperature: " << temperature << "°C" << std::endl;
std::cout << "Initial: " << initial << std::endl;
std::cout << "Is it sunny? " << (isSunny ? "Yes" : "No") << std::endl;

return 0;

```
16 changes: 6 additions & 10 deletions content/javascript/concepts/modules/modules.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Modules'
Description: 'As the program grows bigger, it may contain many lines of code. Instead of putting everything in a single file, modules can be used to separate codes in separate files as per their functionality. This makes the code more organized and easier to maintain. A module is a file that contains code that performs a specific task. A module may contain variables, functions, classes, etc. Suppose, a file named greetPerson.js contains the following code: js // Exporting a function export function greetPerson(name) { return Hi, ${name};'
Description: 'Modules help keep code organized by putting related code, like functions, classes, and variables, into separate files. This makes it easier to manage and reuse in larger projects.'
Subjects:
- 'Web Development'
- 'Computer Science'
Expand All @@ -11,11 +11,9 @@ CatalogContent:
- 'paths/front-end-engineer-career-path'
---

As the program grows bigger, it may contain many lines of code. Instead of putting everything in a single file, modules can be used to separate codes in separate files as per their functionality. This makes the code more organized and easier to maintain.
Modules split large programs into separate files based on tasks, containing related functions, variables, or classes for easier management and reuse.

A module is a file that contains code that performs a specific task. A module may contain variables, functions, classes, etc.

Suppose, a file named **greetPerson.js** contains the following code:
Suppose a file named **greetPerson.js** contains the following code:

```js
// Exporting a function
Expand Down Expand Up @@ -87,9 +85,7 @@ Here, both the `name` variable and the `difference()` function from the **module

## Renaming Imports and Exports

If the objects (variables, functions, etc.) that you want to import are already present in your main file, the program may not behave as you want. In this case, the program takes value from the main file instead of the imported file.

To avoid naming conflicts, you can rename these objects during the export or during the import.
To prevent naming conflicts, rename variables or functions when exporting or importing. This ensures the program uses the correct values from the intended file.

### Rename in the export file (the module)

Expand All @@ -103,7 +99,7 @@ export { function1 as newName1, function2 as newName2 };
import { newName1, newName2 } from './module.js';
```

Here, while exporting the function from **module.js** file, new names (here, `newName1` & `newName2`) are given to the function. Hence, when importing that function, the new name is used to reference that function.
When exporting from module.js, new names (newName1, newName2) are assigned to functions. The new names must be used when importing and referencing them.

### Rename in the import file (the main file)

Expand All @@ -117,7 +113,7 @@ export { function1, function2 };
import { function1 as newName1, function2 as newName2 } from './module.js';
```

Here, while importing the function, the new names (here, `newName1` & `newName2`) are used for the function name. Now you use the new names to reference these functions.
When importing the function, new names (`newName1`, `newName2`) are used. These new names are then used to reference the functions in the code.

## Default Export

Expand Down
Loading