From 9dc48bf6c91fc5ae39f38f356771f48b8aae33ac Mon Sep 17 00:00:00 2001 From: Ajay-Dhangar Date: Tue, 12 Nov 2024 09:08:41 +0530 Subject: [PATCH] Added more docs with better way --- docs/extra/_category_.json | 8 + docs/programming-fundamentals/Arrays.md | 147 -------- .../language-syntax/conditionals.md | 319 ++++++++++++++++++ .../language-syntax/functions.md | 227 +++++++++++++ .../language-syntax/loops.md | 206 +++++++++++ 5 files changed, 760 insertions(+), 147 deletions(-) create mode 100644 docs/extra/_category_.json delete mode 100644 docs/programming-fundamentals/Arrays.md create mode 100644 docs/programming-fundamentals/language-syntax/conditionals.md create mode 100644 docs/programming-fundamentals/language-syntax/functions.md create mode 100644 docs/programming-fundamentals/language-syntax/loops.md diff --git a/docs/extra/_category_.json b/docs/extra/_category_.json new file mode 100644 index 000000000..ad0c282ff --- /dev/null +++ b/docs/extra/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Extra", + "position": 99, + "link": { + "type": "generated-index", + "description": "Learn about Programming Fundamentals. This is the first step to become a good programmer." + } +} \ No newline at end of file diff --git a/docs/programming-fundamentals/Arrays.md b/docs/programming-fundamentals/Arrays.md deleted file mode 100644 index 8e4f08e32..000000000 --- a/docs/programming-fundamentals/Arrays.md +++ /dev/null @@ -1,147 +0,0 @@ ---- -id: Arrays -title: Introduction to Arrays fundamentals -sidebar_label: Arrays -sidebar_position: 1 -description: "Information About Arrays in progamming" -tags: [arrays,fundamentals] ---- - - -# Arrays in C - -## What is an Array? -An array is a collection of elements of the same type, stored in contiguous memory locations. It allows you to store multiple values in a single variable, making it easier to manage and access data efficiently. - -## Characteristics of Arrays -- **Homogeneous**: All elements in an array must be of the same data type. -- **Fixed Size**: The size of an array is defined at the time of declaration and cannot be changed during runtime. -- **Contiguous Memory**: The elements of an array are stored in consecutive memory locations. - -## Declaration of Arrays -To declare an array in C, you specify the data type followed by the array name and size in square brackets. - -### Syntax: -```c -data_type array_name[array_size]; -``` -### Example: -```C -int numbers[5]; // Declaration of an integer array of size 5 -``` - -## Initialization of Arrays -You can initialize an array at the time of declaration using curly braces. - -### Syntax: -```C -data_type array_name[array_size] = {value1, value2, ..., valueN}; -``` - -### Example: -```C -int numbers[5] = {1, 2, 3, 4, 5}; // Initialization with values -``` -If the size is omitted, the compiler counts the number of initializers: - -```C -int numbers[] = {1, 2, 3, 4, 5}; // Compiler determines the size (5 in this case) -``` - -## Accessing Array Elements -Array elements can be accessed using the index, which starts from 0. - -### Syntax: -```C -array_name[index]; -``` - -### Example: -```C -int firstElement = numbers[0]; // Accessing the first element (1) -``` - -## Modifying Array Elements -You can modify an element of the array using its index. - -### Example: -```C -numbers[0] = 10; // Changing the first element from 1 to 10 -``` - -## Multidimensional Arrays -C also supports multidimensional arrays, such as two-dimensional arrays (like matrices). - -### Declaration: -```C -data_type array_name[row_size][column_size]; -``` - -#### Example: -```C -int matrix[3][3]; // Declaration of a 3x3 integer matrix -``` - -### Initialization: -```C -int matrix[3][3] = { - {1, 2, 3}, - {4, 5, 6}, - {7, 8, 9} -}; // Initialization of a 3x3 matrix -``` - -### Accessing Elements: -```C -int element = matrix[1][2]; // Accessing the element in the second row and third column (6) -``` - -## Common Operations on Arrays -1. Traversing an Array -You can iterate through an array using a loop to access or modify each element. - -``` -for (int i = 0; i < 5; i++) { - printf("%d ", numbers[i]); // Prints each element -} -``` - -2. Searching an Array -You can search for an element using linear or binary search. - -Linear Search Example: -```C -int search(int arr[], int size, int key) { - for (int i = 0; i < size; i++) { - if (arr[i] == key) { - return i; // Return the index if found - } - } - return -1; // Return -1 if not found -} -``` - -3. Sorting an Array -You can sort an array using various algorithms like bubble sort, selection sort, or quicksort. - -Bubble Sort Example: -```C -void bubbleSort(int arr[], int size) { - for (int i = 0; i < size - 1; i++) { - for (int j = 0; j < size - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - // Swap arr[j] and arr[j+1] - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } -} -``` - -## Summary -- An array is a collection of elements of the same type stored in contiguous memory locations. -- Arrays can be one-dimensional or multi-dimensional. -- Elements can be accessed and modified using their index. -- Common operations on arrays include traversing, searching, and sorting. diff --git a/docs/programming-fundamentals/language-syntax/conditionals.md b/docs/programming-fundamentals/language-syntax/conditionals.md new file mode 100644 index 000000000..4865725eb --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/conditionals.md @@ -0,0 +1,319 @@ +--- +id: conditionals +sidebar_position: 5 +title: Conditionals +sidebar_label: Conditionals +description: "Master conditional statements in JavaScript, Java, Python, and C++. Learn how to use if-else, switch statements, and ternary operators for decision-making in programming." +tags: [conditionals, if-else, switch, programming, syntax, js, java, python, cpp] +--- + +Conditionals allow you to execute different blocks of code based on certain conditions. They are essential for decision-making in programming. This guide will cover the common types of conditional statements and provide detailed examples across multiple languages. + + + +## Types of Conditional Statements + +1. **If-Else**: Executes a block of code if a condition is true; otherwise, it executes another block. +2. **Else-If Ladder**: Used when there are multiple conditions to check. +3. **Switch Case**: A more efficient way to handle multiple conditions based on the value of a single variable. +4. **Ternary Operator**: A shorthand way to write simple `if-else` statements. + +## Conditional Statements in Different Languages + + + + +### JavaScript Conditionals Overview + +In JavaScript, you can use `if-else`, `else-if` ladder, `switch` case, and the ternary operator for conditional statements. + +#### 1. If-Else Statement + +`if-else` statements are used to execute a block of code based on a condition. If the condition is true, the code inside the `if` block is executed; otherwise, the code inside the `else` block is executed. + +```js title="If-Else Statement in JavaScript" +let number = 10; + +if (number > 5) { + console.log("Number is greater than 5"); +} else { + console.log("Number is 5 or less"); +} +``` + +#### 2. Else-If Ladder + +An `else-if` ladder is used when you have multiple conditions to check. The code block associated with the first true condition is executed. + +```js title="Else-If Ladder in JavaScript" +let grade = 85; + +if (grade >= 90) { + console.log("A"); +} else if (grade >= 80) { + console.log("B"); +} else if (grade >= 70) { + console.log("C"); +} else { + console.log("Fail"); +} +``` + +#### 3. Switch Case + +`switch` statements are used to select one of many code blocks to be executed. It is more efficient than multiple `else-if` statements when you have multiple conditions based on the value of a single variable. + +```js title="Switch Case in JavaScript" +let day = "Monday"; + +switch (day) { + case "Monday": + console.log("Start of the work week"); + break; + case "Friday": + console.log("End of the work week"); + break; + default: + console.log("Midweek days"); +} +``` + +#### 4. Ternary Operator + +The ternary operator is a shorthand way to write simple `if-else` statements. It consists of a condition followed by a `?` and two expressions separated by `:`. If the condition is true, the first expression is executed; otherwise, the second expression is executed. + +```js title="Ternary Operator in JavaScript" +let age = 18; +let canVote = age >= 18 ? "Yes" : "No"; +console.log(`Can vote: ${canVote}`); // Output: Can vote: Yes +``` + + + + + +### Java Conditionals Overview + +Java supports `if-else`, `else-if` ladder, `switch` case, and the ternary operator for conditional statements. + +#### 1. If-Else Statement + +`if-else` statements in Java are used to execute a block of code based on a condition. If the condition is true, the code inside the `if` block is executed; otherwise, the code inside the `else` block is executed. + +```java title="If-Else Statement in Java" +int number = 10; + +if (number > 5) { + System.out.println("Number is greater than 5"); +} else { + System.out.println("Number is 5 or less"); +} +``` + +#### 2. Else-If Ladder + +An `else-if` ladder is used when you have multiple conditions to check. The code block associated with the first true condition is executed. + +```java title="Else-If Ladder in Java" +int grade = 85; + +if (grade >= 90) { + System.out.println("A"); +} else if (grade >= 80) { + System.out.println("B"); +} else if (grade >= 70) { + System.out.println("C"); +} else { + System.out.println("Fail"); +} +``` + +#### 3. Switch Case + +`switch` statements in Java are used to select one of many code blocks to be executed based on the value of a variable. + +```java title="Switch Case in Java" +String day = "Monday"; + +switch (day) { + case "Monday": + System.out.println("Start of the work week"); + break; + case "Friday": + System.out.println("End of the work week"); + break; + default: + System.out.println("Midweek days"); + break; +} +``` + +#### 4. Ternary Operator + +The ternary operator is a shorthand way to write simple `if-else` statements in Java. It consists of a condition followed by a `?` and two expressions separated by `:`. If the condition is true, the first expression is executed; otherwise, the second expression is executed. + +```java title="Ternary Operator in Java" +int age = 18; +String canVote = (age >= 18) ? "Yes" : "No"; +System.out.println("Can vote: " + canVote); // Output: Can vote: Yes +``` + + + + + +### Python Conditionals Overview + +Python supports `if-else`, `else-if` ladder, and the ternary operator for conditional statements. Python does not have a native `switch` statement, but you can use dictionaries for similar behavior. + +#### 1. If-Else Statement + +`if-else` statements in Python are used to execute a block of code based on a condition. If the condition is true, the code inside the `if` block is executed; otherwise, the code inside the `else` block is executed. + +```python title="If-Else Statement in Python" +number = 10 + +if number > 5: + print("Number is greater than 5") +else: + print("Number is 5 or less") +``` + +#### 2. Else-If Ladder (Elif) + +An `elif` ladder is used when you have multiple conditions to check. The code block associated with the first true condition is executed. + +```python title="Else-If Ladder in Python" +grade = 85 + +if grade >= 90: + print("A") +elif grade >= 80: + print("B") +elif grade >= 70: + print("C") +else: + print("Fail") +``` + +#### 3. Ternary Operator + +The ternary operator is a shorthand way to write simple `if-else` statements in Python. It consists of a condition followed by a `if` and `else` expressions separated by `else`. If the condition is true, the `if` expression is executed; otherwise, the `else` expression is executed. + +```python title="Ternary Operator in Python" +age = 18 +can_vote = "Yes" if age >= 18 else "No" +print(f"Can vote: {can_vote}") # Output: Can vote: Yes +``` + +:::note +Python does not have a native `switch` statement, but you can use dictionaries for similar behavior. +::: + +```python title="Switch Case in Python" +day = "Monday" + +days = { + "Monday": "Start of the work week", + "Friday": "End of the work week" +} + +print(days.get(day, "Midweek days")) # Output: Start of the work week +``` + + + + + +### C++ Conditionals Overview + +In C++, you can use `if-else`, `else-if` ladder, `switch` case, and the ternary operator for conditional statements. + +#### 1. If-Else Statement + +`if-else` statements in C++ are used to execute a block of code based on a condition. If the condition is true, the code inside the `if` block is executed; otherwise, the code inside the `else` block is executed. + +```cpp title="If-Else Statement in C++" +int number = 10; + +if (number > 5) { + std::cout << "Number is greater than 5" << std::endl; +} else { + std::cout << "Number is 5 or less" << std::endl; +} +``` + +#### 2. Else-If Ladder + +An `else-if` ladder is used when you have multiple conditions to check. The code block associated with the first true condition is executed. + +```cpp title="Else-If Ladder in C++" +int grade = 85; + +if (grade >= 90) { + std::cout << "A" << std::endl; +} else if (grade >= 80) { + std::cout << "B" << std::endl; +} else if (grade >= 70) { + std::cout << "C" << std::endl; +} else { + std::cout << "Fail" << std::endl; +} +``` + +#### 3. Switch Case + +`switch` statements in C++ are used to select one of many code blocks to be executed based on the value of a variable. + +```cpp title="Switch Case in C++" +std::string day = "Monday"; + +if (day == "Monday") { + std::cout << "Start of the work week" << std::endl; +} else if (day == "Friday") { + std::cout << "End of the work week" << std::endl; +} else { + std::cout << "Midweek days" << std::endl; +} +``` + +#### 4. Ternary Operator + +The ternary operator is a shorthand way to write simple `if-else` statements in C++. It consists of a condition followed by a `?` and two expressions separated by `:`. If the condition is true, the first expression is executed; otherwise, the second expression is executed. + +```cpp title="Ternary Operator in C++" +int age = 18; +std::string canVote = (age >= 18) ? "Yes" : "No"; +std::cout << "Can vote: " << canVote << std::endl; // Output: Can vote: Yes +``` + + + + + + +## Visualizing Conditional Flow + +To better understand the logic of `if-else` statements, consider the following Mermaid diagram that illustrates how a basic conditional check works: + +```mermaid +graph TD + A[Start] --> B{Condition} + B -->|True| C[Execute True Block] + B -->|False| D[Execute False Block] + C --> E[End] + D --> E[End] +``` + +## Conclusion + +Conditional statements are fundamental to programming and allow you to control the flow of your code based on specific conditions. By mastering `if-else`, `switch`, and ternary operators, you can write more efficient and readable code across different programming languages. + + + + +--- + +

