pc module1

27
! " What is a Flow Chart? A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. What is an Algorithm? What are the steps of algorithm? Step-by-step method of solving a problem or making decisions, as in making a diagnosis. Steps of the algorithm ) Finiteness: - an algorithm terminates after a finite numbers of steps. 2) Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion. 3) Input: - an algorithm accepts zero or more inputs 4) Output:- it produces at least one output. 5) Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time Difference between algorithm and flowchart? An algorithm is a precise rule (or set of rules) specifying how to solve some problem while A flowchart is a diagram of the sequence of operations (pictorial representation of algorithm) in a computer program

Upload: santosh-rath

Post on 11-May-2015

72 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

What is a Flow Chart?

A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction.

What is an Algorithm? What are the steps of algorithm?

Step-by-step method of solving a problem or making decisions, as in making a diagnosis.

Steps of the algorithm ) Finiteness: - an algorithm terminates after a finite numbers of steps. 2) Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion. 3) Input: - an algorithm accepts zero or more inputs 4) Output:- it produces at least one output. 5) Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time

Difference between algorithm and flowchart?

An algorithm is a precise rule (or set of rules) specifying how to solve some problem while A flowchart is a diagram of the sequence of operations (pictorial representation of algorithm) in a computer program

Page 2: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Keywords

The following keywords are reserved and may not be used as an identifier for any other purpose.

auto double int long break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while

Formatted String:

It specified to read which type of data.

%c CHARACTER

%d INTEGER

%f FLOAT

%lf DOUBLE

%u UNSIGNED

%s STRING

%Lf LONG DOUBLE

%ld LONG INT

Page 3: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Preprocessor directives in c

In c all the preprocessor directives start with # character except define () operator. All the directives have different task which executes just before the actual execution of c program which makes the program more portable. These directives are used only to instruct compilers.

Why preprocessor in c programming language? Answer: 1. It improves the readability of program. 2. It makes easy to modify 3. Portability

#include directive in c

Syntax: #include <filename> or #include “filename” Task of include directive is to include or import any type file. There are two form of include directive. Form 1: #include<filename>: This statement will search the file in include directory (Default location of include directory is c:\tc\include). If that file is present then #include statement is replaced by content of file. If that file is not present then it will cause of compilation error. //test.c #include<stdio.h> #include<conio.h> void main(){ clrscr(); printf("HOW large is this file?");

Page 4: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

getch(); } C Programming - Data Types

C language data types can be broadly classified as 1. Primary data type 2. Derived data type 3. User-defined data type Primary data type All C Compilers accept the following fundamental data types

Page 5: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Secondary Data Types

• Array in C programming An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int,...

• Pointers in C Programming In this tutorial I am going to discuss what pointer is and how to use them in our C program. Many C programming learner thinks that pointer is one of the difficult topic in C language but its not...

• Structure in C Programming We used variable in our C program to store value but one variable can store only single piece information (an integer can

Page 6: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

hold only one integer value) and to store similar type of values we had to declare...

User defined type declaration C language supports a feature where user can define an identifier that characterizes an existing data type. This user defined data type identifier can later be used to declare variables. In short its purpose is to redefine the name of an existing data type. Syntax: typedef <type> <identifier>; like typedef int number; Now we can use number in lieu of int to declare integer variable. For example: “int x1” or “number x1” both statements declaring an integer variable. We have just changed the default keyword “int” to declare integer variable to “number A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common data types are integer, character, and floating-point. Main ( ): All C programs consist of one or more functions. As a general rule, the only function that must be present is called main ( ), which is the first function called when program execution begins

Page 7: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

The general form of a C program:

