Lecture2
Lecture2
Puru
#include <simplecpp>
main_program{
turtleSim();
repeat(8){
forward(100);
right(45);
}
}
repeat(8) { repeat(num_sides){
forward(10); forward(10);
right(45); right(360.0/num_sides);
} }
} }
#include <simplecpp>
main_program{
turtleSim(); Tell the computer: Reserve space in
your memory where I can store an
integer (int). I will refer to it by the
int num_sides; name num_sides
repeat(num_sides){
forward(10); We need some magic so that
num_sides can have the right
right(360.0/num_sides);
value
}
}
Divide the number 360 by the number stored in the
space named num_sides and pass the result as
an argument to this command
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 9
explanation
#include <simplecpp>
int num_sides;
exterior_angle = sum_exterior/num_sides;
repeat(num_sides) {
forward(side_length);
right(exterior_angle);
}
}
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 12
formatting: indentation, grouping, naming
#include <simplecpp> #include <simplecpp> main_program{
turtleSim(); cout << No. of sides? ;
main_program{ int n; cin >> n; repeat(n) {
turtleSim(); forward(200); right(360.0/n);} }
int num_sides;
repeat(num_sides) {
forward(200);
right(360.0/num_sides);
}
}
You will lose marks and your sleep for bad formatting
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 13
language syntax
• syntax = grammatical rules indicating how
commands must be written
• syntax of programming languages is very strict,
e.g.
– right(90);cannot be written as right(90)
penUp() cannot be written as penup()
or even penUp, i.e., without parentheses
#include <simplecpp>
main_program{
cout << a ;
repeat(5){
cout << b ;
repeat(2){ cout << c ; }
cout << d ;
}
}
abccdbccdbccdbccdbccd
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 16
curly braces group statements
repeat(4){} repeat(4){ repeat(4){
forward(50); forward(50);
forward(50);
right(90); }
right(90); wait(2); right(90);
wait(2); } wait(2);
02/08/19
Autumn 2019 CS101@CSE IIT Bombay 20
homework/practice
draw a square with dashed lines
repeat(4){
…
}
32 bits
int sides;
Results in a memory location being reserved for this variable. The
program will refer to it by the name sides
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 24
variable names: identifiers
• sequence of letters, digits & the underscore _ character
exterior_angle = sum_exterior/num_sides;
e = s/n
• expression
a constant (a number) or
a variable name or
a formula involving constants or variables or
a function call
x = rt;
x = r*s;
r = x*y + p*q;
s = x*(y+p)*q;
t = x – y + p – q;
t = x – y + p – q; // equal precedence,
// so evaluated left to right,
// t becomes (((2-3)+4)-5 = -2
• e.g. x*q
if varA, varB have the same data type: the result will have same
data type
• if varA, varB have different data types: the result will have more
expressive data type
• int/short/unsigned int are less expressive than float/double
• shorter types are less expressive than longer types
• we will only use int and double in this course
r = p * q; // 12 stored into r
cout << r << endl; // 12 printed
p = p + 1 is false in mathematics
“=” in C++ is different from “=” in mathematics
5! = 1 x 2 x 3 x 4 x 5
main_program{
int n, nfac=1, i=1;
cin >> n;
repeat(n){ Accummulation idiom
nfac = nfac * i;
i = i + 1;
}
cout << nfac << endl;
Sequence idiom
}
for lab related logistics, please visit the web page, every
week … Lab section to day of the week may change
repeat(4){
repeat(3){
forward(50); penUp();
forward(50); penDown();
}
right(90);
}