Brief Note of C++ CH
Brief Note of C++ CH
Brief Note of C++ CH
Fundamental of programming I 2
Cont..
main ( ) function:
All other functions are called either directly or indirectly from main.
Fundamental of programming I 3
Syntax and Semantics
Syntax :
Fundamental of programming I 4
Cont..
Semantics:
deal with the meanings given to syntactically correct
constructs of the language.
is defined in terms of the program’s run-time behavior
A program could be syntactically correct yet meaningless
Example
sum=0;
while (sum!=-1)
sum=sum+10;
Fundamental of programming I 5
The parts of a simple C++ Program
extension “ .CPP ”
Fundamental of programming I 6
Cont..
Syntax: cout<<Object;
Fundamental of programming I 7
Cont..
Cin:
used for taking input from the keyboard. followed by
the input redirection operator (>>) also called
extraction operator.
take value from the keyboard and store it in the
memory
• Syntax: cin>>Object
Fundamental of programming I 8
Comments on C++ programs
text which explains some aspect of a program.
Example
Fundamental of programming I 9
Cont..
Multiple Line Comment:
Eg:
Fundamental of programming I 10
Variables and Constants
Variable:
Fundamental of programming I 11
Cont..
Fundamental of programming I 12
Declaring Variables
Keywords
Fundamental of programming I 13
Initializing Variables
Fundamental of programming I 14
Scope of Variables
Fundamental of programming I 15
Example
Let's see an example of a global variable.
#include <iostream>
using namespace std;
int g;
int main()
{
int a = 4;
g=a*2
; cout << g << endl;
return 0;
}
Here, g is a global variable since it is declared outside of the main function. Thus unlike the
local variable 'a' which can only be used in the function main, 'g' can be used throughout the
program and can be used in all the functions in the program.
Fundamental of programming I 16
Cont..
Local Variables:
Fundamental of programming I 17
#
Fundamental of programming I 18
Characters
• Char in C++ are represented as any value inside a single
quote
Fundamental of programming I 19
Operators
• Assignment operator
• Arithmetic operator
• Relational operator
Fundamental of programming I 20
Cont..
• Logical operator
• Increment/decrement operator
Fundamental of programming I 21
Assignment operator (=)
Syntax: Operand1=Operand2;
Operand1 : is always a variable
Operand2: can be one or combination of:
A literal constant: Example: x=12;
A variable: Example: x=y;
An expression: Example: x=y+2;
Fundamental of programming I 22
Cont..
Compound assignment operators: (+=, -=, *=, /=, %=, >>=,
<<=, &=, ^=)
Example:
• a -= 5; a = a – 5;
• a /= b; a = a / b;
Fundamental of programming I 24
Relational operator (==, !=, > , <, >=, <=)
Example
• ‘A’ < ‘F’ would return true or 1. it is like (65 < 70)
Fundamental of programming I 25
Logical Operators (!, &&, ||)
Logical negation (!)
• is a unary operator.
Example :
!20 //gives 0
Fundamental of programming I 26
Cont..
Logical AND (&&)
• produces 0 if one or both of its operands evaluate to 0
otherwise it produces 1.
Example :
10 && 5 //gives 1
10 && 0 // gives 0
Fundamental of programming I 27
Cont..
Logical OR (||)
• produces 0 if both of its operands evaluate to 0 otherwise, it
produces 1.
Example :
10 || 5.5 //gives 1
10 && 0 // gives 1
Fundamental of programming I 28
Increment/Decrement Operators: (++) and (--)
The auto increment (++): adding 1 from a numeric variable.
Example
Fundamental of programming I 29
Prefix and Postfix:
prefix :
postfix :
variable.
Fundamental of programming I 30
Example
int k = 5;
Fundamental of programming I 31
The size of() Operator
Example:
a = size of(char)
b = size of(int)
Fundamental of programming I 32
Operator Precedence
Fundamental of programming I 33
Precedence Table:
Fundamental of programming I 34
Cont..
Fundamental of programming I 35
Example
•a = = b + c * d
• a = = (b + c) * d
Fundamental of programming I 36
Individual Assignment Two
1. What is the precedence when there are a global variable in
the program with the same name?
2. What is C++?
3. What is the difference between user defined and
predefined headers?
4. What are the different data types present in C++?
5. Write a C++ program to calculate area and perimeter of
parallelogram
6. Write a C++ program to enter velocity, Acceleration and
Time and print the final velocity using the formula:
V = u+a*t
Fundamental of programming I 37
Chapter Three
Control Flow
Fundamental of programming I 38
Control Flow
»Program flow is a general term which describes the order in
which your lines of code are executed. This means that some lines
will only be read once, some multiple times, and others may be
skipped completely, depending on the situation
Fundamental of programming I 40
Sequential statements
Fundamental of programming I 41
Sequential statements
• Jump statements
Fundamental of programming I 42
Selection statements/ Conditional statements
»Selection statements are statements in a program where there
are points at which the program will decide at runtime
whether some part of the code should or should not be
executed.
Fundamental of programming I 44
The simple if statement
»The simple if statement will decide only one part of the
program to be executed if the condition is satisfied or
ignored if the condition fails.
»The General Syntax is:
if (expression)
Statements;
Fundamental of programming I 45
Cont..
Example
if(age>18)
Fundamental of programming I 46
The if else statement
»Another form of the “if” is the “if …else” statement.
»The “if else” statement allows us to specify two alternative
statements:
• One which will be executed if a condition is satisfied and
• Another which will be executed if the condition is not satisfied.
• The General Syntax is:
if (expression)
statements1;
else
statements2;
Fundamental of programming I 47
Cont..
Example1
if(age>18)// expression
cout<<”you are an adult”;// statements1
else
cout<<”You are a kid”;// statements2
Fundamental of programming I 48
Cont..
Example 2
int x;
cout<<”Enter a number: “;
cin>>x;
if(x%2==0)
cout<<”The Number is Even”;
else
cout<<”The Number is Odd”;
Fundamental of programming I 49
The “if else if” statement
»The third form of the “if” statement is the “if …else if”
statement.
»The “if else if” statement allows us to specify more than
two alternative statements each will be executed based on
testing one or more conditions.
Fundamental of programming I 50
Cont..
The General Syntax is:
if (expression1)
statements1;
else if(expression2)
statements2;
.
.
.
else if(expressionN)
statementsN;
else
statements
Fundamental of programming I 51
Cont..
»First “expression1” is evaluated and if the outcome is none
zero (true), then “statements1” will be executed. Otherwise,
which means the “expression1” then “expression2” will be
evaluated: if the output of expression2 is True then
“statements2” will be executed otherwise the next
“expression” in the else if statement will be executed and so
on.
»If all the expressions in the “if” and “else if” statements are
False then “statements” under else will be executed.
Fundamental of programming I 52
Cont..
Example
if(score >= 90)
cout<< “\n your grade is A”;
else if(score >= 80)
cout<< “\n your grade is B”;
else if(score >= 70)
cout<< “\n your grade is C”;
else if(score >= 60)
cout<< “\n your grade is D”;
else
cout<< “\n your grade is F”;
Fundamental of programming I 53
Nesting If statements within another if statement
»One or more if statements can be nested with in another if
statement.
»The nesting will be used to test multiple conditions to
perform a task.
»It is always recommended to indent nested if statements to
enhance readability of a program
Fundamental of programming I 54
Cont..
The General Syntax :
if (expression1)
{
if (expression2)
statementsN;
else
statementsM;
}
else
{
if (expression3)
statementsR;
else
statementsT;
}
Fundamental of programming I 55
Cont..
Example if(x>z)
cout<<“X is maximum”;
int main() else
{ cout<<“z is maximum”;
}
int x, y, z; Else{
cout<<”\n Enter the If(y>z)
three tnumbers :”; cout<<“y is maximum”;
Else
cin>>x>>y>>z; cout<<“z is maximum”;
if(x >y) return 0;
}
{
Fundamental of programming I 56
The Switch Statement
»Another C++ statement that implements a selection control
flow is the switch statement (multiple-choice statement).
»The switch statement provides a way of choosing between a
set of alternatives based on the value of an expression. The
general form of the switch statement is:
Fundamental of programming I 57
Cont..
Fundamental of programming I 58
Cont..
• The General Syntax might be:
switch(expression)
{
case constant1:
statements;
.
.
case constant n:
statements;
default:
statements;
}
Fundamental of programming I 59
Cont..
Fundamental of programming I 60
Cont..
»After one case is satisfied, execution continues until either a
break statement is encountered or all intervening statements
are executed, which means until the execution reaches the
right French bracket of the switch statement.
Fundamental of programming I 62
Cont..
Example 2: display 5 working days using switch statement
switch (day){
case 1:
cout<<“the day is Monday:”<<endl;
break;
case 2:
cout<<“the day is tuesday:”<<endl;
break;
case 3:
cout<<“the day is wednesday:”<<endl;
break;
case 4:
cout<<“the day is thursday:”<<endl;
break;
case 3:
cout<<“the day is friday:”<<endl;
break;
default: cout<<“ unknown”<<endl;
}
Fundamental of programming I 63
Loop Statements/ Repetition
Fundamental of programming I 64
The for statement / loop
The “for” statement (also called loop) is used to
repeatedly execute a block of instructions until a specific
condition fails.
The General Syntax is:
for(expression1 ; expression2 ; expression3)
statements;
Fundamental of programming I 65
Cont..
Fundamental of programming I 66
Cont..
Thus, first expression1 is evaluated and then each time
the loop is executed, expression2 is evaluated. If the
outcome of expression2 is non zero then statements is
executed and expression3 is evaluated. Otherwise, the
loop is terminated.
In most programs, the “for loop” will be used for such
expressions where expression1 is initialization,
expression2 is condition and expression3 is either
increment or decrement.
Fundamental of programming I 67
Cont..
The general format
for(initialization ; condition ; increase/decrease)
statement;
Steps of execution of the for loop:
1. Initialization is executed. (will be executed only once)
2. Condition is checked, if it is true the loop continues,
otherwise the loop finishes and statement is skipped.
3. Statement is executed.
4. Finally, whatever is specified in the increase or decrease
field is executed and the loop gets back to step two.
Fundamental of programming I 68
Cont..
Example :
for(n=0,i=100; n!=i; n++,i--)
{
//whatever here
}
NB: for loop is used for definite loops when the
number of iteration is known.
Fundamental of programming I 69
Cont..
Example 1
Fundamental of programming I 70
Cont..
Example 2
• for statement adds the even numbers between 0 and n
#include<iostream.h>
int main()
{
int Sum=0;
for(int i=1; i<=4; i+=2)
{
Sum=Sum+i;
}
cout<<Sum;
return 0;
}
Fundamental of programming I 71
Cont..
Example 2
• for statement adds the even numbers between 0 and n
#include<iostream.h>
int main()
{
Fundamental of programming I 73
Example 3
#include<iostream.h> for (k=1;k<=2*i-1;k++)
int main() {
{ cout<<"* ";
int i,j,k;
}
for (i=1;i<=5;i++)
cout<<endl;
{
for (j=5;j>i;j--) }
{ return 0;
cout<<" "; }
}
Fundamental of programming I 74
Cont..
Example 2
• #include<iostream.h>
int main()
{
Fundamental of programming I 76
Cont..
Fundamental of programming I 77
Cont..
While loop: Is used when the number of iteration is not
known. Means you do not know how many times the loop
has to be repeated.
// infinite while loop
int x=1;
while(x==1)
{
// body of the loop
}
Fundamental of programming I 78
Cont..
Example
adds the numbers between 0 and 100
number=1;
sum=0;
while(number <= 100)
{
sum += number;
number++;
}
Fundamental of programming I 79
Do…while loop
Fundamental of programming I 80
Cont..
• First statement is executed and then expression is evaluated. If the
outcome of the expression is nonzero, then the whole process is
repeated. Otherwise the loop is terminated.
Example: adds the numbers between 0 and 100
int number=1;
int sum=0;
do
{sum += number;
number++;
Cout<<sum;
}
while(number <= 100);
Fundamental of programming I 81
Cont..
• First statement is executed and then expression is evaluated. If the
outcome of the expression is nonzero, then the whole process is
repeated. Otherwise the loop is terminated.
Example: adds the numbers between 0 and 100
int number=1;
do
{
// body of loop
}
while(number ==1);
The above condition is always true. Means the loop body will run for
infinite times.
Fundamental of programming I 82
Jump statement in C++
Fundamental of programming I 83
The continue statements
• The continue statement
The continue statement terminates the current iteration of
a loop and instead jumps to the next iteration.
It is an error to use the continue statement outside a loop.
In while and do while loops, the next iteration starts from
the loop condition.
In a “for” loop, the next iteration starts from the loop’s
third expression.
Fundamental of programming I 84
Cont..
• Example
#include<iostream.h>
int main()
{
for(int i=0;i<10;i++)
{ if(i==5)
continue;
cout<<i;
}
return 0;
}
Fundamental of programming I 85
The break statement
Fundamental of programming I 86
Cont..
Example
#include<iostream.h>
int main()
{
for(int i=0;i<10;i++)
{ if(i==5)
break;
cout<<i;
}
return 0;
}
Fundamental of programming I 87
The return statement
Fundamental of programming I 88
Cont..
Example
#include<iostream.h>
int main()
{
for(int i=0;i<10;i++)
{ if(i==5)
return 0;
cout<<i;
}
return 0;
}
Fundamental of programming I 89
The return statement
Fundamental of programming I 90
Cont..
Example
#include<iostream.h>
int main()
{
int n = 4;
if (n % 2 == 0)
goto label1;
else
goto label2;
label1:
cout << "Even" << endl;
return 0;
label2:
cout << "Odd" << endl;
Fundamental of programming I 91