Global declarations int main(parameter list) { statement sequence } return-type f1(parameter list) { statement sequence } return-type f2(parameter list) { statement sequence }... return-type fN(parameter list) { statement sequence

Review of Terms . • Source code: The text of a program that a user can read, commonly thought of as the program. The source code is input into the C compiler. • Object code: Translation of the source code of a program into machine code, which the computer can read and execute directly. Object code is the input to the linker. • Linker: A program that links separately compiled modules into one program. It also combines the functions in the Standard C library with the code that you wrote. The output of the linker is an executable program.

Page 8: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

• Library: The file containing the standard functions that your program can use. These functions include all I/O operations as well as other useful routines. • Compile time: The time during which your program is being compiled. • Run time: The time during which your program is executing

NOTE: The type void either explicitly declares a function as returning no value or creates generic pointers

All Data Types Defined by the C Standard Type Typical Size in char 8 –127 to 127 unsigned char 8 0 to 255 signed char 8 –127 to 127 int 16 or 32 –32,767 to 32,767 unsigned int 16 or 32 0 to 65,535 signed int 16 or 32 Same as int short int 16 –32,767 to 32,767 unsigned short int 16 0 to 65,535 signed short int 16 Same as short int long int 32 –2,147,483,647 to 2,147,483,647 long long int 64 –(263 –1) to 263 –1 (Added by C99) signed long int 32 Same as long int unsigned long int 32 0 to 4,294,967,295 unsigned long long int 64 264 –1 (Added by C99) float 32 1E–37 to 1E+37 with six digits of

precision double 64 1E–37 to 1E+37 with ten digits of

precision long double 80 1E–37 to 1E+37 with ten digits of

precision

Page 9: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Variables: As you probably know, a variable is a named location in memory that is used to hold a value that can be modified by the program. All variables must be declared before they can be used. The general form of a declaration is: type variable_list; Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier names separated by commas. Here are some declarations: Eg: int i, j, l; Eg: short int si; Eg: unsigned int ui; Eg: double balance, profit, loss;

Remember:

Where Variables Are Declared Variables can be declared in three places: inside functions, in the definition of function parameters, and outside of all functions. These positions correspond to local variables, formal parameters, and global variables, respectively. Local Variables:

Variables that are declared inside a function are called local variables Local variables exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. Furthermore, a variable declared within one code block has no bearing on or

Page 10: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

relationship to another variable with the same name declared within a different code block.

Global Variables:

Unlike local variables, global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program's execution. You create global Variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in.

Variable Initializations:

You can give variables a value as you declare them by placing an equal sign and a constant after the variable name. The general form of initialization is type variable_name = constant; Some examples are char ch = 'a'; int first = 0; double balance = 123.23;

Constants:

Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.

Page 11: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Escape Sequences:

Code Meaning \b Backspace \f Form feed \n new line \r Carriage return \t Horizontal tab \" Double quote \' Single quote \ \ Backslash \v Vertical tab \a Alert \? Question mark \N octal constant (where N is an octal constant) \xN Hexadecimal constant (where N is a hexadecimal constant)

Operators

C is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages. There are four main classes of operators: arithmetic, relational, logical, and bitwise. In addition, there are some special operators, such as the assignment operator, for particular tasks

1) The Assignment Operator

You can use the assignment operator within any valid expression the general form of the assignment operator is: variable name = expression; where an expression may be as simple as a single constant or as complex as you require.

a) Multiple Assignments You can assign many variables the same value by using multiple assignments in a single statement. For example, this program fragment assigns x, y, and z the value 0:

Page 12: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

x = y = z = 0; in professional programs, variables are frequently assigned common values using this method.

b) Compound Assignments There is a variation on the assignment statement, called compound assignment. that simplifies the coding of a certain type of assignment operations. For example, x = x+10; can be written as x += 10; The operator += tells the compiler to assign to x the value of x plus 10. Compound assignment operators exist for all the binary operators (those that require two operands).In general, statements like

a) var = var operator expression Can be rewritten as

a) var operator = expression For another example, x = x-100; is the same as x -= 100; Because compound assignment is more compact than the corresponding = equivalent, compound assignment is also sometimes referred to as shorthand assignment. Compound assignment is widely Used in professionally written C programs; you should be familiar with it.

2) Arithmetic Operators

Table: lists C's arithmetic operators. The operators +, –, *, and / work as they do in most other computer languages. You can apply them to almost any built-in data type. When you apply / to an integer or character, any remainder will be truncated. For example, 5/2 will equal 2 in integer division. The modulus operator % also works in C as it does in other languages, yielding the remainder of an integer division. However, you cannot use it on floating-point types. The following code fragment illustrates %: Operator Action – Subtraction, also unary minus + Addition * Multiplication / Division

Page 13: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

% Modulus –– Decrement ++ Increment

3) Increment and Decrement Operators