Feedback and Support

+ + \ No newline at end of file diff --git a/docs/programming-fundamentals/language-syntax/functions.md b/docs/programming-fundamentals/language-syntax/functions.md new file mode 100644 index 000000000..3a3b54fa4 --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/functions.md @@ -0,0 +1,227 @@ +--- +id: functions +sidebar_position: 3 +title: Functions +sidebar_label: Functions +description: "Learn about functions and how to create and use them in JavaScript, Java, Python, and C++. Understand function declarations, parameters, return values, and best practices." +tags: [functions, programming, syntax, js, java, python, cpp] +--- + +Functions are blocks of code that perform specific tasks and can be reused throughout a program. They help in organizing code, making it modular, and reducing redundancy. + + + +## What is a Function? + +A function is a reusable block of code that performs a specific task. Functions typically take input, process it, and return an output. The general structure of a function includes: + +1. **Function name**: Identifies the function. +2. **Parameters**: Input values the function uses (optional). +3. **Return type**: The value the function sends back as output (optional). +4. **Function body**: The code that runs when the function is called. + +## Functions in Different Languages + + + + +### JavaScript Functions Overview + +JavaScript supports both function declarations and expressions. + +#### Function Declaration + +```js title="Declaring a function in JavaScript" +function greet(name) { + return `Hello, ${name}!`; +} + +console.log(greet("Alice")); // Output: Hello, Alice! +``` + +#### Function Expression + +```js title="Function expression in JavaScript" +const greet = function(name) { + return `Hello, ${name}!`; +}; + +console.log(greet("Bob")); // Output: Hello, Bob! +``` + +#### Arrow Functions + +Introduced in ES6, arrow functions provide a concise way to write functions. + +```js title="Arrow function in JavaScript" +const greet = (name) => `Hello, ${name}!`; + +console.log(greet("Charlie")); // Output: Hello, Charlie! +``` + +### Function Scope and Closures + +Functions in JavaScript can create closures, capturing variables from their surrounding scope. + +```js title="Function scope and closures in JavaScript" +function outerFunction() { + let outerVar = "I'm outer"; + + function innerFunction() { + console.log(outerVar); // Can access outerVar + } + + return innerFunction; +} + +const inner = outerFunction(); +inner(); // Output: I'm outer +``` + + + + + +### Java Functions (Methods) Overview + +In Java, functions are defined within classes and are called methods. They have a return type, a name, and can have parameters. + +#### Method Declaration + +```java title="Declaring a method in Java" +public class Main { + public static void main(String[] args) { + greet("Alice"); + } + + public static void greet(String name) { + System.out.println("Hello, " + name + "!"); + } +} +``` + +### Return Values and Parameters + +Methods can return values using the `return` keyword. + +```java title="Returning a value from a method in Java" +public static int add(int a, int b) { + return a + b; +} +``` + + + + + +### Python Functions Overview + +Python functions are simple to define and use. The `def` keyword is used for defining functions. + +#### Function Declaration + +```python title="Defining a function in Python" +def greet(name): + return f"Hello, {name}!" + +print(greet("Alice")) # Output: Hello, Alice! +``` + +### Default Parameters + +Functions can have default parameter values. + +```python title="Default parameters in Python" +def greet(name="World"): + return f"Hello, {name}!" + +print(greet()) # Output: Hello, World! +``` + +### Lambda Functions + +Python also supports lambda (anonymous) functions for simple, one-line functions. + +```python title="Lambda function in Python" +add = lambda x, y: x + y +print(add(2, 3)) # Output: 5 +``` + + + + + +### C++ Functions Overview + +In C++, functions can be declared outside or inside a class. They need to specify a return type, name, and optionally, parameters. + +#### Function Declaration + +```cpp title="Declaring a function in C++" +#include +using namespace std; + +void greet(string name) { + cout << "Hello, " << name << "!" << endl; +} + +int main() { + greet("Alice"); // Output: Hello, Alice! + return 0; +} +``` + +### Return Values and Parameters + +Functions can return values and take multiple parameters. + +```cpp title="Returning a value from a function in C++" +int add(int a, int b) { + return a + b; +} + +int main() { + cout << add(5, 3); // Output: 8 + return 0; +} +``` + + + + +## Function Visualization with Mermaid + +Here's a simple Mermaid diagram to illustrate the flow of a function: + +```mermaid +graph TD; + A[Start] --> B[Call greet Function]; + B --> C{Has Parameter?}; + C -- Yes --> D[Process Parameter]; + C -- No --> E[Use Default Value]; + D --> F[Return Output]; + E --> F[Return Output]; + F --> G[End]; +``` + +In this flowchart: + +- **Start**: The beginning of the function. +- **Call greet Function**: Invoking the function. +- **Has Parameter?**: Checking if a parameter is passed. +- **Process Parameter**: Handling the parameter. +- **Use Default Value**: Using a default value if no parameter is passed. +- **Return Output**: Returning the output. +- **End**: The end of the function. + +## Conclusion + +Functions are essential building blocks in programming, allowing you to write reusable code and improve the structure of your programs. Understanding how to create and use functions in different programming languages is a fundamental skill for any developer. Practice writing functions to enhance your programming skills and make your code more efficient and maintainable. + + + +--- + +

