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

Basic ProgrammingWorkshop Day 1

The document outlines the topics covered on Day 1 of a C++ programming course, including variables, arithmetic operators, header files, input/output, strings, conditional statements, and loops. It explains various types of variables in C++, their uses, and basic programming concepts such as control statements and loops. Additionally, it highlights the importance of header files and the standard input/output methods in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Basic ProgrammingWorkshop Day 1

The document outlines the topics covered on Day 1 of a C++ programming course, including variables, arithmetic operators, header files, input/output, strings, conditional statements, and loops. It explains various types of variables in C++, their uses, and basic programming concepts such as control statements and loops. Additionally, it highlights the importance of header files and the standard input/output methods in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

(BASIC)

DAY 1
TOPICS TO BE COVERED
● Day 1 :
○ Basic Introduction
○ Variables
○ Arithmetic Operators
○ Header Files
○ Input and Output in C++
○ String
○ Conditional Statements
○ Loops
Types of programming languages:

1. C++
2. Java
3. Python etc…
C++ Variables
Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

● int - stores integers (whole numbers), without decimals, such as 123 or -123
● double - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
● string - stores text, such as "Hello World". String values are surrounded by double quotes
● bool - stores values with two states: true or false
● Double - Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits

const int myNum = 15 //const value can never be changed

const int mod=1e9+7;


ASCII VALUES
A-Z = 65 to 90
a-z = 97 to 122
0-9 = 48 to 57

Overflow
- int , long int, long long
Underflow
- signed and unsigned
Arithmetic Operators
Pre and post increment/decrement

In Pre-Increment, the operator sign (++) comes before the variable. It


increments the value of a variable before assigning it to another variable.
In Post-Increment, the operator sign (++) comes after the variable. It
assigns the value of a variable to another variable and then increments its
value
Header Files in C++
Some Basic header files in c++

Most used header file in c++ : #include<bits/stdc++.h>


(as it comprises of all standard header files)
Why do we use ( using namespace std;)
C++ organizes its standard library inside the std
namespace to prevent name conflicts with user-defined
variables and functions. Without (using namespace
std;) , you'd have to prefix standard library
components with std:: , like this-

To make the code less


verbose, you can use:
Basic C++ code
Input and Output in C++
In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.
● Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main
memory then this process is called input.
● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device (display
screen) then this process is called output.

All of these streams are defined inside the <iostream> header file which contains all the standard input and
output tools of C++. The two instances cout and cin of iostream class are used very often for printing outputs and
taking inputs respectively. These two are the most basic methods of taking input and printing output in C++.

Input -> cin >> a; To change line in output we use->


Output -> cout << a; ● endl
● “\n”
Strings
Strings can be defined in several ways in C++. Strings can be accessed from the standard
library using the string class. Character arrays can also be used to define strings. String
provides a rich set of features, such as searching and manipulating, which are commonly
used methods. Despite being less advanced than the string class, this method is still
widely used, as it is more efficient and easier to use. Ways to define a string in C++ are:
● Using String keyword
● Using C-style strings
-> Conditional Statements in C++
● if
● else if
● else
● switch

-> Control Statements


● Break Statement
● Continue Statement
if, else if, else Statements-

● Use if to specify a block of code to be executed,

if a specified condition is true

● Use else to specify a block of code to be executed,

if the same condition is false

● Use else if to specify a new condition to test,

if the first condition is false


Switch Statement-
In C++, the switch statement is a flow control statement that is used to execute the different blocks
of statements based on the value of the given expression. It is an alternative to the long if-else-if
ladder which provides an easy way to execute different parts of code based on the value of the
expression.

Rules of the switch Statement in C++


There are some rules that we need to follow when using switch statements in C++. They are as
follows:
1. The case value must be either int or char type.
2. There can be any number of cases.
3. No duplicate case values are allowed.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.
Difference between if else and switch case-
Loops
Why do we use loop ?

Which code seems more feasible ?


For Loop
In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the given
number of times. It is generally preferred over while and do-while loops in case the number of iterations is
known beforehand. For eg.
1) Print first 20 natural numbers :
While Loop
In C++, the while loop is an entry-controlled loop that repeatedly executes a block of code as long as the
given condition remains true. Unlike the for loop, while loop is used in situations where we do not know the
exact number of iterations of the loop beforehand as the loop execution is terminated on the basis of the
test condition.
1) Print first 20 natural numbers:
Do while Loop
In C++, the do-while loop is an exit-controlled loop that repeatedly executes a block of code at least
once and continues executing as long as a given condition remains true. Unlike the while loop, the
do-while loop guarantees that the loop body will execute at least once, regardless of whether the
condition is true or false.
Break : When C++ reaches a break keyword, it Continue : The continue statement breaks
breaks out of the switch block. one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in
the loop.

You might also like