C includes two useful operators that simplify two common operations. These are the increment and decrement operators, ++ and ––. The operator ++ adds 1 to its operand, and ––subtracts 1. In other words: x = x+1; is the same as ++x; And x = X–1; is the same as x– –; Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand. For example, x = x+1; can be written ++x; or x++; There is, however, a difference between the prefix and postfix forms when you use these operators in a larger expression. When an increment or decrement operator precedes its operand, the increment or decrement operation is performed before obtaining the value of the operand for use in the expression. If the operator follows its operand, the value of the operand is obtained before incrementing or decrementing it. For instance. x = 10; y = ++x; Sets y to 11. However, if you write the code as x = 10; y = x++; y is set to 10. Either way, x is set to 11; the difference is in when it happens. Most C compilers produce very fast, efficient object code for increment and decrement operations code that is better than that generated by using the equivalent assignment statement. For this reason, you should use the increment and decrement operators when you can. Here is the precedence of the arithmetic operators: Here is the precedence of the arithmetic operators: Highest ++ –– – (unary minus)

Page 14: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

* / % Lowest + – Operators on the same level of precedence are evaluated by the compiler from left to right. Of course, you can use parentheses to alter the order of evaluation. C treats parentheses in the same way as virtually all other computer languages. Parentheses force an operation, or set of operations, to have a higher level of precedence.

4) Logical Operators

In the term relational operator, relational refers to the relationships that values can have with one another. In the term logical operator, logical refers to the ways these relationships can be connected. Because the relational and logical operators often work together, they are discussed together here. The idea of true and false underlies the concepts of relational and logical operators. In C, true is any value other than zero. False is zero. Expressions that use relational or logical operators return 0 for false and 1 for true. Table shows the relational and logical operators. The truth table for the logical operators is shown here using 1's and 0's. p q p && q p || q !p 0 0 0 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0

5) Relational Operators

Operator Action > Greater than >= Greater than or equal < Less than <= Less than or equal = = Equal

Page 15: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

!= Not equal Logical Operators

Operator Action && AND | | OR ! NOT Table: Relational and Logical Operators The following table shows the relative precedence of the relational and logical operators: Highest! > >= < <= = = != && Lowest | | As with arithmetic expressions, you can use parentheses to alter the natural order of evaluation in a relational and/or logical expression. For example,! 0&&0 | | 0 Is false. However, when you add parentheses to the same expression, as shown here, the result is true. Remember, all relational and logical expressions produce a result of either 1 or 0. Therefore, the following program fragment is not only correct, but will print the number 1. The following table shows the relative precedence of the relational and logical operators: As with arithmetic expressions, you can use parentheses to alter the natural order of evaluation in a relational and/or logical expression. For example, is false. However, when you add parentheses to the same expression, as shown here, the result is true. Remember, all relational and logical expressions produce a result of either 1 or 0. Therefore, the following program fragment is not only correct, but will print the number 1.

6) Bitwise operators :

Page 16: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Unlike many other languages, C supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming tasks, it needed to be able to support many operations that can be done in assembler, including operations on bits. Bitwise operation refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the standard char and int data types and variants.

