0% found this document useful (0 votes)
8 views13 pages

Lec 3

This lecture introduces the structure and syntax of C++ programming, emphasizing the importance of proper statement termination, syntax rules, and the use of comments for human readability. It explains the concept of control flow, variables, and the nesting of repeat statements, highlighting the need for precise syntax and indentation in programming. The session concludes with a discussion on the significance of comments and indentation for both the programmer and anyone reading the code.

Uploaded by

woyib90397
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Lec 3

This lecture introduces the structure and syntax of C++ programming, emphasizing the importance of proper statement termination, syntax rules, and the use of comments for human readability. It explains the concept of control flow, variables, and the nesting of repeat statements, highlighting the need for precise syntax and indentation in programming. The session concludes with a discussion on the significance of comments and indentation for both the programmer and anyone reading the code.

Uploaded by

woyib90397
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

An Introduction to Programming through C++

Professor Abhiram G. Ranade


Department of Computer Science and Engineering
Indian Institute of Technology Bombay
Lecture No. 1 Part – 3
Introduction
Program Structure and Syntax
Welcome back. In the previous segment, we discussed the following things: We discussed the
repeat statement, and the cin, cout statements and some commands to compute mathematical
functions. Actually, we also discussed the statement by which we can reserve locations or cells
in memory.

(Refer Slide Time: 0:46)

Now, in this segment, I am going to make some general remarks about C++ programs. First of
all, let me observe that a program is really a sequence of statements or commands, and it has to
be written in a particular manner. So the keyword ‘main_program’ must appear first, followed by
an open brace. After that, the sequence of statements representing the main program have to be
put in, and finally, the closing brace has to be put in.

A statement or a command is typically terminated by a semicolon. So the semicolon is the C++


equivalent of the full stop of the English language. Just as in the English language, a sentence
ends with a full stop, in C++ a statement or a command ends with a semicolon. And just as you
read sentences top to bottom, left to right on a page written in the English language, similarly,
C++ programs are also executed by reading the statements top to bottom and left to right.

Now, some of the statements require additional data for them to do their work. For example, if I
write ‘forward’ I need to tell the computer how much I want to move forward, so such data is
called an ‘argument’. Similarly, ‘right’ needs to be told how much angle. So this is another
argument, there can be commands which require more than one argument, and we will see them
soon. And there can be commands which do not require any argument at all. So, ‘turtleSim()’ for
example, did not require any argument, and so in that case, we just put that opening and closing
parenthesis immediately.

(Refer Slide Time: 2:53)

Often in the discussion about languages or especially programming languages the term ‘syntax’
is used. The term syntax is used to refer to the form or the grammar which the language is
supposed to follow. So syntax refers to the grammatical rules indicating how commands must be
written and the syntax of programming languages is very strict. What do I mean by that?

Well, ‘right(90)’ cannot be written in any other form. For example, you cannot write it as
right-space-90. That is not allowed, the parentheses are necessary. However, you could put a
space before and after the parenthesis; that is allowed. However, you cannot split the word
‘right’, so you cannot write r-space-i-g-h-t, that is not allowed. You cannot even capitalize the
‘r’, okay? So it is strict in a certain sense and you have to understand in what sense the language
is strict, and you have to adhere to it very very very very precisely.

So, ‘penUp()’, for example cannot be written as ‘penup()’, so the ‘u’ is expected to be
capitalized. Similarly, the parentheses are needed, so you cannot just leave out the parentheses.
There are other statements also which we will learn and they will also have their syntax and
programs will have to be written strictly in that syntax. You might say that look oh what is the
big difference between ‘penup’ and ‘penUp’, is not it common sense? Well it may be common
sense to you and me, but a computer requires you to be exact, so a computer will think of
‘penUp’ as something different from ‘penup’. So, if you want ‘Up’ you have to say capital up.

However, in spite of all this, there is a lot of flexibility allowed. So for example, I already said
that you can put spaces which do not split a word or split a number, so you can put a space
before or after the parentheses. In addition there are some other natural features as well. So,
wherever we say that look put a number over here, usually you will allow a numerical expression
also. So for example in a repeat or in a right rather in the right we can put an expression such as
360 divided by nsides, or it could be n, if n is the name of a variable.

