Computer Methods in Science Course

Friday, April 4, 2025

Basic Comparison of C++ and HTML/JavaScript

 Here’s a basic comparison of C++ and HTML/JavaScript for common programming concepts and simple code examples for each. This will help you understand the difference in syntax, structure, and execution logic between the two environments.


πŸ“Œ 1. Program Structure

ConceptC++HTML/JavaScript
TypeCompiled, strongly typedInterpreted, weakly typed
Entry Pointmain() functionHTML loads, JS runs on events/load
Use CaseSystem software, algorithmsWeb development, interactivity

C++ Example

#include <iostream>
using namespace std; int main() { cout << "Hello from C++!" << endl; return 0; }

HTML/JavaScript Example

<!DOCTYPE html>
<html> <body> <p id="demo">Hello from HTML!</p> <script> document.getElementById("demo").innerHTML += " And JavaScript!"; </script> </body> </html>

πŸ“Œ 2. Variables and Data Types

FeatureC++JavaScript
DeclarationRequires type (int, float)var, let, or const
Type SystemStatically typedDynamically typed

C++ Example

int age = 25;
float height = 5.9;

JavaScript Example

let age = 25;
let height = 5.9;

πŸ“Œ 3. Conditionals

C++ Example

int number = 10;
if (number > 5) { cout << "Greater than 5" << endl; }

JavaScript Example

let number = 10;
if (number > 5) { console.log("Greater than 5"); }

πŸ“Œ 4. Loops

C++ Example

for (int i = 0; i < 5; i++) {
cout << i << endl; }

JavaScript Example

for (let i = 0; i < 5; i++) {
console.log(i); }

πŸ“Œ 5. Functions

C++ Example

int add(int a, int b) {
return a + b; }

JavaScript Example

function add(a, b) {
return a + b; }

πŸ“Œ 6. User Interaction (Input/Output)

C++ Example (Console)

int age;
cout << "Enter your age: "; cin >> age;

HTML/JavaScript Example (Web Page)

<input type="text" id="ageInput" placeholder="Enter your age">
<button onclick="getAge()">Submit</button> <p id="output"></p> <script> function getAge() { let age = document.getElementById("ageInput").value; document.getElementById("output").innerText = "Your age is " + age; } </script>

πŸ“Œ 7. Comments

C++ Example

// This is a single-line comment
/* This is a multi-line comment */

JavaScript Example

// This is a single-line comment
/* This is a multi-line comment */

Summary

FeatureC++HTML/JavaScript
Used forBackend/system appsWeb frontend/interactivity
Type systemStatically typedDynamically typed
CompilationNeeds compilationRuns in browser
UI InteractionCLI (console)HTML/DOM interaction
Language ComplexityMore structured and strictMore flexible, event-driven

No comments:

Post a Comment