0% found this document useful (0 votes)
3 views

day1-6 cpp

The document provides an introduction to C++, highlighting its features such as object-oriented programming, efficiency, and portability. It covers basic programming concepts including variables, data types, operators, and control flow structures like conditional statements and loops. Additionally, it includes example programs demonstrating these concepts in practice.

Uploaded by

yashjain2112
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

day1-6 cpp

The document provides an introduction to C++, highlighting its features such as object-oriented programming, efficiency, and portability. It covers basic programming concepts including variables, data types, operators, and control flow structures like conditional statements and loops. Additionally, it includes example programs demonstrating these concepts in practice.

Uploaded by

yashjain2112
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 1 : Introduction

1. Introduction and features of C++


1.1. What is C++ ?

C++ is a powerful, highperformance programming language known for its


efficiency and versatility. It extends the popular C programming language,
offering features like objectoriented programming and lowlevel memory
manipulation. C++ is widely used for system/software development, game
development, and more. With its active community and rich libraries, C++
provides an excellent platform for building a wide range of applications.

1.2. Features of C++


• Objectoriented: Supports objectoriented programming, allowing for efficient
organization of code through classes and objects.
• Efficiency: Known for high performance and efficient memory management,
making it suitable for resource intensive applications.
• STL: Provides the Standard Template Library (STL) with prebuilt classes and
functions, enhancing code reusability and productivity.
• Portability: Code can be compiled and executed across different platforms
with minimal modifications, ensuring widespread usability.
• Multiparadigm: Supports procedural, objectoriented, and generic
programming, allowing flexibility in coding styles.
• Exception Handling: Offers robust mechanisms for handling errors and
exceptions, ensuring the reliability of programs.
• Community Support: Boasts a large and active developer community,
providing ample resources and support for learning and problemsolving.
2. First Program “Hello Word”
2.1. The code

#include <iostream>

int main() {

std::cout << "Hello, World!" << std::endl; // Output "Hello, World!" to the console

return 0;

2.2. Tokens

Tokens are essential elements of a program, representing the smallest meaningful


symbols to the compiler. Our code demonstrates all six types of tokens, excluding
typical operators.

Token type Description/Purpose Examples


Keywords Words with special int, double, for, auto
meaning to the compiler
Identifiers Names of things that are cout, std, x, myFunction
not built into the language
Literals Basic constant values "Hello, world!", 24.3, 0, ’c’
whose value is specified
directly in the source code
Operators Mathematical or logical +, , &&, %, <<
operations
Punctuation/Separators Punctuation defining the {}(),;
structure of a program
Whitespace Spaces of various sorts; Spaces, tabs, newlines,
ignored by the compiler comments

2.3. Escape sequence


Escape sequences are combinations of characters, starting with a backslash,
representing special meanings in strings or characters, like newline (\n) or tab
(\t).

Escape Sequence Represented Character


\a System bell (beep sound)
\b Backspace
\f Formfeed (page break)
\n Newline (line break)
\r “Carriage return” (returns cursor to start
of line)
\t Tab
\\ Backslash
\’ Single quote character
\" Double quote character
3. Basic of C++
3.1. Variables

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.

3.1.1. Variable Program

#include <iostream>

using namespace std;

int main() {

int x; // Declare an integer variable named x

x = 4 + 2; // Calculate 4 + 2 and assign the result to x

cout << x / 3 << ' ' << x * 2;

return 0;

3.2. Data Types

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() {

int integerNumber = 10; // Integer variable

float floatNumber = 3.14f; // Float variable (note the 'f' suffix)

double doubleNumber = 2.71828; // Double variable

char character = 'A'; // Character variable

bool booleanValue = true; // Boolean variable (true or false)

return 0;

3.3. Operators
An operator is a symbol that operates on a value to perform specific mathematical or
logical computations.

3.3.1. Types of Operators


• Arithmetic: `+`, `-`, `*`, `/`, `%`
• Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
• Logical: `&&`, `||`, `!`
• Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
• Assignment: `=`, `+=`, `-=` etc.
• Increment/Decrement: `++`, `--`
3.3.2. Operators Program

#include <iostream>

int main() {

// Arithmetic Operators

int a = 10, b = 5;

int sum = a + b, difference = a - b, product = a * b, quotient = a / b, remainder


= a % b;

// Comparison Operators

bool isEqual = (a == b), isGreaterThan = (a > b), isLessThan = (a < b);

// Logical Operators

bool logicalAND = (isEqual && isGreaterThan), logicalOR = (isEqual ||


isLessThan), logicalNOT = !logicalAND;

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:

3.4.1. Input Program

#include <iostream>

int main() {

int number;

std::cout << "Enter a number: ";

std::cin >> number; // Input is stored in the 'number' variable

std::cout << "You entered: " << number << std::endl;

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.

1.1.1. if Statement Syntax:


if (condition) {

// Code to be executed if the condition is true

1.2. if-else Statement:


The if-else statement evaluates a condition. If the condition is true, the code inside the
if block is executed. If the condition is false, the code inside the else block is
executed.

1.2.1. if-else Statement Syntax:


if (condition) {

// Code to be executed if the condition is true

} else {

// Code to be executed if the condition is false

}
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.

1.3.1. else-if Syntax:


if (condition1) {

// Code to be executed if condition1 is true

} else if (condition2) {

// Code to be executed if condition2 is true and condition1 is false

} else {

// Code to be executed if both condition1 and condition2 are false

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:

1.4.1. Switch-Case Syntax


switch (expression) {

case value1:

// Code to be executed if expression equals value1

break;

case value2:

// Code to be executed if expression equals value2

break;

// ... more cases ...


default:

// Code to be executed if expression doesn't match any case

1.4.2. Switch-Case Program


#include <iostream>

int main() {

char grade;

std::cout << "Enter your grade: ";

std::cin >> grade;

switch (grade) {

case 'A':

std::cout << "Excellent!";

break;

case 'B':

std::cout << "Good!";

break;

case 'C':

std::cout << "Satisfactory.";

break;

case 'D':

std::cout << "Needs improvement.";

break;

case 'F':
std::cout << "Failed.";

break;

default:

std::cout << "Invalid grade!";

return 0;

1.5. If-elseIf-else Program


#include <iostream>

int main() {

int number;

std::cout << "Enter a number: ";

std::cin >> number;

// Conditional statement using if-else

if (number > 0) {

std::cout << "The number is positive." << std::endl;

} else if (number < 0) {

std::cout << "The number is negative." << std::endl;

} else {

std::cout << "The number is zero." << std::endl;

return 0;

1.6. Break and Continue


The break statement is used inside a loop to immediately exit the loop's body and
terminate the loop. When a break statement is encountered, the program control exits
the loop, and the code following the loop is executed.

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).

1.6.1. Break and Continue Program


#include <iostream>

int main() {

int sum = 0;

for (int i = 1; i <= 20; ++i) {

if (i % 2 == 0) {

std::cout << i << " is even. Skipping to the next iteration.\n";

continue; // Skip even numbers

sum += i; // Add odd numbers to the sum

std::cout << "Current Sum: " << sum << std::endl;

if (sum >= 50) {

std::cout << "Sum reached 50 or more. Breaking the loop.\n";

break; // Exit the loop if the sum is 50 or more

std::cout << "Final Sum: " << sum << std::endl;

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:

2.1. For Loop


The for loop is used when you know beforehand how many times you want to
execute the loop.

2.1.1. For Loop Syntax


for (initialization; condition; update) {

// Code to be executed

2.1.2. For loop Program


#include <iostream>

int main() {

for (int i = 1; i <= 5; ++i) {

std::cout << i << " "; // Output: 1 2 3 4 5

return 0;

2.2. While Loop


The while loop is used when you want to execute a block of code as long as a
condition is true.

2.2.1. While Loop syntax


while (condition) {

// Code to be executed

2.2.2. While Loop Program


#include <iostream>

int main() {

int i = 1;

while (i <= 5) {

std::cout << i << " "; // Output: 1 2 3 4 5

++i;

return 0;

2.3. Do-While Loop


The do-while loop is similar to the while loop, but it guarantees that the code inside
the loop is executed at least once before checking the condition.

2.3.1. Do-while Loop Syntax


do {

// Code to be executed

} while (condition);

2.3.2. Do-While Loop Program


#include <iostream>

int main() {

int i = 1;
do {

std::cout << i << " "; // Output: 1 2 3 4 5

++i;

} while (i <= 5);

return 0;

You might also like