Feedback and Support

+ + \ No newline at end of file diff --git a/docs/programming-fundamentals/language-syntax/loops.md b/docs/programming-fundamentals/language-syntax/loops.md new file mode 100644 index 000000000..a3a6c3416 --- /dev/null +++ b/docs/programming-fundamentals/language-syntax/loops.md @@ -0,0 +1,206 @@ +--- +id: loops +sidebar_position: 4 +title: Loops +sidebar_label: Loops +description: "Explore how to use loops in JavaScript, Java, Python, and C++. Learn about different types of loops and their usage, including for loops, while loops, and do-while loops." +tags: [loops, programming, syntax, js, java, python, cpp] +--- + +Loops are used in programming to repeat a block of code until a certain condition is met. This can simplify complex tasks that require repetitive actions, making code more efficient and easier to maintain. + + + +## Types of Loops + +1. **For Loop**: Repeats a block of code a specific number of times. +2. **While Loop**: Repeats a block of code as long as a condition is true. +3. **Do-While Loop**: Similar to a while loop, but guarantees at least one iteration. +4. **Enhanced For Loop (for-each)**: Used to iterate over collections or arrays (in Java, Python, and C++). + +## Loops in Different Languages + + + + +### JavaScript Loops Overview + +In JavaScript, you can use different types of loops to iterate over arrays, objects, or perform repetitive tasks. Here are examples of common loops in JavaScript: + +#### 1. For Loop + +```js title="JavaScript For Loop Example" +for (let i = 0; i < 5; i++) { + console.log(i); // Output: 0, 1, 2, 3, 4 +} +``` + +#### 2. While Loop + +```js title="JavaScript While Loop Example" +let i = 0; +while (i < 5) { + console.log(i); // Output: 0, 1, 2, 3, 4 + i++; +} +``` + +#### 3. Do-While Loop + +```js title="JavaScript Do-While Loop Example" +let i = 0; +do { + console.log(i); // Output: 0, 1, 2, 3, 4 + i++; +} while (i < 5); +``` + + + + + +### Java Loops Overview + +In Java, loops are used to iterate over arrays, collections, or perform repetitive tasks. Here are examples of common loops in Java: + +#### 1. For Loop + +```java title="Java For Loop Example" +for (int i = 0; i < 5; i++) { + System.out.println(i); // Output: 0, 1, 2, 3, 4 +} +``` + +#### 2. While Loop + +```java title="Java While Loop Example" +int i = 0; +while (i < 5) { + System.out.println(i); // Output: 0, 1, 2, 3, 4 + i++; +} +``` + +#### 3. Do-While Loop + +```java title="Java Do-While Loop Example" +int i = 0; +do { + System.out.println(i); // Output: 0, 1, 2, 3, 4 + i++; +} while (i < 5); +``` + +#### 4. Enhanced For Loop (for-each) + +```java title="Java Enhanced For Loop Example" +int[] numbers = {1, 2, 3, 4, 5}; +for (int num : numbers) { + System.out.println(num); // Output: 1, 2, 3, 4, 5 +} +``` + + + + + +### Python Loops Overview + +In Python, loops are used to iterate over sequences, collections, or perform repetitive tasks. Here are examples of common loops in Python: + +#### 1. For Loop + +```python title="Python For Loop Example" +for i in range(5): + print(i) # Output: 0, 1, 2, 3, 4 +``` + +#### 2. While Loop + +```python title="Python While Loop Example" +i = 0 +while i < 5: + print(i) # Output: 0, 1, 2, 3, 4 + i += 1 +``` + + + + + +### C++ Loops Overview + +In C++, loops are used to iterate over arrays, collections, or perform repetitive tasks. Here are examples of common loops in C++: + +#### 1. For Loop + +```cpp title="C++ For Loop Example" +for (int i = 0; i < 5; i++) { + std::cout << i << std::endl; // Output: 0, 1, 2, 3, 4 +} +``` + +#### 2. While Loop + +```cpp title="C++ While Loop Example" +int i = 0; +while (i < 5) { + std::cout << i << std::endl; // Output: 0, 1, 2, 3, 4 + i++; +} +``` + +#### 3. Do-While Loop + +```cpp title="C++ Do-While Loop Example" +int i = 0; +do { + std::cout << i << std::endl; // Output: 0, 1, 2, 3, 4 + i++; +} while (i < 5); +``` + +#### 4. Range-Based For Loop (C++11 and above) + +```cpp title="C++ Range-Based For Loop Example" +std::vector numbers = {1, 2, 3, 4, 5}; +for (int num : numbers) { + std::cout << num << std::endl; // Output: 1, 2, 3, 4, 5 +} +``` + + + + + + +## Understanding Loop Flow with a Diagram + +To better understand how loops work, let's use a Mermaid diagram to visualize the flow of a `while` loop. + +```mermaid +graph TD; + Start --> Condition{"Condition met?"} + Condition -- Yes --> CodeBlock["Execute code block"] + CodeBlock --> Condition + Condition -- No --> End["Exit loop"] +``` + +In this flowchart: +- The loop starts and checks the condition. +- If the condition is true, the code block is executed, and the condition is checked again. +- If the condition is false, the loop ends. + +This visualization helps illustrate the iterative nature of loops and how they repeat until a specific condition is no longer met. + +## Conclusion + +Loops are essential in programming to automate repetitive tasks and iterate over data structures. By using loops effectively, you can write more efficient code and solve complex problems with ease. + + + +--- + +

Feedback and Support

+ + \ No newline at end of file