Jan 23, 2012

Making Decision (if Statement)

Up until now, we have been writing a program that executes the statements one by one until the defined end. The functionality of this type of program is limited since it flows in single direction. Now we will be looking at one of the most powerful programming feature - the ability to change the flow of program execution. It is achieved by establishing the truth or falsity of an expression (condition).

Consider the following program.

#include<stdio.h>

int main()
{
   int V;
   printf("Enter the value of voltage, V: ");
   scanf("%d", &V);
   printf("The value of V is %d\n", V);
   return 0;
}

The program prints the value of V which is inputted by the users. What if we want the program prints V when the users enter a value that is more than 5 only. This means, the program has to be able to make a decision based on the user's input. C provides a decision making capability in the form of if statement. if is used when we want statements to be executed if an expression is TRUE. Otherwise the statements are skipped. Now, lets add an if statement to the above program as follows.

#include<stdio.h>

int main()
{
   int V;
   printf("Enter the value of voltage, V: ");
   scanf("%d", &V);
   if(V > 5)
      printf("The value of V is %d\n", V);
   return 0;
}

Output
Enter the value of voltage, V: 8
The value of V is 8


if(V > 5)
   printf("The value of V is %d\n", V);

V is compared with 5. If the value of V is greater than 5, the printf() statement will be executed which prints a string on the monitor. Otherwise the printf() will be skipped. In the example, 8 is entered by the user, thus "The value of V is 8" is displayed.

Relational & Logical Operators


Relational operators are used to compare two values. There are six (6) operators as listed in the following table.

Operator Meaning
==
Equal to
!=
Not equal to
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to

Relational operators have lower precedence than arithmetic operators. For example, consider the following expression.

x + y > z

Since arithmetic operators precede relational operators. The expression is evaluated as

(x + y) > z

Assumes that x, y and z is 2, 3 and 4. Then, the expression is said TRUE. An expression is TRUE if it satisfies the condition and FALSE otherwise. In C, TRUE has the value of 1 while FALSE has the value of 0.

There are three (3) logical operators which are AND (&&), OR (||) and NOT (!). More than one expression can be combined using AND (&&) and OR (||) operators. For example, consider the following expression.

a > b && x == y

If both a > b and x == y are TRUE, the expression returns,

1 && 1

which will returns 1 (TRUE).

! is used to invert the logical value of an expression. For example,

!(x > 2)

Assume that x is 3 then the expression is said to be FALSE.

Jan 15, 2012

Program Input

Consider this program.

#include<stdio.h>

int main()
{
   int V;
   V = 15;
   printf("The value of V is %d\n", V);
   return 0;
}

The value of V is assigned with 15. Thus the printed text will be "The value of V is 15". Now, what if we want to print different value of V. We have to go back to the source code and change 15 to the desired value. And what if we want the value to be set during the program execution. For example, the program might ask a value which the user wants to print. Such operation can be effected by using a function called scanf. The scanf function is the opposite of printf function. While printf prints values at the terminal, scanf reads values entered by users into the program. The following program prints a message which asking the user to enter a value of V.

#include<stdio.h>

int main()
{
   int V;
   printf("Enter the value of voltage, V: ");
   scanf("%d", &V);
   printf("The value of V is %d\n", V);
   return 0;
}

Output
Enter the value of voltage, V: 5
The value of V is 5


The first printf prints "Enter the value of voltage, V: ". This message prompts the user to type in a value for voltage. Then scanf function is invoked. The scanf contains two arguments. The first argument is the format control string specifies the data type of value to be read from the terminal. In this example %d is specified which indicates integer value is expected to be read. The second argument specifies which variable to be used to store the inputted value. The ampersand (&) is necessary in this case. The function of & will be explained later. For now don't forget to put leading & in front of the variable when using scanf function.

As explained in the preceding discussion, integer value is to be read from the terminal and stored in V. 5 is type in as given in the example. Thus, 5 is printed by the subsequent printf function and then the program execution is complete.