Operators, Loops And Decisions-Introduction To Programming-Lecture Notes, Study notes of Computer Programming

A program is a precise sequence of steps to solve a particular problem. This course includes basic programming structure like loops, operator, memory allocation, reference, pointers etc. It teaches how to be a good programmer. This lecture handout is about: Operators, Loops. Decisions, Bitwise, Manipulation, Assignment, Variables, Design, Statements, Switch, Functions, Arrays, Pointers, Nested

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

anchal
anchal 🇮🇳

4.6

(9)

95 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
255
Lecture No. 22
Reading Material
Deitel & Deitel - C++ How to Program Review previous lectures
Summary
x Bitwise Manipulation and Assignment Operator
x Design Recipes
x Variables
x Data Types
x Operators
-
Arithmetic operators
-
Logical operators
-
Bitwise operators
x Programming Constructs
x Decisions
-
if statement
-
Nested if statement
x Loops
-
while loop
-
do-while loop
-
for loop
x switch, break and continue Statements
x Functions
-
Function Calling
-
Top-Down Methodology
x Arrays
x Pointers
x File I/O
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Operators, Loops And Decisions-Introduction To Programming-Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!

Lecture No. 22

Reading Material

Deitel & Deitel - C++ How to Program Review previous lectures

Summary

 Bitwise Manipulation and Assignment Operator  Design Recipes  Variables  Data Types  Operators

  • Arithmetic operators
  • Logical operators
  • Bitwise operators  Programming Constructs  Decisions
  • if statement
  • Nested if statement  Loops
  • while loop
  • do-while loop
  • for loop  switch, break and continue Statements  Functions
  • Function Calling
  • Top-Down Methodology  Arrays  Pointers  File I/O

docsity.com

Bitwise Manipulation and Assignment Operator

Last time we discussed bitwise operators, we will continue with the elaboration of bitwise manipulation and assignment operator.

C/C++ are well constructed languages, at start we used to write:

a = a + 1;

This is used to increment the variable. Then we came to know of doing it in a different manner:

a += 1;

This is addition and assignment operation using single operator +=.

The same thing applies to bitwise operators; we have compound assignment operators for & (bitwise AND), | (bitwise OR) and ^ (bitwise exclusive OR). It is written in the same way as for the above mentioned arithmetic operators. Suppose we want to write:

a = a & b;

It can be written as:

a &= b;

Similarly for | and ^ operations we can write the statement in the following fashion.

a |= b; and a ^= b;

Remember, the ~ (NOT) operator is unary as it requires only one operand. Not of a variable a is written as: ~a. There is no compound assignment operator available for it.

Now we will recap topics covered in the previous lectures one by one.

Design Recipe

Our problems, typically, are of real world nature, e.g., Payroll of a company. These problems are expressed in words. As a programmer we use those words to understand the problem and to come up with its possible solution. To begin with the comprehension and resolution process, we analyze the problem and express the problem in words in reduced and brief manner. Once we have reduced it into its essence, we put some examples to formulate it. For example, if the problem is to calculate the annual net salary of employees, we can take an example for a

docsity.com

Arithmetic Operators

+ operator is used to add two numbers, - is used to subtract one number from the other, ***** is used to multiply two numbers, / is used to divide numbers. We also have a modulus operator % , used to get the remainder. For example, in the following statement:

c = 7 % 2;

7 will be divided by 2 and the remainder 1 will be the stored in the variable c. We also used this operator in our programs where we wanted to determine evenness or oddness of a number. There are also compound arithmetic operators += , -= , *= , /= and also %= for our short hand. It is pertinent to note that there is no space between these compound operators.

Logical Operators The result for logical operators is always true or false. && (AND operator) and || (OR operator). Logical Comparison operators are used to compare two numbers. These operators are: < , <= , == , > , >=. Don't confuse the == operator of equality with = operator of assignment. It is important for us to remember the difference between these two operators of equality (==) and assignment (=). However, C/C++ creates a little problem for us here. When we write a statement as:

a = b;

The assignment statement itself has a value, which is the same as that of the expression on the right hand side of the assignment operator. We can recall from our last lecture that we only wrote a number inside the if statement. We also know that if the resultant inside the if statement is non-zero then its code block is executed. In case, the result is zero, the control is transferred to the else part. If we want to compare two variables a and b inside if statement but wrongly write as:

