diff --git a/docs/day-03/1Basic_syntax.md b/docs/day-03/1Basic_syntax.md new file mode 100644 index 000000000..cfe9a5433 --- /dev/null +++ b/docs/day-03/1Basic_syntax.md @@ -0,0 +1,109 @@ +# Basic C++ Syntax Explanation and Examples: + +## 1. Comments: + +Comments are used to document code for better understanding. They are ignored by the compiler. + +### Examples: + +```cpp +// This is a single-line comment + +/* + This is a multi-line comment +*/ + +``` +## 2. Main Function: + +The `main()` function is the entry point of any C++ program. It's where the execution of the program begins. + +### Example: +```cpp +#include + +int main() { + std::cout << "Hello, World!"; + return 0; +} + +``` + +## 3. Data Types: + +C++ supports various data types for different kinds of values. + +### Example: + +```cpp +int age = 25; // Integer +float pi = 3.14; // Floating point number +char grade = 'A'; // Character +bool isTrue = true; // Boolean (true/false) + + +``` + +## 4. Variables: + +Variables are used to store data values. + +### Example: + +```cpp +int x = 5; // Declare and initialize an integer variable +float y = 3.14; // Declare and initialize a floating point variable +``` + +## 5. Control Structures: + +Control structures help in controlling the flow of the program. + +### Example: + +```cpp +if (x > 0) { + // Code block executed if condition is true +} else { + // Code block executed if condition is false +} + +while (x > 0) { + // Code block executed repeatedly while condition is true +} + +for (int i = 0; i < 5; i++) { + // Code block executed repeatedly with incrementing i from 0 to 4 +} + +``` +## 6. Functions: + +Functions are blocks of code that perform a specific task. + +### Example: + +```cpp +int add(int a, int b) { + return a + b; +} + +``` +## 7. Classes: + +Classes are user-defined data types that contain data members and member functions. + +### Example: + +```cpp +class Rectangle { +public: + int width; + int height; + + int area() { + return width * height; + } +}; + +``` \ No newline at end of file diff --git a/docs/day-03/2Structure_of_a_C++ Program.md b/docs/day-03/2Structure_of_a_C++ Program.md new file mode 100644 index 000000000..e6560d2a0 --- /dev/null +++ b/docs/day-03/2Structure_of_a_C++ Program.md @@ -0,0 +1,50 @@ +# Structure of a C++ Program + +A typical C++ program consists of several parts: + +## Preprocessor Directives + +These are commands to the compiler and are preceded by a `#` symbol. Common preprocessor directives include `#include` for including header files and `#define` for defining constants. + +## Main Function + +Every C++ program must have a `main()` function, which serves as the entry point of the program. Execution of the program begins from here. + +## Function Definitions + +Additional functions may be defined to perform specific tasks within the program. These functions can be defined before or after the `main()` function. + +## Header Files + +C++ programs often use header files to declare functions, classes, and constants. These header files are included at the beginning of the program using the `#include` directive. + +## Namespace + +Namespace is used to avoid naming conflicts and to organize code into logical groups. Standard library elements are typically in the `std` namespace. + +## Comments + +Comments are used to document the code and improve its readability. They can be single-line (`//`) or multi-line (`/* */`). + +# Example C++ Program + +Here's an example of a simple C++ program that calculates the factorial of a number: + +```cpp +#include + +// Function to calculate factorial +int factorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * factorial(n - 1); +} + +int main() { + int num; + std::cout << "Enter a number: "; + std::cin >> num; + std::cout << "Factorial of " << num << " is: " << factorial(num) << std::endl; + return 0; +} diff --git a/docs/day-03/Comments_in_C++.md b/docs/day-03/Comments_in_C++.md new file mode 100644 index 000000000..5ddffe206 --- /dev/null +++ b/docs/day-03/Comments_in_C++.md @@ -0,0 +1,28 @@ +# Comments in C++ + +Comments in C++ are essential for documenting code and making it more understandable. There are two types of comments in C++: + +## Single-line comments + +These comments begin with `//` and continue until the end of the line. They are often used for short, inline explanations. + +```cpp +// This is a single-line comment +int x = 5; // This is also a single-line comment +``` +# Multi-line Comments in C++ + +Multi-line comments in C++ begin with `/*` and end with `*/`. They can span multiple lines and are often used for longer explanations or for temporarily disabling blocks of code. + +```cpp +/* This is a multi-line comment + It can span multiple lines */ +int y = 10; + +/* +This is another example of a multi-line comment +that spans multiple lines +*/ +``` + +Comments are ignored by the compiler and do not affect the execution of the program. They are purely for the benefit of developers to understand the code better. It's good practice to include comments to explain complex logic, algorithmic steps, or to provide context for future maintainers of the code. \ No newline at end of file diff --git a/docs/day-03/_category_.json b/docs/day-03/_category_.json index 91740c50d..afc85edb1 100644 --- a/docs/day-03/_category_.json +++ b/docs/day-03/_category_.json @@ -1,7 +1,8 @@ { - "label": "Day 03", - "position": 4, - "link": { - "type": "generated-index" + "label": "Day 03", + "position": 4, + "link": { + "type": "generated-index" + } } -} + \ No newline at end of file