Professional Documents
Culture Documents
C&P all lectures (21)
C&P all lectures (21)
Lecture-1
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
1
10/10/2023
Reference books
2. Object-Oriented Programming Exercises
with C++, by Haris Tsetsekas
3. Any good book on C++
4. Let Us C (For practice programs only)
By Yashavant Kanetkar
Assessments
• Sessional Assessment (40 Marks)
– Assignment/s
– Quizzes
– Mid Term (20 marks)
• Final Assessment (60 Marks)
– Exam(60 Marks)
2
10/10/2023
Computer
• It is an electronic device
• It Manipulates information
• It has the ability to store, retrieve,
and process data.
• It Converts data into information -
useful to people
• It is controlled by programmed
instructions which tell it what to do.
• It can be used to create or change
documents, play games, browse the
Web, social media access, business,
health care.
5
3
10/10/2023
4
10/10/2023
5
10/10/2023
Generations of Computers
• Generation includes both hardware and software, which
together make up an entire computer system.
• There are totally five computer generations known till date.
11
6
10/10/2023
13
7
10/10/2023
16
8
10/10/2023
Classification of Computers
• Supercomputers.
• Mainframe computers
• Minicomputers
• Personal computers (PCs) or microcomputers
17
9
10/10/2023
20
10
10/10/2023
THE END
22
11
10/12/2023
Lecture-2&3
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
1
10/12/2023
2
10/12/2023
3
10/12/2023
4
10/12/2023
6. Web Applications
• Users access it as an application via web browsers over a network such as the Internet or an
intranet.
• It is also a computer software application that developers code in a browser support language
and that relies on a common web browser to render the application executable.
• The first generation of web applications allows the business to post the information publicly.
• Anyone with a web browser and internet access can see this information.
10
5
10/12/2023
11
12
6
10/12/2023
13
7
10/12/2023
15
16
8
10/12/2023
18
9
10/12/2023
• (0.1010000 )2 = ( ?? )10
19
• Try these;
• 99 = ( ?? )2
• 185= ( ?? )2
20
10
10/12/2023
• (14.25)10= ( ?? )2
0.25 X2 =0.5 0
0.5 X 2 =1.0 1
0 X 0= 0 0
(14.25)10= ( 1110.0100)2
• Try these
• ( 99.15)10 = ( ?? )2
• (1990.86)10 = ( ?? )2 21
22
11
10/12/2023
THE END
23
12
10/20/2023
Lecture-4&5
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
(37)8 = (31)10
(37246)8 = (16038)10
1
10/20/2023
(973)10 = (1715)8
2
10/20/2023
=1204758
100 010 111 (binary)
Try these 4 2 7 (octal)
00000101110000002= ???
(120475 )8 = (001010000100111101 )2
5 0 1
101 000 001
3
10/20/2023
(1973)10 = (7B5)16
4
10/20/2023
10
5
10/20/2023
Programming Languages
11
Programming Languages
• Computer programs are instructions which tell the computer what
to do.
• Programs are written in a language which computer can use.
• There are hundreds of programming languages, and they were
developed to make the programming process easier for people.
• All programs are converted into a language which computer can
understand.
12
6
10/20/2023
13
Machine Language
• Machine Language is computers’s native
language.
• It is a set of built-in primitive instructions.
• These instructions are in the form of
binary code.
• For example, to add two numbers, we
have to write an instruction in binary
code, like this:
• For example: 1101101010011010
• Programming in machine language is a
tedious process.
• Programming in machine language are
very difficult to read and modify.
14
7
10/20/2023
15
High-Level Language
• High-level languages are English-like
and easy to learn and use.
• A high-level languages are platform-
independent.
• we can write a program in a high-
level language and run it in different
types of machines.
• The instructions in a high-level
programming language are called
statements.
• Example to compute area of a circle
with a radius of 5:
• area = 5 * 5 * 3.1415
16
8
10/20/2023
17
18
9
10/20/2023
19
20
10
10/20/2023
11
10/20/2023
23
24
12
10/20/2023
25
26
13
10/20/2023
THE END
27
14
10/20/2023
Lecture-6
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
C++ Programming
• C++ is derived from the C language.
• C++ is a superset of C
• every correct statement in C is also a
correct statement in C++, although
the reverse is not true.
• It Supports Data Abstraction
• It Supports OOP
• greatly influenced development of C#
and Java
• applications: systems software,
application software, device drivers,
embedded software, high-
performance server and client
applications, entertainment software
such as video games, native code for
Android applications
1
10/20/2023
Applications of C++
C++ Libraries
• C++ class libraries are modular components of
reusable code.
• Using class libraries, we can integrate blocks of
code that have been previously built and tested.
• C++ library consists of one or more header files
and an object library.
• The header files provide class and other
definitions needed to access the library functions.
• The object library provides compiled functions
and data that are linked with your program to
produce an executable program.
4
2
10/20/2023
3
10/20/2023
4
10/20/2023
Source code
• Source code is written by programmer by
using any High Level Language or
Intermediate language which is human-
readable.
• source code is a set of instructions or
commands or statements which is written
by a programmer by using a computer
programming language like C, C++, Java,
Python, Assembly language etc.
• Source code is easy to read and modify.
• Source code contains comments that
programmer puts for better understanding.
• Computer can not directly understand
source code
9
Object Code
• Source code is provided to language translator which converts it into
machine understandable code which is called machine code or object
code.
• the compiler converts the source program into an object program.
• object code is a program or a file that is created after compiling the source
code
• It contains the instructions for the machine to perform. These instructions
are in the form of binary digits.
10
5
10/20/2023
Executable code
• The machine understands thd object code and the
computer execute this code.
• Executable code is a file or a program that indicates
tasks according to encoded instructions.
• The CPU can directly execute an executable file to
defined tasks.
11
12
6
10/20/2023
13
Preprocessor Directives
• C++ source code is pre-processed before it is compiled into object code .
• A preprocessor directive, which begins with a # sign (such as #include, #define), tells the preprocessor to perform
a certain action (such as including a header file, or performing text replacement), before compiling the source
code into object code.
• Preprocessor directives are not programming statements, and therefore should NOT be terminated with a semi-
colon.
• #include <iostream> // To include the IO library header
• #include <cmath> // To include the Math library header
• #define PI 3.14159265 // To substitute the term PI with 3.14159265 in this file
• // DO NOT terminate preprocessor directive with a semi-colon
• In almost all of the C++ programs, we use #include <iostream> to include the input/output stream library header
into our program, so as to use the IO library function to carry out input/output operations (such as cin and
cout).
14
7
10/20/2023
15
Structure of a C Program
A C-Language program basically consists of the following parts
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
Example
Output
#include <iostream>
int main( )
{ This is my first program
cout << “this is my first program “;
return (0);
}
8
10/20/2023
THE END
18
9
Lecture-7,8 & 9
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
1
Structure of a C Program
A C-Language program basically consists of the following parts
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
Example
Output
#include <iostream>
int main( )
{ This is my first program
cout << “this is my first program “;
return (0);
}
A First Program Header File:In almost all of the C++ programs, we
use #include <iostream> to include the input/output
stream library header into our program, so as to use
the IO library function to carry out input/output
#include <iostream> operations (such as cin and cout).
int main( ) On startup, control always goes to main(). If there is no
{ function called main() in program, an error
cout << “this is my first program “; will be reported when you run the program.
return (0); The program may consist of many functions, classes,
} and other program elements.
main() calls member functions in various objects to
carry out the program’s real work. The main() function
may also contain calls to other standalone
functions.
The last statement in the cout means character output,
open and close braces function body is return 0;. cout is in the c++ header iostream
mark the beginning This tells main() to return << is called insertion operator
and end the value 0 to cout is an object and it is used along
whoever called it, in this with the insertion operator << in order
case the operating system to display a stream of characters.
or compiler.
But a Complete Program
#include <iostream>
using namespace std;
int main( )
{
cout << “this is my first program “;
cout << “It is a simple program”;
cout<< “I will make advance programs in future”;
return (0);
}
The header File
• The first line of the program is #include <iostream>
• It is not part of a function body and doesn’t end with a
semicolon.
• it starts with a number sign (#) which is called a preprocessor
directive.
• A preprocessor directive is an instruction to the compiler.
• A part of the compiler called the preprocessor deals with these
directives before it begins the real compilation process.
• The preprocessor directive #include tells the compiler to insert another file into your source file.
• The type of file usually included by #include is called a header file.
• The preprocessor directive #include tells the compiler to add the source file IOSTREAM to the
source code file before compiling.
• IOSTREAM is a header file concerned with basic input/output operations, and contains
declarations that are needed by the cout identifier and the << operator.
• Without these declarations, the compiler won’t recognize cout and will think << is being used
• incorrectly.
• There are many such header files in include directory.
5
using namespace std;
• Namespace is a sort of directory in which identifiers
are defined.
• A namespace is a part of the program in which certain
names are recognized; outside of the namespace
they’re unknown.
• Using namespace std; we are telling the compiler to
look for cout in the std namespace, in which many
standard C++ identifiers are defined.
• A C++ program can be divided into different
namespaces.
• with each concerned statement in our program
• A clever alternative is to add (using namespace std;)
above int main() instruction
• This line tells the compiler that it should look in the std
namespace for any identifier we haven’t defined. If we
do this, we can omit the std:: prefix when writing cout.
This is the recommended practice.
• If we do not type (using namespace std;) in the
beginning of a program then whenever we want to
access an identifier defined in a namespace, we tell
the compiler to look for It in that namespace using the
scope resolution operator (::)
6
main()
• When we run the program the first statement that is
executed at the beginning of a function called main().
• Other than the function main a program may consist
of many functions, classes, and other program
elements.
• But on startup, control always goes to main().
• If there is no function called main() in your program, an
error will be reported when you run the program.
• main() calls member functions in various objects to
carry out the program’s real work.
• The main() function may also contain calls to other
standalone functions.
7
return 0;
• The return value of 0 indicates
normal termination
• non-zero (typically 1) indicates
abnormal termination.
• If C++ compiler will automatically
insert a "return 0;" at the end of the
the main() function, thus, it
statement can be omitted.
• Instead of using numeric value of
zero and non-zero, you can also use
EXIT_SUCCESS or EXIT_FAILURE,
which is defined in the cstdlib header
(i.e., you need to "#include
<cstdlib>".
8
Adding Comments
• Comments are used to document and explain your
codes and program logic.
• Comments are not programming statements and
are ignored by the compiler, but they are VERY
IMPORTANT for providing documentation and
explanation for others to understand your program
(and also for yourself few days later).
10
Examples of cout object
// printing a statement using cout
#include <iostream> output
using namespace std;
int main()
{ this is my first program
cout<<“this is my first program “;
return 0;
}
// printing a constant using cout
#include <iostream>
using namespace std; output
int main()
{ the value of a is = 2
int a=2; the value of b is = 3.24
float b=3.24; the value of c =p
char c=‘p’;
cout<< “the value of a is = ”<< a;
cout<< “\nthe value of b is = “<< b;
cout<<“\nthe value of c is =“<<c;
return 0;
11
}
Escape sequence
• These special characters are used
in cout statement to obtain the
result printed in our desired form
• Escape sequences can be used as
separate characters or embedded
in string constants
• Examples:
12
The endl Manipulator
• endl is a manipulator.
• manipulators are instructions to the output
stream that modify the output in various ways.
• this causes a linefeed to be inserted into the
stream, so that subsequent text is displayed on
the next line.
• They are used with insertion operators (<<).
• it has the same effect as sending the ‘\n’
character, but is somewhat clearer.
• in most cases endl and ‘\n’) are equivalent.
13
Examples \n and endl
output
the value of a is = 2
the value of b is = 3.24
the value of c =p
16
cin object
cin object is used to accept the input from
keyboard.
It is associated with the standard C input stream
stdin.
The extraction operator(>>) is used along with
the object cin for reading inputs.
17
Cin object
// taking inputs using cin object;
#include <iostream>
using namespace std; // taking inputs using cin object;
int main() #include <iostream>
{ using namespace std;
int a; int main()
float b;
char c; {
cout<< “enter integer a “; int a;
cin>>a; float b;
cout”\nenter a floating point number b ”; char c;
cin>>b; cout<< “enter integer a, float b and character c”;
cout<<“\nenter a single character c=“ cin>>a>>b>>c;
cin>>c; cout<<“\nthe value of a is = “<<a;
cout<<“\nthe value of a is = “<<a; cout<<“\nthe value of b is = “<<b;
cout<<“\nthe value of b is = “<<b; cout<<“\nthe value of c is = “<<c;
cout<<“\nthe value of c is = “<<c; return 0;
return 0; }
}
18
Operators
1. Arithmetic operators
– Assignment operator
– Increment/decrement
operators
2.Relational operators
3.Logical operators
19
Arithmetic operators
20
Arithmetic operators cont.
increment/Decrement operators
21
Arithmetic operators cont.
Assignment operators
22
THE END
23
11/5/2023
Lecture-10,11 &12
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
Operators
1. Arithmetic operators
– Assignment operator
– Increment/decrement
operators
2.Relational operators
3.Logical operators
1
11/5/2023
Arithmetic operators
Relational Operator
2
11/5/2023
Relational
operators
Let A=3
B=5
Example
#include <iostream>
using namespace std;
int main( )
{
int a = 5, b = 5, c = 10;
cout<< “\n a=5, b=5 and c=10”;
cout<< “\n is The value of a=b\t” << (a==b);
cout<< “\n is a=c\t” << (a==c);
cout<< “\n is a>b\t” << (a>b);
cout<< “\n is a>c\t” << (a>c);
cout<< “\n is a<c\t” << (a<c);
cout<< “\n is a!=b\t” << (a!=b);
cout<< “\n is a!=c\t” << (a!=c);
cout<< “\n is a>=b\t” << (a>=b);
return (0);
}
6
3
11/5/2023
Logical operators
#include <iostream>
Example using namespace std;
int main( )
{
int a = 5, b = 5, c = 10, result;
4
11/5/2023
10
5
11/5/2023
11
simple structured
address
float double long double
pointer reference
12
6
11/5/2023
13
14
7
11/5/2023
Variables Declaration
• These are named memory location
• Variables declared in global or local declaration sections
• Basic types: char, int, float
• Syntax: Type Name;
• Examples:
int sum;
float avg;
char dummy;
8
11/5/2023
Variable Initialization
• Giving a variable an initial value is called initialization
• Variables not necessarily initialized when declared
(value is unpredictable - garbage)
• We Can initialize in declaration:
Example:
int x=2; int x=0; float a=2.43;
int y=5; int y=0; float b=3.20;
int z=7; int z=0;
or
Or Or float a=2.43, b=3.20;
int x=2, y=5, z=7; int x=0, y=0, z=0;
Or
int x, y, z = 0;
Examples of variables
// printing
#include <iostream.h>
int main()
output
{
int a=2;
the value of a is = 2
float b=3.24;
the value of b is = 3.24
char c=‘p’;
the value of c =p
char name[ ] =“pakistan zindabad”;
The name is = Pakistan Zindabad
cout<< “the value of a is = ”<< a;
cout<< “\nthe value of b is = “<< b;
cout<<“\nthe value of c is =“<<c;
cout<<“\nthe name is =“<< name;
return 0;
18
9
11/5/2023
Program 1
Write a program which takes four(4) numbers as
input and prints their sum and Average. Using
simple assignment operators
19
Program 2
Write a program which takes two (2) numbers as
input and exchanges their values.
20
10
11/5/2023
Library Functions
• Many activities in C++ are carried out by library functions.
• These functions perform mathematical computations, and data
conversion, among other things.
• # include <cmath> is to be included when calling library functions
• sqrt(x): for square root
of a number x
• pow (m,n): calculates
power of a number, m
is base and n is power.
• cbrt(x): calculates cube
root of a number x.
• log(x)
• round(x)
21
Program 3
write a program which solves a quadratic equation.
take a, b and c as input from user
b b 2 4ac
x
2a
22
11
11/5/2023
THE END
23
12
11/12/2023
Lecture-13,14&15
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
Loops
1
11/12/2023
Loops
Loops are used to repeat a block of code.
There are three types of loops:
1. for
2. while
3. do…..while.
For loop
for (initialization; condition; step)
{
Statement 1; #include <iostream> Output
Statement 2; 0
int main() 1
…………...
{ 2
Statement n; int x; 3
} for ( x = 0; x < 10; x++ ) 4
{ 5
cout<< “\n” << x ; 6
} 7
return 0; 8
} 9
{
cout<< “\n ” << x;
x++;
}
4
2
11/12/2023
For loop
#include <iostream> The loop will run from x=0 to x=9, i.e 10 times
using namespace std;
int main()
{
int x;
{ // first time { // second time { // third time
x=0; x=1; x=2;
for ( x = 0; x < 10; x++ )
cout<< “\n“ << x;
{ cout<< “\n“ << x; cout<< “\n“ << x;
x++;
cout<< “\n“ << x;
x++; }
} x++;
return 0; } Output
}
} 0
{ // tenth time 1
x=9; 2
cout<< “\n“ << x; 3
.......... 4
5
x++;
6
} 7
cout<< “\n outside loop x=” << x; // What will be 8
printed here??? 9
Outside loop 10 5
Program 3.1
Write a program to print all the 256 ASCII
characters in C-Language using for loop.
#include <iostream> #include <iostream>
using namespace std; Using namespace std;
int main()
int main() {
{ int x;
Make changes in this int x;
for ( x = 0; x < 256; x++ )
program {
for ( x = 0; x < 10; x++ ) cout<< x<< “ “ << (char) x <<“\t”;
{ }
return 0;
cout<< “\n“ << x; }
}
return 0;
}
3
11/12/2023
ASCII Character
chart
Program 3.2
Write a program, by using for loop, which takes
five(5) integers as input and prints their sum and
average.
4
11/12/2023
Program 3.3
Write a program to print alphabets in upper case
from A to Z.
ASCII code of A is 65
for ( i=0 ; i < 10;i++) ; For Loop with no Body ( Carefully Look at the Semicolon )
for (i=0,j=0;i<100;i++,j++) Multiple initialization & Multiple
Statement1; Update Statements Separated by Comma
for ( ; i<10 ; i++) Initialization not used
for ( ; i<10 ; ) Initialization & Update not used
for ( ; ; ) Infinite Loop,Never Terminates
10
5
11/12/2023
11
9 9 9 9 9 9 9 9 9 9 cout <<“\n”;
}// end of external loop
return 0;
}
12
6
11/12/2023
cout <<“\n”;
Make changes in this }// end of external loop
program
return 0;
}
13
cout <<“\n”;
Question: }// end of external loop
How to stop printing at “Z” ?
return 0;
} 14
7
11/12/2023
cout <<“\n”;
How to modify this code }// end of external loop
return 0;
} 15
A
BB
CCC
DDDD
EEEEE
.
.
.
.
15 lines
16
8
11/12/2023
Decisions
• Decision making is about deciding the order of execution of
statements based on certain conditions or repeat a group of
statements until certain specified conditions are met.
• In a program a decision causes a onetime jump to a different
part of the program, depending on the value of an expression.
• Decisions can be made in C++ in several ways.
1. if statement
2. switch statement
3. conditional operator statement (? : operator)
17
Decisions
1-if statement
• The if statement may be implemented in
different ways depending on the complexity of
conditions to be tested.
• The different forms are,
– Simple if statement
– if....else statement
– Nested if....else statement
– Using else if statement (else if ladder)
18
9
11/12/2023
THE END
19
10
11/17/2023
Lecture-16,17&18
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
Decisions
• Decision making is about deciding the order of execution of
statements based on certain conditions or repeat a group of
statements until certain specified conditions are met.
• In a program a decision causes a onetime jump to a different
part of the program, depending on the value of an expression.
• Decisions can be made in C++ in several ways.
1. if statement
2. switch statement
3. conditional operator statement (? : operator)
1
11/17/2023
Decisions
1-if statement
• The if statement may be implemented in
different ways depending on the complexity of
conditions to be tested.
• The different forms are,
– Simple if statement
– if....else statement
– Nested if....else statement
– Using else if statement (else if ladder)
Decisions
1-if statement cont.
The if statement enables you to test for a condition and branch to different
parts of code, depending on the result.
if (expression)
{
statement1;
statement2;
.
.
}
2
11/17/2023
Decisions
1-if statement cont.
• The if statement enables you to test for a condition and branch to
different parts of code, depending on the result.
• Test expression is the conditional criterion.
• If expression is logical TRUE the statements inside the braces are
executed.
• If expression is logical FALSE (zero), then the statements are skipped
if (expression)
{
statement1;
statement2; No
. Semicolon
here (;)
.
}
Decisions
1-if statement cont.
Example:
Write a program which takes an integer as input and checks whether it
is even.
// decisions
#include <iostream>
using namespace std;
int main( )
{ output
int a;
cout<<“ enter an integer ”;
cin>>a; enter an integer 22
if(a%2 == 0) the number is even
{
cout<<“the number is even”;
}
return 0;
} 6
3
11/17/2023
Decisions
2-if – else statement
If the expression is true, the statement-block1 is
executed, else statement-block1 is skipped and
statement-block2 is executed.
Condition
statement not
to be written
with else
Decisions
2-if –else statement cont.
Example:
Write a program which takes an integer as input and checks
whether it is even or odd.
// decisions output
#include <iostream>
using namespace std; enter an integer 22
int main( ) the number is even
{
int a;
cout<<“ enter an integer ”;
cin>>a; output
if(a%2 == 0)
{ cout<<“the number is even”;} enter an integer 13
else the number is odd
{cout<<“the number is odd”; }
return 0;
} 8
4
11/17/2023
Decisions
3-Nested if –else statement cont.
When if expression is true
then execution continues
and enters inside the first
if to perform the check for
the next if block, where if
expression 1 is true the
statement-block1 is
executed otherwise
statement-block2 is
executed.
If the expression given in
first if is false then else in
the last will be execute
the statements of block-3
if(a > b)
Decisions {
if(a > c)
3-Nested if –else statement {
cont. }
cout<<“the largest number is << a;
5
11/17/2023
Decisions
4- else-if ladder
if(expression1)
Else if ladder is usually made when {
multiple conditions are checked to statement block1;
make a decision }
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
11
12
6
11/17/2023
Decisions
Conditional Statement
max=(num1 > num2) ? num1:num2;
14
7
11/17/2023
Decisions
else-if ladder vs Conditional statement
Example: Write a program which takes three integers as input and checks and prints the largest
integer
15
Decisions
Conditional Statement cont.
Write a program which takes 5 integers as input and
prints the largest
#include <iostream.h>
main()
{
int max=0,i, num;
for(i=1;i<=5;i++)
{
cout<< “\n enter integer”;
cin>> num;
max=(max > num) ? max:num;
}
8
11/17/2023
17
18
9
11/17/2023
Program: write a program which takes an integer as input and checks whether it is
prime or not
#include<iostream.h>
int main ( )
{
int i, j;
cout<< “enter an integer greater than or equal to 2”;
cin>>i;
for(j = 2; j <= (i-1);j++)
{
if(i%j==0)
{
cout<<“the number is not prime”;
exit(0);
}
}
cout<< “the number is prime”;
return 0;;
}
19
Practice Program
20
10
11/17/2023
Decisions
Switch Case
In if-else / else-if ladder as the number of
alternatives increases the complexity of program
increases drastically.
• Switch statement is a multi-way decision
statement
21
Decision
if(Condition 1)
Switch Case cont.
Statement 1 switch ( expression )
else {
{ case label1 :
Statement 2 body1
if(condition 2) break;
{
if(condition 3) case label2 :
statement 3 body2
else break;
if(condition 4) case label3 :
{ body3
statement 4 break;
} default :
} default-body
else break;
{ }
statement 5
} next-statement;
22
}
11
11/17/2023
23
24
12
11/17/2023
25
26
13
11/17/2023
27
28
14
11/17/2023
Practice program
Write a program to make a simple calculator to
perform following basic functions
• Addition
• Subtraction
• Multiplication
• division
29
THE END
30
15
11/23/2023
Lecture-19,20 &21
Corse Title: Computer and Programming Language
Course Code: EL-105
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2023
Offered to: F.E. (Electronic Engineering)
While loop
Syntax
while (expression) //to check working of while loop
#include <iostream>
Output
0
{ using namespace std;
int main( )
1
2
Statement 1; {
3
int x = 0; // initialization of counter
Statement 2; while(x<10) // limit of loop 4
{ 5
…………... cout<< x<< “\n” ; 6
x++; //increment of counter 7
Statement n; } 8
return 0;
} }
9
1
11/23/2023
while(condition)
{
while ( condition )
{
9 9 9 9 9 9 9 9 9 9
{
cout<< i;
j++;
} // end of internal loop
cout <<“\n”;
i++;
}// end of external loop
return 0; } // end of main 4
2
11/23/2023
A B C D
E F G H
.
.
.
.
Y Z
do while loop
• the loop condition is tested at the
end of the body of the loop.
• the loop is executed at least one.
Syntax :
do
{
Statement 1;
Statement 2;
…………...
Statement n;
}
while(expression);
3
11/23/2023
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
do
{
cout<< “hello, world!" ;
}
while ( x != 0 );
return 0;
}
7
Practice Program
• Write a program which prints prime numbers
less than 100.
4
11/23/2023
10
5
11/23/2023
Home Work
11
#include <iostream>
#include <conio.h>
using namespace std;
int main()
program: {
take some char ch;
int ca=0,ce=0,ci=0,co=0,cu=0; // initialize all counters
text as input cout<< “ type some text and press enter”;
and counts do
{
each vowel ch=getche();
cin should not be used to take
character input multiple times
separately switch(ch) here
{
and prints case ‘a’: ca++; break;
their count. case ‘e’: ce++; break;
……….
……….
case ‘u’: cu++; break;
default : break;
} // end of switch
} // end of do-while
while (ch !=‘\r’);
cout<< "\n number of a=" << ca << "\n number of e=" << ce << "\n number of i=" <<
ci << "\n number of o=" << co<< "\n number of u=" << cu;
return 0;
} // end of main
12
6
11/23/2023
Practice Programs
Practice all the programs given in “Exercises” in
the end of chapter-3 (Loops and decisions) of
text book
13
THE END
14