if ( a = b ) { // if code block

// do something } else { // do something else }

In this case, if the value of the variable b is non-zero (and hence value of the statement a = b is non-zero) then if code block will be executed. But this was not required, it is a logical fault and compiler was unable to detect it. Our objective was to

docsity.com

compare two variables. For that purpose, we should have used assignment operator == for that as:

if ( a == b )

One should be very careful while using comparison operators. You should not miss any case of it and be sure about what you wanted to do and what will be the output of a comparison statement.

You should keep in mind straight line of Calculus for the sake of completeness; you should always divide your domain into two regions. If we take >= as one region then the other region is <. Similarly if we say < as a region , the other region is >=. Depending on the problem requirements, these regions should be very clear.

Bitwise Operators

& is bitwise AND operator, | is bitwise OR operator, ^ is bitwise Exclusive OR operator and ~ is bitwise inversion or NOT operator. ~ (NOT operator) is unary operator as it requires one operator and the remaining operators &, | and ^ are binary operators because they require two operands.

Programming Constructs

For us, it is not necessary to know who is the one to devise or decide about these constructs to be part of the program logic. The important thing is the concept of programming constructs, required to write a program. We have earlier discussed three constructs.

  1. The sequential execution of statements of a program. Execution of statements begins from very first statement and goes on to the last statement.
  2. Secondly we need decisions that if something is true then we need to do something otherwise we will do something else. We use if statement for this.
  3. The third construct is loops. Loops are employed for repetitive structures.

Decisions

Normally, if statement is used where decisions are required.

If statement

The syntax of if statement is fairly simple i.e.

if (condition) { // if code block

docsity.com

// Do something else }

From stylistic and readability perspective, we properly indent the statements inside if- statements as shown above. We discussed pictorial representation of if-statement. By using flowchart of if statement that was a bit different than normally we see inside books, we introduced structured flowcharting. In structured flowcharting , we never go to the left of the straight line that joins Start and Stop buttons. There is a logical reason for it as while writing code, we can’t move to the left outside the left margin. Left margin is the boundary of the screen and indentation is made towards the right side. So we follow the construct that is equivalent to the program being written. The major advantage of this approach is achieved when we draw a flowchart of solution of a complex problem. The flowchart is the logical depiction of the solution to the problem. One can write code easily with the help of the flowchart. There will be one to one correspondence between the segments of the flowcharts and the code.

Loops

Going on from the decision structures we discussed about loops. In our program if we have to do something repeatedly then we can think of applying loop structure there. There are few variants of loops in C language. However, other languages might have lesser number of loop variants but a programming language always has loops constructs.

While Loop

The syntax of the while loop is as follows:

while ( condition ) { // while code block

// Do something

}

The condition is a logical expression like a == b that returns true or false. Braces are mandatory to for while loop when there are multiple lines of code inside the while code block. If there is only single line inside the while code block, the braces become optional. It is good practice to use braces. The statements inside the while code block are never executed, if the while condition results in false for very first time it is entered. In other words, statements inside the while code block executes 0 to n times.

docsity.com

The flowchart for the while loop is as follows:

Do-While Loop

Next loop variant is Do-while. It syntax is as under

do { // do-while code block

// Do something

} while ( condition )

The important difference of this loop from the rest ones is that it is executed once before the condition is evaluated. That means the statements of do-while code block execute at least once.

False condition

hile

Process

Exit

True

w

docsity.com

// Do something

}

Both the methods for writing of for loop are perfectly correct. You can use anyone of these. If you indent your code properly, the process will become easier.

The flowchart for for loop is as under:

False condition

for

Process

Exit

True

for Initialization Statements

for Incre/Decre Statements

docsity.com

switch, break and continue Statements For multi-way decisions , we can use nested if-statements or separate if-statements or switch statement. There are few limitations of switch statement but it is necessary to use break statements in every case inside the switch statement. If a case results in true when there is no break statement inside it, all the statements below this case statement are executed. break statement causes to jump out of the switch statement. We use break at the end of every case statement. By using break , the jumping out from switch statement is in a way bit different from the rules of structured programming. But break statement is so elegant and useful that you can use it inside switch statement and inside loops. If we use break inside a loop, it causes that loop to terminate. Similarly continue statement is very useful inside loops. continue statement is used, when at a certain stage, you don’t want to execute the remaining statements inside your loop and want to go to the start of the loop.

Functions In C/C++, functions are a way of modularizing the code. A bigger problem is broken down into smaller and more manageable parts. There is no rule of thumb for the length of each part but normally one function’s length is not more than one screen.

Function Calling

We covered Functions Calling by value and by reference. The default of C language is call by value. Call by value means that when we call a function and pass some

docsity.com

construct especially when used with loops. Normally it is very rare that you see an array in a program and loop is not being used to manipulate it. Like nested if-statements, we have nested loops, used with multi-dimensional arrays. A while loop can have an inner while loop. Similarly a for loop can have a for loop inside. It is also not necessary that a while loop should have only a while loop but it can be a for loop also or any other construct like if-statement.

Pointers

It is very important topic of C/C++. Pointers are different types of variables that contain memory address of a variable instead of a value.

The very first example we discussed for pointers was for implementing function calling by reference. Suppose we want to interchange (swap) two numbers by making a function call. If we pass two variables to the function, these will be passed as ordinary variables by value. Therefore, it will be ineffective as swapping of variables inside the function will only be on the copies and not on the original variables. So instead of passing variables we pass their addresses. In the called function, these addresses are taken into pointer variables and pointers start pointing the original variables. Therefore, the swapping operation done inside the function is actually carried out on the original variables. We also saw that Pointers and Arrays are inter-linked. The array name itself is a pointer to the first element. It is a constant pointer that cannot be incremented like normal pointer variables. In case of two-dimensional arrays, it points to the first row and first column. In three-dimensional array, you can imagine it pointing to the front corner of the cube.

File I/O

We discussed about Files and File I/O for sequential and random files. We used a mixture of C/C++ for file handling and how the sequential and random files are accessed. We saw several modes of opening files. The important functions were seek and tell functions. Seek functions (seekg and seekp ) used to move into the file and tell functions (tellg and tellp) provided us the location inside the file.

You are required to go with very clear head, try to understand concepts and assess how much you have learned so far to prepare for the mid-term examination.

docsity.com

docsity.com