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

CPP Week 2 Handout

finance math C++

Uploaded by

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

CPP Week 2 Handout

finance math C++

Uploaded by

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

Introduction to

C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Introduction to C++ Decision Making

Loops
Week 2 - Control Structures Summary

Vahan Hovhannisyan

19/Jan/2017
Introduction to
Last Week C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops

Summary
I Introduction C++
I Your first C++ code
I Types and arithmetic in C++
Introduction to
This Week C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making
Blocks and Scope Loops

Summary

Decision Making

Loops
Introduction to
Need for Control Statements C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making
I The code is usually not implemented linearly, thus Loops
control statements and decision making are needed Summary
I Loops, example
I For all x = 1 to 100 do something
I Decision making
I Logical expressions
I Relational operators: ==, >, etc, with two possible
outcomes: true or false
Introduction to
Relational Operators C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making
Operator Meaning Example Value Loops
== equal 10 - 3 == 7 true Summary

!= not equal 10 - 3 != 7 false


>= greater or equal 10 - 3 >= 7 true
> greater 10 - 3 > 7 false
<= less or equal 10 - 3 <= 7 true
< less 10 - 3 < 7 false
Introduction to
Blocks and Scope C++

Vahan
Hovhannisyan

Recap
I Block is a sequence of statements enclosed in {}: Blocks and Scope

{ Decision Making

Loops
c o u t << ” H e l l o ” << e n d l ;
Summary
}
I One-line blocks are called compound statements: {}
I Variables declared in a block have that block as their
scope
{
int a;
}
Introduction to
Scope Examples C++

Vahan
Hovhannisyan

Recap

I By default the variable defined inside the scope is used Blocks and Scope

Decision Making
I Example
Loops

int a = 5; Summary

{
int a = 3;
c o u t << ” I n t h e i n n e r b l o c k a = ”
<< a << e n d l ;
}
c o u t << ” I n t h e o u t e r b l o c k a = ”
<< a << e n d l ;
Introduction to
Tutorial: Guess the Output C++

Vahan
Hovhannisyan
int i1 = 1 , i2 = 2 , i3 = 3;
i n t main ( ) Recap

{ Blocks and Scope

Decision Making
int i1 = i2 + i3 ;
Loops
int i2 = i1 ∗ i3 ;
Summary
cout <<i 1 <<” \ t ”<<i 2 <<” \ t ”<<i 3 <<e n d l ;
{
int i3 = i1 + 2;
cout <<i 1 <<” \ t ”<<i 2 <<” \ t ”<<i 3 <<e n d l ;
{
i n t i 2 = ++i 3 ;
cout <<i 1 <<” \ t ”<<i 2 <<” \ t ”<<i 3 <<e n d l ;
}
}
return 0;
}
Introduction to
The if and if...else Statements C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

The general form is the following: Decision Making

if (condition ) Loops

{ Summary

Statements
}
else
{
Statements
}
Introduction to
if...else Example C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops
Check if the number is even or odd.
Summary
i f ( n % 2 == 0 )
c o u t << n << ” i s e v e n . ” << e n d l ;
else
c o u t << n << ” i s odd . ” << e n d l ;
Introduction to
Tutorial: Quadratic Equation (Full) C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops

Summary

Solve a quadratic equation for all possible cases


Introduction to
More Logical Operators C++

Vahan
Hovhannisyan

I && - both statements must be true Recap

Blocks and Scope


i f ( x > 0 && x < 1 0 )
Decision Making
{
Loops
c o u t << x << ” i s i n t h e i n t e r v a l ” Summary
<< ” ( 0 , 1 0 ) ” << e n d l ;
}
I || - at least one of the statements is true
i f ( x < 0 | | x > 10)
{
c o u t << x << ” i s o u t s i d e t h e ”
<< ” i n t e r v a l [ 0 , 1 0 ] ” << e n d l ;
}
Introduction to
Tutorial: Value Range C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Check weather the function value belongs to the range for Loops

any input value of x: Summary

1. y = x 2 − 10x; for y ≥ 0
2. y = sin2 (x) + 2 cos(x); for y ∈ [−0.5, 0.3)
1
3. y = e x + ln(2x); for y ∈ [0.001, 0.01] or [100, ∞)
Introduction to
Truth Table C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops
p q p && q p || q Summary
true true true true
true false false true
false true false true
false false false false
Introduction to
Truth Table C++

Examples Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops
Expression Value Summary
(6 <= 6) && (5 < 3) false
(6 < 6) || (5 >= 3) true
(6 <= 6) || (5 < 3) true
(6 >= 6) && (5 != 3) true
(6 > 6) && (5 == 3) false
Introduction to
for statement C++

Vahan
Hovhannisyan

Recap

I The general form: Blocks and Scope

Decision Making
for (init; repetition condition; expr)
Loops
{
Summary
Statements;
}
I example
f o r ( i n t i = 1 ; i <= 1 0 ; i ++)
{
c o u t << i ∗ i << e n d l ;
}
Introduction to
Tutorial: Sum of Squares C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops

Summary

Calculate the sum of squares from 1 to n.


Introduction to
Summary C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops

Summary
I Blocks and scope
I Decision making
I Loops
Introduction to
Next Week C++

Vahan
Hovhannisyan

Recap

Blocks and Scope

Decision Making

Loops

I More loops Summary

I Output formatting
I Read/write from/into a file
I User defined functions

You might also like