C++: From Darkness To Dawn
C++: From Darkness To Dawn
What is C++?
C++ is a powerful general-purpose programming language. It is an extension of the C
language and supports both procedural and object-oriented programming.
C++:
int main() {
cout << "Hello, World!" << endl; // Prints to the console
return 0;
}
Explanation:
#include <iostream>: Includes the standard input/output library.
using namespace std;: So we don’t have to write std:: every time.
int main(): The main function where the program starts.
cout: Used to display output.
return 0;: Ends the program.
Popular sites:
https://www.programiz.com/cpp-programming/online-compiler
https://www.onlinegdb.com/online_c++_compiler
https://www.tutorialspoint.com/compile_cpp_online.php
How to Use:
1. Open the link.
2. Paste or write your C++ code.
3. Click Run or Execute.
4. See the output below the editor.
Run C++ Locally (More Control & Power)
For Windows:
1. Install a Compiler:
o Download Code::Blocks (with MinGW):
https://www.codeblocks.org/downloads
o Or use Dev-C++ or Visual Studio (Community Edition).
2. Write Code:
o Open the IDE, create a new project or file.
o Write your code, then Build and Run.
For macOS:
1. Install Xcode Command Line Tools:
Bash:
xcode-select --install
Bash:
Bash:
Bash:
#include <iostream>
using namespace std;
int main() {
int age = 25; // integer variable
cout << "Age: " << age << endl;
return 0;
}
Example 2: Float (Decimal) Variable
C++:
#include <iostream>
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
#include <iostream>
#include <string> // Required for string
using namespace std;
int main() {
#include <iostream>
using namespace std;
int main() {
int score = 10;
score = 20; // update value
cout << "Score: " << score << endl;
return 0;
}
What are Data Types?
Data types tell the compiler what kind of data a variable will store.
Each variable must be declared with a data type like int, float, char, etc.
Integer (int)
C++:
String (string)
C++:
#include <string>
Boolean (bool)
C++:
#include <iostream>
#include <string>
using namespace std;
int main() {
return 0;
}
What Are Conditional Statements?
Conditional statements are used to make decisions in a program.
They help you run specific code only when certain conditions are true.
if Statement
C++:
#include <iostream>
int main() {
#include <iostream>
int main() {
int age = 15;
if (age >= 18) {
std::cout << "You are an adult." << std::endl;
} else {
std::cout << "You are a minor." << std::endl;
}
return 0;
}
#include <iostream>
int main() {
int score = 75;
if (score >= 90) {
std::cout << "Grade: A" << std::endl;
} else if (score >= 80) {
std::cout << "Grade: B" << std::endl;
} else if (score >= 70) {
std::cout << "Grade: C" << std::endl;
} else {
std::cout << "Grade: F" << std::endl;
}
return 0;
}
int main() {
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
default:
std::cout << "Invalid day";
}
return 0;
}
Logical Operators
` `
for Loop
Used when you know how many times to repeat.
C++:
#include <iostream>
int main() {
for (int i = 1; i <= 5; i++) {
std::cout << "Count: " << i << std::endl;
}
return 0;
}
while Loop
Used when you don’t know exactly how many times to loop in advance.
C++:
#include <iostream>
int main() {
int i = 1;
while (i <= 5) {
std::cout << "Count: " << i << std::endl;
i++;
}
return 0;
}
do...while Loop
Runs the code at least once, then checks the condition.
C++:
#include <iostream>
int main() {
int i = 1;
do {
std::cout << "Count: " << i << std::endl;
i++;
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) break;
std::cout << i << " ";
}
return 0;
}
Example: continue
C++:
#include <iostream>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
std::cout << i << " ";
}
return 0;
}
Summary
Loop Type Best Use Case
for Fixed number of repetitions
while Unknown repetitions (check before run)
do...while Must run at least once
What Is a Function?
A function is a block of code that performs a specific task.
Instead of writing the same code again and again, you write it once in a function and call it
when needed.
return_type function_name(parameters) {
// code to execute
}
C++:
function_name(arguments);
Example 1: Simple Function (No Parameters)
C++:
#include <iostream>
using namespace std;
// Function definition
void greet() {
cout << "Hello, Souhail!" << endl;
}
int main() {
greet(); // Function call
return 0;
}
Example 2: Function with Parameters
C++:
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
Return type int, void, float, etc. What the function gives back
Email:
[email protected]
Blogger:
https://souhaillaghchimdev.blogspot.com/