You are on page 1of 4

CHAPTER

10
Decision Making in C

D ecision making structures require that the programmer specify 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.

Following is the general form of a typical decision making structure found in most of the
programming languages:

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
following types of decision making statements.

TUTORIALS POINT
Simply Easy Learning Page 35
Example
#include <stdio.h>

int main ()
{
/* local variable definition */
int a = 10;

/* do loop execution */
LOOP:do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;

}while( a < 20 );

return 0;
}

When the above code is compiled and executed, it produces the following result:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

The Infinite Loop


A loop becomes infinite loop if a condition never becomes false. The for loop is
traditionally used for this purpose. Since none of the three expressions that form the for
loop are required, you can make an endless loop by leaving the conditional expression
empty.

#include <stdio.h>

int main ()
{

TUTORIALS POINT
Simply Easy Learning Page 57
char '\0'

float 0

double 0

pointer NULL

It is a good programming practice to initialize variables properly otherwise, your program


may produce unexpected results because uninitialized variables will take some garbage
value already available at its memory location.

TUTORIALS POINT
Simply Easy Learning Page 68
CHAPTER

15
C Pointers

P ointers in C are easy and fun to learn. Some C programming tasks are performed

more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be
performed without using pointers. So it becomes necessary to learn pointers to become a
perfect C programmer. Let's start learning them in simple and easy steps.

As you know, every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator, which denotes an
address in memory.

Consider the following example, which will print the address of the variables defined:

#include <stdio.h>

int main ()
{
int var1;
char var2[10];

printf("Address of var1 variable: %x\n", &var1 );


printf("Address of var2 variable: %x\n", &var2 );

return 0;
}

When the above code is compiled and executed, it produces result something as follows:

Address of var1 variable: bff5a400


Address of var2 variable: bff5a3f6

So you understood what is memory address and how to access it, so base of the concept is
over. Now let us see what is a pointer.

TUTORIALS POINT
Simply Easy Learning Page 79

You might also like