Showing posts with label introduction to programming. Show all posts
Showing posts with label introduction to programming. Show all posts

Conditional Statements

Tuesday, June 12, 2012
In every day life, we are often making decisions. We perform different tasks while taking decisions. For example, the statement ‘Bring one litre of milk while returning home from college, if the milk shop is open’ involves this phenomenon.
In this statement, there is an element of decision making. We bring one litre of milk if the shop is open. And if the shop is closed, we come back to home without milk.
Thus we are making a decision on the condition that the shop is open. The decision-making process is everywhere in our daily life. We see that the college gives admission to a student if he has the required percentage in his previous examination and/or in the entry test. Similarly administration of a basketball team of the college decides that the students having height more than six feet can be members of the team.
For writing interesting and useful programs, we have to develop the decision making power in them.
Now we will see what kind of decisions are in programming and how these can be used.

Every programming language provides a structure for decision making.
'C' also provides this structure. The statement used for decisions in 'C' language is known as the 'if statement'. The if statement has a simple structure. That is



We explain it as if condition is true, then execute the statement or a group of statements. Here the condition is a statement which explains the condition on which a decision will be made. We can understand it from the example that Ali can become the member of the basket ball team if he has a height more than six feet .In this case, the condition will be



We have written the condition in English language. Now let's see how we can implement this in terms of variables, operators and C statements. In the program, we will write the condition in parentheses, followed by a statement or group of statements to be executed.
Now here is the concept of block of statements. We use braces { } to make a group (block) of a number of statements. We put ‘{’ before first statement and ‘}’ after the last statement. Thus if we have to do many things after the if statement. The structure of if statement becomes as under



Note the indentation of the lines and semi-colon after each statement. Semi-colons are necessary after every C statement. The indentation is only a matter of style. It makes the code easy to read and understand from where a block starts, ends and what kind of block it is. It does not affect the logic of the program. But the braces can affect the logic. We can also write a comment line to state the purpose of code block.
Let's consider a simple example to explain the if statement. Suppose, we have ages of two students (say for the time being we have got these ages in variables). These variables are- age1 and age2. Now we say that if the age1 is greater than age2, then display the statement ‘Student 1 is older than student 2’.
The coding for this program will be as below



Here, in our code we see a new operator i.e. ‘ > ‘ (greater than) in the if statement. We need such operators (like greater than, less than, equal to etc) while making decisions. These operators are called 'relational operators'. These are almost the same relational operators we use in algebra.
Following table summarizes the relational operators.








AlgebraicIn C languageExampleMeaning
Greater than > > x > yx is greater than y
Equal to = == x == yX is equal to y
Less than < < x < yX is less than y
Greater than or equal to>=x >= yx is greater than or equal to y
Less than or equal to<=x <= yx is less than or equal to y
Not equal to!=x != yx is not equal to y

Note that there is no space between ==, >=, <= and !=. These are considered as single operators. The operator == (equal to) is different from the operator =. We know that operator = is the assignment operator which is used in assignment statement to assign a value to a variable. Don't confuse the assignment operator (=) with equal to operator (==). If we write single = in condition of if statement. For example, if we write if ( x = 2 ), the compiler will not give error. This means that it is not a syntax error. The conditional expression in if statement returns a value. In this case, x = 2 will also have some value but it will not in the form of true or false. So it will create a logical error. So be careful while using equal to condition in if statement. For previous lesson click here: Use of Operators
For next lesson click here: Flow Charting


the easiest way to learn programming
introduction to programming
Conditional Statements

Variables

Monday, May 28, 2012
We store every kind of data in variables. Variables are locations in memory for storing data. The memory is divided into blocks. It can be viewed as pigeon-holes. You can think of it as PO Boxes also. In post offices there are different boxes and each has an address. Similarly in memory, there is a numerical address for each location of memory (block). It is difficult for us to handle these numerical addresses in our programs. So we give a name to these locations. These names are variables. We call them variables because they can contain different values at different times.

The variable names in C may be started with a character or an underscore ( _ ). But avoid starting a name with underscore ( _ ). C has many libraries which contain variables and function names normally starting with underscore ( _ ). So your variable name starting with underscore ( _ ) may conflict with these variables or function names.

In a program every variable has:
  • Name
  • Type
  • Size
  • Value
The variables having a name, type and size (type and size will be discussed later) are just empty boxes. They are useless until we put some value in them. To put some value in these boxes is known as assigning values to variables. In C language, we use assignment operator for this purpose.

For previous lesson click here: Our First Program,c++ First program
For next lesson click here: Assignment Operator


the easiest way to learn programming
introduction to programming
Variables

Our First Program,c++ First program

Sunday, May 27, 2012
Let’s write our first program in C and understand the basics of C program by explaining it line by line.

# include

main()

{
cout << "hello!"; }


The first line is # include

This is preprocessor directive. The features of preprocessor will be discussed later. For the time being take this line on faith. You have to write this line. The sign # is known as HASH and also called SHARP.