Highest! > >= < <= = = != && The bitwise AND, OR, and NOT (one's complement) are governed by the same truth table as their logical equivalents, except that they work bit by bit. The exclusive OR has the truth table shown here: p q p ^q 0 0 0 1 0 1 1 1 0 0 1 1

- As the table indicates, the outcome of an XOR is true only if exactly one of the operands is true; otherwise, it is false.

- Bitwise operations most often find application in device drivers— such as modem programs, disk file routines, and printer routines— because the bitwise operations can be used to mask off certain bits, such as parity. (The parity bit confirms that the rest of the bits in the byte are unchanged. It is often the high-order bit in each byte.)

- Operator Action & AND | OR ^ Exclusive OR (XOR) ~ One's complement (NOT)

Page 17: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

>> Shift right << Shift left

Table: Bitwise Operators Example: #include<stdio.h> #include<conio.h> Void main () { Char ch; Int x=127, y; Y=ch&x; Printf(“%d”,y);} Output: Parity is often indicated by the eighth bit, which is set to 0 by ANDing it with a byte that has bits 1through 7 set to 1 and bit 8 set to 0. The expression ch & 127 means to AND together the bits in ch with the bits that make up the number 127. The net result is that the eighth bit of ch is set to 0. In the following example, assume that ch had received the character A and had the parity bit set:

The bitwise OR, as the reverse of AND, can be used to set a bit. Any bit that is set to 1 in either operand causes the corresponding bit in the outcome to be set to 1. For example, the following is 128 | 3:

An exclusive OR, usually abbreviated XOR will set a bit on, if and only if the bits being compared are different. For example, 127 ^120 is

Page 18: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Remember, relational and logical operators always produce a result that is either true or false, whereas the similar bitwise operations may produce any arbitrary value in accordance with the specific operation. In other words, bitwise operations may produce values other than 0 or 1, while logical operators will always evaluate to 0 or 1.The bit-shift operators, >> and <<, move all bits in a variable to the right or left as specified. The general form of the shift-right statement is

- variable >> number of bit positions The general form of the shift-left statement is

- variable << number of bit positions - As bits are shifted off one end, zeroes are brought in the other end. (In the

case of a signed, negative integer, a right shift will cause a 1 to be brought in so that the sign bit is preserved.) Remember, a shift is not a rotate. That is, the bits shifted off one end do not come back around to the other. The bits shifted off are lost.

- Notice that a sequence of two complements in a row always produces the original number. Hence, the first complement represents the coded version of that byte. The second complement decodes the byte to its original value.

- unsigned char x; x as each statement executes value of x - x = 7; 00000111 7 - x = x<<l; 00001110 14 - x = x<<3; 01110000 112 - x = x<<2; 11000000 192 - x = x>>l; 01100000 96 - x = x>>2; 00011000 24 - Each left shift multiplies by 2. Notice that information has been lost after

x<<2 because a bit was shifted off the end. - Each right shift divides by 2. Notice that subsequent divisions do not bring

back any lost bits.

7) Ternary operator:

Page 19: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

C contains a powerful and convenient operator that replaces certain statements of the if-then-else form. The ternary operator? Takes the general form Syntax: Exp1? Exp2: Exp3; Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the Colon.

- The? Operator works like this: Exp1 is evaluated. If it is true, Exp2 is evaluated and becomes the value of the expression. If Exp1 is false, Exp3 is evaluated, and its value becomes the value of the expression. For example, in x = 10; y = x>9? 100: 200;

Y is assigned the value 100. If x had been less than 9, y would have received the value 200. The same code written using the if-else statement is x = 10; If (x>9) y = 100;

Else y = 200; The? Operator will be discussed more fully in Chapter 3 in relationship to the other conditional statements.

8) The Comma Operator

The comma operator strings together several expressions. The left side of the comma operator is always evaluated as void. This means that the expression on the right side becomes the value of the total comma-separated expression. For example, x = (y=3, y+1); First assigns y the value 3 and then assigns x the value 4. The parentheses are necessary because the comma operator has a lower precedence than the assignment operator. Essentially, the comma causes a sequence of operations. When you use it on the right side of an assignment statement, the value assigned is the value of the last

Page 20: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

expression of the comma-separated list. The comma operator has somewhat the same meaning as the word "and" in English, as used in the phrase "do this and this and this."

Order of Evaluation

Precedence of C Operators

C does not specify the order in which the sub expressions of an expression are evaluated. This leaves the compiler free to rearrange an expression to produce more Highest ( ) [ ] –>. ! ~ ++ –––(type) * & size of * / % + – << >> < <= > >= == != & ^ | && | | ?: = += –= *= /= etc.

Control Statement There are four types of control statements

1) Selection Control Statement (Switch-Case) 2) Decision Making Control Statement (if, if-else, if-else-if ladder) 3) Iteration Control Statement (While, Do-While, For) 4) Jump Control Statement (Go to, Continue, Break)

Page 21: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

I. Selection Control Statement: C supports two selection statements: if and switch. In addition, the ? Operator is an alternative to if in certain circumstances.

If: The general form of the if statement is if (expression) statement; else statement; Switch- Case Statement: C has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. The general form of the switch statement is switch (expression) { case constant1: statement sequence break; case constant2: statement sequence break; case constant3: statement sequence break; . . . default statement sequence }

- Note: The expression must evaluate to an integer type. Thus, you can use character or integer values, but floating-point expressions, for example, are

Page 22: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

not allowed. The value of expression is tested against the values, one after another, of the constants specified in the case statements. When a match is found, the statement sequence associated with that case is executed until the break statement or the end of the switch statement is reached. The default statement is executed if no matches are found. The default is optional, and if it is not present, no action takes place if all matches fail.

- The break statement is one of C's jump statements. You can use it in loops as well as in the switch statement (see the section ''Iteration Statements"). When break is encountered in a switch, program execution "jumps" to the line of code following the switch statement.

