day1-6 cpp
day1-6 cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl; // Output "Hello, World!" to the console
return 0;
2.2. Tokens
In C++, a variable is a named storage for data. It has a specific type (like int or
double) and must be declared before use. Variables store different kinds of values,
allowing data manipulation in programs.
#include <iostream>
int main() {
return 0;
A data type is an attribute associated with a piece of data that tells a computer system
how to interpret its value.
3.2.1. Data Types Program
#include <iostream>
int main() {
return 0;
3.3. Operators
An operator is a symbol that operates on a value to perform specific mathematical or
logical computations.
#include <iostream>
int main() {
// Arithmetic Operators
int a = 10, b = 5;
// Comparison Operators
// Logical Operators
std::cout << "Arithmetic: Sum=" << sum << " Diff=" << difference << "
Product=" << product << std::endl;
std::cout << "Comparison: Equal=" << isEqual << " Greater=" <<
isGreaterThan << " Less=" << isLessThan << std::endl;
std::cout << "Logical: AND=" << logicalAND << " OR=" << logicalOR << "
NOT=" << logicalNOT << std::endl;
return 0;}
3.4. Input
In C++, you can take input from the user using the std::cin stream. Here's an example
of how you can use std::cin to take input from the user:
#include <iostream>
int main() {
int number;
return 0;
}
Lecture 2 : Flow of Control
1. Conditional Statements
A conditional statement in programming refers to a construct that allows you to execute
different blocks of code based on the evaluation of a condition. Conditional statements enable
you to make decisions in your programs, making them dynamic and responsive to different
situations.
1.1. if Statement:
The if statement evaluates a condition. If the condition is true, the code inside the if
block is executed.
} else {
}
1.3. else-if Statement:
The else-if statement allows you to evaluate multiple conditions in sequence. If the
previous conditions are false, it checks the next condition.
} else if (condition2) {
} else {
1.4. Switch-Case
The switch statement in C++ is another way to make decisions based on the value of
a variable or expression. It provides an alternative to the if-else if-else ladder for
handling multiple conditions. Here's the basic syntax of the switch statement:
case value1:
break;
case value2:
break;
int main() {
char grade;
switch (grade) {
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'F':
std::cout << "Failed.";
break;
default:
return 0;
int main() {
int number;
if (number > 0) {
} else {
return 0;
The continue statement is used inside a loop to skip the rest of the loop's body for the
current iteration and move to the next iteration of the loop. When a continue
statement is encountered, the program control jumps to the loop's
increment/decrement expression (in for loops) or the loop condition check (in while
and do-while loops).
int main() {
int sum = 0;
if (i % 2 == 0) {
return 0;
}
2. Loops
In C++, loops are used to execute a block of code repeatedly as long as a specified condition
is met. There are several types of loops in C++: for, while, and do-while loops. Here's an
overview of each:
// Code to be executed
int main() {
return 0;
// Code to be executed
int main() {
int i = 1;
while (i <= 5) {
++i;
return 0;
// Code to be executed
} while (condition);
int main() {
int i = 1;
do {
++i;
return 0;