The next line contains main().

There is a main() in every C program. It occurs once in a program. When we write a program and it compiles successfully, it converts into an executable program (file). Then we execute it by typing the command or by double clicking in graphical interface. The system then loads the program into memory. Now the question arises from where the execution should start. In C programs the execution always starts from the main(). In large programs there may be many modules. But the starting point of the program will always be the main function.

Notice that there are parentheses (“( )”, normal brackets) with main. Here the parentheses contain nothing. There may be something written inside the parentheses. It will be discussed in next lectures.

Next, there is a curly bracket also called braces("{ }"). Here is a thing to remember that brackets (parentheses ( ) and braces { }) always occur in pairs. The body of main is enclosed in braces. Braces are very important in C; they enclose the blocks of the program.

The next line in the program,

cout << “hello!”;

is a statement in C language. There are many things in this line to be discussed. Let’s see them one by one.

The word ‘cout’ is known as stream in C and C++. Stream is a complicated thing, you will learn about it later. Think a stream as a door. The data is transferred through stream, cout takes data from computer and sends it to the output that is the screen of the monitor. So we use cout for output.

The sign << indicates the direction of data. Here it is towards cout and the function of cout is to show data on the screen. The thing between the double quoutes (“ ”) is known as character string. In C programming character strings are written in double quotes. Whatever will be written in quotation marks the sign << will direct it towards cout which will show it on the screen. There is a semicolon (;) at the end of the statement. This is very important. All C statements end with semicolon (;). Missing of a semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation. The only semicolon (;) on a line is a null statement. It does nothing. The extra semicolons may be put at the end but are useless and aimless. Do not put semicolon (;) at a wrong place, it may cause a problem during the execution of the program or may cause a logical error. In this program we give a fixed character string to cout and the program prints it to the screen as: hello!

For previous lesson click here: IDE (Integrated Development Environment)
For next lesson click here: Variables


the easiest way to learn programming
introduction to programming
Our First Program,c++ First program

What skills are needed

Saturday, May 26, 2012
Programming is an important activity as people life and living depends on the programs one make. Hence while programming one should
o Paying attention to detail
o Think about the reusability
o Think about good user interface
o Understand the fact the computers are stupid
o Comment the code liberally


For previous lesson click here: Why Programming is important
For next lesson click here: Paying attention to detail


the easiest way to learn programming
introduction to programming
What skills are needed

Why Programming is important

The question most of the people ask is why should we learn to program when there are so many application software and code generators available to do the task for us. Well the answer is as give by the Matthias Felleisen in the book ‘How to design programs’
“The answer consists of two parts. First, it is indeed true that traditional forms of programming are useful for just a few people. But, programming as we the authors understand it is useful for everyone: the administrative secretary who uses spreadsheets as well as the high-tech programmer. In other words, we have a broader notion of programming in mind than the traditional one. We explain our notion in a moment. Second, we teach our idea of programming with a technology that is based on the principle of minimal intrusion. Hence, our notion of programming teaches problem-analysis and problem-solving skills without imposing the overhead of traditional programming notations and tools.”
Hence learning to program is important because it develops analytical and problem solving abilities. It is a creative activity and provides us a mean to express abstract ideas. Thus programming is fun and is much more than a vocational skill. By designing programs, we learn many skills that are important for all professions. These skills can be summarized as:
o Critical reading
o Analytical thinking
o Creative synthesis


For previous lesson click here: what is programming
For next lesson click here: What skills are needed


the easiest way to learn programming
introduction to programming
Why Programming is important

What is programming

Definition: "A program is a precise sequence of steps to solve a particular problem.” It means that when we say that we have a program, it actually means that we know about a complete set activities to be performed in a particular order. The purpose of these activities is to solve a given problem. Alan Perlis, a professor at Yale University, says: "It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? " It is a sarcastic statement about modern education, and it means that the modern education is not developing critical skills like planning, organizing and paying attention to detail. Practically, in our day to day lives we are constantly planning, organizing and paying attention to fine details (if we want our plans to succeed). And it is also fun to do these activities. For example, for a picnic trip we plan where to go, what to wear, what to take for lunch, organize travel details and have a good time while doing so. When we talk about computer programming then as Mr. Steve Summit puts it “At its most basic level, programming a computer simply means telling it what to do, and this vapid-sounding definition is not even a joke. There are no other truly fundamental aspects of computer programming; everything else we talk about will simply be the details of a particular, usually artificial, mechanism for telling a computer what to do. Sometimes these mechanisms are chosen because they have been found to be convenient for programmers (people) to use; other times they have been chosen because they're easy for the computer to understand. The first hard thing about programming is to learn, become comfortable with, and accept these artificial mechanisms, whether they make ``sense'' to you or not.

For next lesson click here: Why Programming is important


the easiest way to learn programming
introduction to programming
what is programming

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger