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

C++ Lab

The document provides an introduction to programming with C++. It discusses what programming and C++ are, the basic elements of a C++ program including functions, data types, operators, input/output, and flow control. Examples of simple C++ code are provided to demonstrate various concepts like arithmetic operations, conditional statements, type conversion, etc. The key topics covered in the document are: 1. What programming and C++ are and how C++ extends the C language. 2. The basic building blocks of a C++ program including main functions, comments, identifiers, keywords, etc. 3. Primitive data types and how to work with user input and output. 4. Common operators for arithmetic, comparison

Uploaded by

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

C++ Lab

The document provides an introduction to programming with C++. It discusses what programming and C++ are, the basic elements of a C++ program including functions, data types, operators, input/output, and flow control. Examples of simple C++ code are provided to demonstrate various concepts like arithmetic operations, conditional statements, type conversion, etc. The key topics covered in the document are: 1. What programming and C++ are and how C++ extends the C language. 2. The basic building blocks of a C++ program including main functions, comments, identifiers, keywords, etc. 3. Primitive data types and how to work with user input and output. 4. Common operators for arithmetic, comparison

Uploaded by

Nurye Nigus
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Lab 1

Programming with c++


What is Programming ?
• A programming language is a vocabulary and set of
grammatical rules for instructing a computer or computing
device to perform specific tasks. •

• The term programming language usually refers to high-level


languages, such as BASIC, C, C++, COBOL, Java, FORTRAN,
Ada, and Pascal.
What is C++ ?
• C++ is a cross-platform language that can be used to
create high-performance applications.
• C++ was developed by Bjarne Stroustrup, as an
extension to the C language.
• C++ gives programmers a high level of control over
system resources and memory.
• The language was updated 4 major times in 2011,
2014, 2017, and 2020 to C++11, C++14, C++17, C+
+20.
Cont..
• A C++ program is a collection of one or more subprograms, called
functions
• What is Functions ?
• A function is a block of code that performs some operation. A function can
optionally define input parameters that enable callers to pass arguments into
the function. A function can optionally return a value as output.
Cont…
• Every C++ program has main function.
• Basic elements of C++ program
• Comments
• identifiers,
• keywords,
• operators,
• Literals, and
• special symbols.
Example code
• #include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}
Primitive data types
Input and output

#include <iostream>
using namespace std;

int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A’;

cout << num1 << endl; // print integer


cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char return 0; }
Cont…

#include <iostream>
using namespace std;
int main() {

int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0; }
Operators
• Arithmetic operators
• Increment and Decrement operators
• Assignment operator
• Relational operators
• Logical operators
• Conditional operators
• Bitwise operators
Arthimetic
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;

// printing the difference of a and b


cout << "a - b = " << (a - b) << endl;

// printing the product of a and b


cout << "a * b = " << (a * b) << endl;

// printing the division of a by b


cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b


cout << "a % b = " << (a % b) << endl;

return 0; }
Increment and Decrement operators

#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in result_a


result_a = ++a;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in result_b


result_b = --b;
cout << "result_b = " << result_b << endl;
return 0; }
Assignment operator
Cont…
#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;

// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;

// assigning the sum of a and b to a


a += b;

// a = a +b
cout << "a = " << a << endl;
return 0; }
Relational
Cont…
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;

result = (a == b); // false


cout << "3 == 5 is " << result << endl;

result = (a != b); // true


cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;

result = a < b; // true


cout << "3 < 5 is " << result << endl;

result = a >= b; // false


cout << "3 >= 5 is " << result << endl;

result = a <= b; // true


cout << "3 <= 5 is " << result << endl;

return 0; }
Logical Operator
Cont…
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 < 5); // false


cout << "(3 == 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 > 5); // false


cout << "(3 == 5) && (3 > 5) is " << result << endl;

result = (3 != 5) || (3 < 5); // true


cout << "(3 != 5) || (3 < 5) is " << result << endl;

result = (3 != 5) || (3 > 5); // true


cout << "(3 != 5) || (3 > 5) is " << result << endl;

result = (3 == 5) || (3 > 5); // false


cout << "(3 == 5) || (3 > 5) is " << result << endl;

result = !(5 == 2); // true


cout << "!(5 == 2) is " << result << endl;

result = !(5 == 5); // false


cout << "!(5 == 5) is " << result << endl;
return 0; }
Type Conversion
• C++ allows us to convert data of one type to that of another. This is
known as type conversion.

• There are two types of type conversion in C++.


1.Implicit Conversion
2.Explicit Conversion (also known as Type Casting)
Implicit
• The type conversion that is done automatically done by the compiler is
known as implicit type conversion. This type of conversion is also
known as automatic conversion.
• Let us look at two examples of implicit type conversion.
Cont…
#include <iostream>
using namespace std;
int main() {
// assigning an int value to num_int
int num_int = 9;

// declaring a double type variable


double num_double;

// implicit conversion
// assigning int value to a double variable
num_double = num_int;

cout << "num_int = " << num_int << endl;


cout << "num_double = " << num_double << endl;

return 0; }
Cont…
#include <iostream>
using namespace std;
int main() {
int num_int;
double num_double = 9.99;

// implicit conversion
// assigning a double value to an int variable
num_int = num_double;

cout << "num_int = " << num_int << endl;


cout << "num_double = " << num_double << endl;
return 0; }
Cont…
Explicit Conversion
• When the user manually changes data from one type to another, this is
known as explicit conversion. This type of conversion is also known
as type casting.
• There are three major ways in which we can use explicit conversion in
C++. They are:
1.C-style type casting (also known as cast notation)
2.Function notation (also known as old C++ style type casting)
3.Type conversion operators
C-style Type Casting
• As the name suggests, this type of casting is favored by the C
programming language. It is also known as cast notation.
Cont…

// initializing int variable


int num_int = 26;

// declaring double variable


double num_double;

// converting from int to double


num_double = (double)num_int;
Function-style Casting
• We can also use the function like notation to cast data from one type to
another.
Cont…

// initializing int variable


int num_int = 26;
// declaring double variable
double num_double;
// converting from int to double
num_double = double(num_int);
Type casting
#include <iostream>
using namespace std;
int main() {
// initializing a double variable
double num_double = 3.56;
cout << "num_double = " << num_double << endl;

// C-style conversion from double to int


int num_int1 = (int)num_double;
cout << "num_int1 = " << num_int1 << endl;

// function-style conversion from double to int


int num_int2 = int(num_double);
cout << "num_int2 = " << num_int2 << endl;

return 0; }
Type Conversion Operators

Besides these two type castings, C++ also has four


operators for type conversion.
They are known as type conversion operators. They
are:
•static_cast
•dynamic_cast
•const_cast
•reinterpret_cast
Flow Control (If …Else)
• In computer programming, we use the if...else statement to run one
block of code under certain conditions and another block of code
under different conditions.
IF … Else
• There are three forms of if...else statements in C++.

• if statement
• if...else statement
• if...else if...else statement
IF statement
if (condition) {
// body of if statement
}

• The if statement evaluates the condition inside the parentheses ( ).


• If the condition evaluates to true, the code inside the body of if is
executed.
• If the condition evaluates to false, the code inside the body of if is
skipped.
IF Statement illustration
Code example
#include <iostream>
using namespace std;
int main() {
int number;

cout << "Enter an integer: ";


cin >> number;

// checks if the number is positive


if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}

cout << "This statement is always executed.";

return 0; }
IF … Else
• The if statement can have an optional else clause. Its syntax is:

if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
IF … Else statement illustration
Cont…
• If the condition evaluates true,
• the code inside the body of if is executed
• the code inside the body of else is skipped from execution

• If the condition evaluates false,


• the code inside the body of else is executed
• the code inside the body of if is skipped from execution
Code Example
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;

if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0; }
if...else...else if statement
• The if...else statement is used to execute a block of code among two alternatives. However, if
we need to make a choice between more than two alternatives, we use the if...else if...else
statement.

if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
IF … Else if … else statement illustration
Code Example
// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
}
else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}
Nested if...else

// outer if statement
if (condition1) {
// statements

// inner if statement
if (condition2) {
// statements
}
}
Code Example
// C++ program to find if an integer is positive, negative or zero
// using nested if statements

#include <iostream>
using namespace std;

int main() {

int num;

cout << "Enter an integer: ";


cin >> num;

// outer if condition
if (num != 0) {

// inner if condition
if (num > 0) {
cout << "The number is positive." << endl;
}
// inner else condition
else {
cout << "The number is negative." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This line is always printed." << endl;
return 0;
}
Switch Statement
• The switch statement allows us to execute a block of code among
many alternatives.

• The syntax of the switch statement in C++ is:


Syntax
switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;

case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}
Flowchart
Code Example
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {

char oper; float num1, num2;


cout << "Enter an operator (+, -, *, /): ";

cin >> oper;

cout << "Enter two numbers: " << endl;

cin >> num1 >> num2;

switch (oper) {
case '+’:
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-’:
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*’:
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/’:
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
Looping (Iterative) Statements
• for loop
• while loop
• do...while loop

You might also like