So, wherever a number is going to appear you can put in the name of a cell in memory or you
can put in an expression, which depends on some values, so that kind of flexibility is there. And
also there is another flexibility which is that we talked about the repeat statement which has a
body and we said that the body consists of several statements, well one of these statements could
be a repeat statement itself.
(Refer Slide Time: 5: 57)

We will come to the example of a repeat inside a repeat, but let me make a few additional
remarks. So first of all I am going to tell you something called ‘comments’. Now, a program is
going to be executed on the computer, but it will also be read by people. So you might write a
program and your teacher might read it. If you are working, your boss or your colleagues might
read the program you have written, or you might write a program and it might be so good, that
you may give it to several other people, all of whom might read that program.

Now, if you write a program, it has to be understandable. So you have to you might want to say
something more in addition to the statements on it. So for this purpose, you can place something
called ‘comments’ in your program, okay.

How do you place comments? Well you can put comments between a ‘/*’ sequence of characters
and a ‘*/’ sequence of characters. So whatever appears between these two sequences will be
thought of as a comment. Alternatively, I can put ‘//’ and then I can put a comment after it and
the comment ends when the line ends. So typically, the first from is used to define a long
comment which extends over several lines, the second form is used to define a short comment
which really extends over a single line or which really put at the end of a line okay.
Now a comment is only meant for the human readers, and the computer completely ignores, the
computer does not care what you put in a comment.

(Refer Slide Time: 7:40)

So we had our program for drawing that polygon, so I have put two comments in this, so at the
top I have put a comment which tells who the author of that program is, and it tells the purpose
of that program. So this is usually good practice. You should put your names on programs so that
if somebody has questions, they can come and talk to you and also you might as well tell them
what is that program going to do and then over here, we did something like dividing 360 by
nsides.

A reader might look at this and say, “Look, why are they dividing 360 by nsides?” Now, if you
want to help out those readers, you can say that the exterior angle of an nsided polygon is 360 by
n. I want to draw an nsided polygon and therefore, I will turn by the angle 360 divided by n, or in
this case, nsides.

So that is how comments help, they do not help the computer but they help human readers and
you are extremely extremely emphatically encouraged to write comments. For one thing, you
will help your friends and boss and your teacher, but you will realize that you write a program
today, in a week or so you will completely forget what you have written. Maybe you look at this
program and ask yourself oh why did I write 360 upon nsides? To help yourself at least, put in a
comment. You will be really really grateful and this is a required good practice if you become a
professional programmer.
(Refer Slide Time: 9:22)

Another important practice and another part of how you write a programs is so called
‘indentation’. So here is the same program and I am going to tell you what indentation is inside
it. So, notice that at the beginning of some lines some space is inserted. So for example, before
this turtleSim, some space is there. Iin the body of the repeat there is a space which offsets it
from the repeat statement itself, so this space is called indentation. The indentation allows you to
quickly see which statements constitute the body of the repeat, or which statements are parts of
the main program. So for example, this ‘include<simplecpp>’ is not a part of the main program,
whereas all these statements are, so by offsetting, I can visually quickly know that look this is the
main program over here or I can visually quickly note that oh this is not only in the main
program, but it is also in a repeat statement inside the main program.

So you may guess the general rule if some x is inside some y, then typically you will put two
additional spaces before every line of x. So that is what has happened over here - for the body of
the repeat which is sort of supposed to be inside the repeat statement and these are the statements
inside the main program so we have put two spaces over here.

Then there is also a style that you have to master, as to how you write the opening brace and the
closing brace. So you can see that the closing brace reverts back to the old indentation. So again
it is considered extremely unfriendly if you do not use indentation. And again, for your own
benefit, you should use indentation because when you come back to the program it will make it
much easier for you to understand what you yourself have written. And of course indentation is
useful for human readers, but it is ignored during execution.

(Refer Slide Time: 11:49)

Now, we said that the body of a repeat itself can contain a repeat statement. So here is an
example. So we have a repeat(4) and inside that, there is a repeat(3) and a right(90) statement, so
how does this execute? Well the first rule is that whatever is inside this outer repeat is going to
be executed 4 times. So what is inside this outer repeat? Well there is a repeat(3) and a right(90),
so we are going to execute this statement 3 times, then we are going to execute this statement
once, then we are going to go back again execute thrice, execute once and so on, do this 4 times.
That is what the rule is there is no real mystery as such, so let us try to figure out what this is
doing.
(Refer Slide Time: 12:44)

So let us just forget one just iteration of this repeat, what is it doing? So it is going to go forward
10 steps, so let me draw that, and to begin with, let us assume that the pen is down. So here is the
turtle and it goes forward 10 pixels. At this point the pen goes up, once the pen goes up the turtle
goes again forward 10 steps, but it goes forward and this time the pen is not down so there is no
line drawn, but at this point the pen is put down again, so this finishes one iteration, what
happens in the second iteration? Again forward line is drawn again a movement is there without
any line being drawn and one more iteration a line is drawn and there is a movement again okay?
So this finishes one iteration of that repeat 3 statement.

After that we continue forward and now there is a right(90), so earlier the turtle was pointing in
this direction, so right(90) will cause it to point in this direction. So that ends an iteration of the
outer repeat. So again we start from the very beginning, so we go back to the top of the repeat(4)
and we start executing, so now we are executing the second iteration. So for the second iteration
again the repeat(3) is going to be executed, so this will again cause forward(10) and then penUp,
and forward, but this time the pen is high so that nothing will be drawn.

So that is just one iteration of the inner repeat, so one more iteration movement, one more
iteration which consists of this movement and again we come to the bottom of the repeat(4), this
time at the end of the second iteration. So at this point the turtle was pointing in the downward
direction, it now turns right and points in this direction.

So now one more iteration we will call again this movement forward so drawing but forward
without movement. Movement and drawing, forward without movement. Movement and
drawing, forward without movement and again we will turn right, so the turtle will start pointing
in this direction, so again it will execute forward and draw, forward without draw, forward and
draw, forward without draw, forward and draw, forward without draw. So the turtle will end up
at the same place after that it will again turn right so it will end up facing in this direction.

So, the turtle will start facing right over here, it will draw these dashed lines, turn, draw these
dashed lines, turn, draw these dashed lines, turn, draw these dashed lines and again turn and
come back to the same position and have the same orientation okay. So notice that repeat within
repeat is very very useful. Imagine, if I had wanted to draw a long line with lots of dashes, I just
have to make that repeat(100). If I wanted 100 dashes, I do not have to write 100 statements.

(Refer Slide Time: 15:47)

So again, a quick summary of what I just said in that example, in general if I have a
repeat(x){yyy statement}, it means execute ‘yyy’, ‘x’ times. Now, if ‘yyy’ itself contains
repeat(w){zzz}, then ‘zzz’ is executed ‘w’ times in each execution of ‘yyy’, or, in each iteration
of that outer repeat(x), this repeat(w){zzz} is going to be executed.

And in a single execution of repeat(w){zzz}, ‘zzz’ is actually executed ‘w’ times. Now I can
have as many repeats going on inside each other, or ‘nested’ inside each other. So what is the
logic there? Exactly similar.

(Refer Slide Time: 16:32)

Alright, so as a quick exercise, what do you think this program is going to do? So figure this out
yourself and only then advance forward. So, give yourself a few seconds. So I will not explain it,
but I will just show you the solution and so you can compare your answer with it. You can also
type this program in, and you can see exactly what it does.
(Refer Slide Time: 16:58)

So the program will print this.

(Refer Slide Time: 17:10)

So now, I want to talk about some terms which you will hear commonly when you discuss
programming. So you will hear people say that ‘control’ is at statement ‘w’, this means that the
computer is currently executing statement w. Or people talk about control flow. Control flow is
how the control moves or the order in which statements get executed. So execution starts at the
top and goes down and the execution is retraced if there is a repeat statement. So the control
flows over and over the body of a repeat statement.

The term ‘variable’ is also used. A variable is simply the region of memory designated for
storing some value that you need. So the nsides that we define is actually going to be called a
variable. So we said that we use a cell in memory but that cell is going to be called a variable
because we have asked for it, and we are going to store some specific kinds of values over there.
Why is it called a variable? Well, because we are allowed to change the value that we store over
there, exactly how this change happens, we will see a little bit later okay?

(Refer Slide Time: 18:32)

So here is what we discussed at this segment: we discussed the general notion of a program, we
discussed the notions of syntax and terms such as control flow, and then we made this
observation that repeat statements can be nested inside other repeat statements. So we will take a
quick break.

You might also like