There are three important things to know about the switch statement:

I. The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of relational or logical expression.

II. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants that are in common.

III. If character constants are used in the switch statement, they are automatically converted to integers (as is specified by C's type conversion rules).

II. Decision Making Statement:

A) Nested ifs A nested if is an, if that is the target of another if or else. Nested ifs are very common in programming. In a nested if, an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else SYNTAX:

Page 23: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

if(i) { if(j) dosomething1(); if(k) dosomething2(); /* this if */ else dosomething3(); /* is associated with this else */ } else dosomething4(); /* associated with if(i) */

B) The if-else-if Ladder A common programming construct is the if-else-if ladder, sometimes called the if-else-if staircase because of its appearance. Its general form is if (expression) statement; else if (expression) statement; else if (expression) statement; . . . else statement;

III. Iteration Statements: In C, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be repeatedly executed until a certain condition is reached. This condition may be predetermined (as in the for loop) or open ended (as in the while and do-while loops).

A) The for Loop The general design of the for loop is reflected in some form or another in all procedural programming languages. However, in C, it provides unexpected flexibility and power. The general form of the for statement is SYNTAX: For (initialization; condition; increment) statement;

Page 24: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Q. In the following program, a for loop is used to print the numbers 1 through 100 on the screen: Example: #include <stdio.h> int main(void) { int x; for(x=1; x <= 100; x++) printf("%d ", x); return 0; } Explanation: In the loop, x is initially set to 1 and then compared with 100. Since x is less than 100, printf ( ) is called and the loop iterates. This causes x to be increased by 1 and again tested to see if it is still less than or equal to 100. If it is, printf ( ) is called. This process repeats until x is greater than 100, at which point the loop terminates. In this example, x is the loop control variable, which is changed and checked each time the loop repeats.

B) The while Loop The second loop available in C is the while loop. Its general form is: While (condition) { Statement; } Where statement is an empty statement, a single statement, or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop.

C) The do-while Loop

Page 25: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop. This means that a do-while loop always executes at least once. The general form of the do-while loop is: Do { Statement; } While (condition); Although the curly braces are not necessary when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while. The do-while loop iterates until condition becomes false. Q. The following do-while loop will read numbers from the keyboard until it finds a number less than or equal to 100: Do { scanf(''%d", &num); } while(num > 100); IV: Jump Statements C has four statements that perform an unconditional branch: return, go to, break, and continue. Of these, you can use return and go to anywhere inside a function. You can use the break and continue statements in conjunction with any of the loop statements. As discussed earlier in this chapter, you can also use break with switch. The return Statement The return statement is used to return from a function. It is categorized as a jump statement because it causes execution to return (jump back) to the point at which the call to the function was made. A return may or may not have a value associated with it. A return with a value can be used only in a function with a non-void return

Page 26: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

type. In this case, the value associated with return becomes the return value of the function. A return without a value is used to return from a void function. The general form of the return statement is: Return expression; The go to statement: It requires a label for operation. (A label is a valid identifier followed by a colon.)Furthermore, the label must be in the same function as the go to that uses it— you cannot jump between functions. The general form of the go to statement is Go to label; ... Label: Where label is any valid label either before or after go to. For example, you could create a loop from 1 to 100 using the go to and a label, as shown here: x = 1; loop1: x++; if(x <= 100) go to loop1; The break Statement: The break statement has two uses. You can use it to terminate a case in the switch statement (covered in the section on switch earlier in this chapter). You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. For example, #include <stdio.h> int main (void) { int t; for(t=0; t < 100; t++) {

Page 27: Pc module1

�������������� ������� �������� � ���

��������� ��� ����������������������������������������������������������������������������������

������������������������� ����������������������

�� ����� ������������������������������������������������������������������������������������������

�� �� ���������!������������������������������������������������������������������

� ���"����

printf(''%d ", t); if(t == 10) break; } return 0;} The continue Statement The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place.

Block Statements Block statements are simply groups of related statements that are treated as a unit. The statements that make up a block are logically bound together. Block statements are also called compound statements. A block is begun with a {and terminated by its matching}. Programmers use block statements most commonly to create a multi statement target for some other statement, such as if. However, you may place a block statement anywhere you would put any other statement. For Example, this is perfectly valid (although unusual) C code: