Session-2

INTRODUCTION TO PROGRAMMING
AIDEEN NASIRISHARGH
Tuesday, 8st of Aban
2nd TA Session
LET’S START REAL CODING!

In last session, we did:





Met with Memory and CPU
Met with C++ and compared it, somehow, w/ C and Java
Met with editors and compilers. Suggested DevCpp.
Learnt how to run a “Hello World!” program and compile it!
In this session, we’re planned to:




What’s a program and how/who questions about its exec!
Meet with assignments and operators
Meet with if – The decision statement
While, as a repeating structure
THE STORY: THIS PROGRAM WORKS!
HOW A PROGRAM WORKS

Algorithm!
What’s it? (water boiling sample!)
 Computer is a logical machine and can hear only 0s and 1s, and
nothing in the middle!


A computer Program

One ordered set of instructions to be executed one after another


Each instruction:



They’ll be translated into assembly codes but we an consider them
as is (i.e. complicated commands in one line!)
Gives an info to machine (e.g. defining new variable)
Changes the status of the Machine (its memory, its running line)
Program Counter, a built-in variable for the program,

It’s auto-incremental
SEQUENTIAL STATEMENTS

Samples:


Calculation of a math formula!
Swap!


How to improve it?!
Solving a 2nd order equation
IF, THIS IS THE PROBLEM!
Is all running methods, top to bottom?!
 How to skip one or more lines?
 How to decide what to run, based on current
state of machine (e.g. value of the variables)
 Solution: if!
 Let’s see an example…

 Find
equality
 Find max, min
 Find oddity
WHILE
Doing something until getting to a wanted
statement!
 That is “iteration”! Another core of
programming.
 Syntax?

LET’S DO EXAMPLES!
PRACTICE IS ALL THAT MATTER…
PRACTICE
Find number of bits a number needs
 Number of digits
 Sum of digits
 Reverse number (1435 => 5341)
 Prime Number

 optimizations
ANY QUESTION?
Thank you!
Let’s go for some sleep :D