Nothing Special   »   [go: up one dir, main page]

Introduction To C Programming Part 3 B

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

I.

DECISION MAKING
Decision-making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.

C programming language assumes any non-zero and non-null values as true,


and if it is either zero or null, then it is assumed as false value. C programming
language provides the following types of decision-making statements.

1
Example

1. If, else example:

2
2. If, else if and else example:

3. Nested if Statements:
This enables us to have an if statement inside another if statement.

3
4. switch Statement example
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each
switch case.

4
II. LOOPS
A loop statement allows us to execute a statement or group of statements
multiple times. Given below is the general form of a loop statement in most
of the programming languages:

C programming language provides the following types of loops to handle


looping requirements.

5
1. While Loop:
A while loop in C programming repeatedly executes a target statement as long as a given
condition is true.

6
2. For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.

3. do…while Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute
at least one time.

7
4. Nested Loops
C programming allows US to use one loop inside another loop.

8
III. FUNCTIONS
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and all the most trivial programs can
define additional functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.

9
A function definition in C programming consists of a function header and a function
body. Here are all the parts of a function:
 Return Type: A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
 Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 Parameters: A parameter is like a placeholder. When a function is invoked, you pass
a value to the parameter. This value is referred to as actual parameter or argument.
The parameter list refers to the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function may contain no parameters.
 Function Body: The function body contains a collection of statements that define
what the function does.

Calling a Function
While creating a C function, you give a definition of what the function has to do.
To use a function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called
function.
A called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main
program. To call a function, you simply need to pass the required parameters along with the
function name, and if the function returns a value, then you can store the
returned value. For example:

10
11
IV. SCOPE RULE
A scope in any programming is a region in the program where a defined variable can
have its existence and beyond that, the variable cannot be accessed. There are two
places where variables can be declared in C programming language:
 Inside a function or a block which is called local variables,
 Outside of all functions which is called global variables.
1. Local Variables
Variables that are declared inside a function or block are called local
variables. They can be used only by statements that are inside that function
or block of code. Local variables are not known to functions outside their
own. The following example shows how local variables are used. Here all the
variables a, b, and c are local to the main() function.

2. Global Variables
Global variables are defined outside a function, usually on top of the
program.
A global variable can be accessed by any function. That is, a global variable
is available for use throughout your entire program after its declaration. The
following program shows how global variables are used in a program.

12
13
V. ARRAYS
An Array is a type of data structure that can store a fixed-size sequential collection of
elements of the same type.
An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1], and
..., numbers[99] to represent individual variables.
A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.

Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows:

This is called a single-dimensional array. The arraySize must be an integer constant


greater than zero and type can be any valid C data type. For example, to declare a
10-element array called balance of type double, use this statement:

Here, balance is a variable array which is sufficient to hold up to 10 double numbers


(floating point numbers with 12 decimal places).

14
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as
follows:

The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:

You will create exactly the same array as you did in the previous example.

Following is an example to assign a single element of the array:

The above statement assigns the 5th element in the array with a value of 50.0.
All arrays have 0 as the index of their first element which is also called the base
index and the last index of an array will be total size of the array minus 1.
Shown below is the pictorial representation of the array we discussed above:

15
Examples
1.

2.

3. Asking the user to enter the array elements

16
4. Asking the user for the size of array

17
5. Function to fill elements in an array and call the function in the main function and display the
elements on the screen.

Multidimensional Arrays
C programming language allows multidimensional arrays. Here is the general form of a
multidimensional array declaration:

For example, the following declaration creates a three-dimensional integer array:

Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A two-dimensional
array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array
of size [x][y], you would write something as follows:

18
Where type can be any valid C data type and arrayName will be a valid C identifier.
A two-dimensional array can be considered as a table which will have x number of rows and y
number of columns. A two-dimensional array a, which contains three rows and four columns
can be shown as follows:

Thus, every element in the array a is identified by an element name of the


form a[ i ][ j ], where ‘a’ is the name of the array, and ‘i' and ‘j’ are the
subscripts that uniquely identify each element in ‘a’.

Examples
1.

19
2. Asking the user to enter the array elements

20
3. Asking the user for the size of array

4. Function to fill elements in a two dimensional array and call the function in the main
function and display the elements on the screen.

21

You might also like