CPP Week 2 Handout
CPP Week 2 Handout
C++
Vahan
Hovhannisyan
Recap
Loops
Week 2 - Control Structures Summary
Vahan Hovhannisyan
19/Jan/2017
Introduction to
Last Week C++
Vahan
Hovhannisyan
Recap
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
Decision Making
Blocks and Scope Loops
Summary
Decision Making
Loops
Introduction to
Need for Control Statements C++
Vahan
Hovhannisyan
Recap
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
Decision Making
Operator Meaning Example Value Loops
== equal 10 - 3 == 7 true Summary
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
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
if (condition ) Loops
{ Summary
Statements
}
else
{
Statements
}
Introduction to
if...else Example C++
Vahan
Hovhannisyan
Recap
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
Decision Making
Loops
Summary
Vahan
Hovhannisyan
Vahan
Hovhannisyan
Recap
Decision Making
Check weather the function value belongs to the range for Loops
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
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
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
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
Decision Making
Loops
Summary
Vahan
Hovhannisyan
Recap
Decision Making
Loops
Summary
I Blocks and scope
I Decision making
I Loops
Introduction to
Next Week C++
Vahan
Hovhannisyan
Recap
Decision Making
Loops
I Output formatting
I Read/write from/into a file
I User defined functions