Branching in C
You will use the following statements in C to facilitate branching.
1. The if statement
2. The if-else statement
Let us learn about each.
The if statement
The if statement is similar to the if statement in English. Consider the following statements.
- if it is raining, I will not go to school.
- if tomorrow is a holiday, I will watch a movie.
In the above two statements, a decision is taken (I will not go to school, I will watch a movie) based on some conditions (if it is raining, if tomorrow is a holiday). In these two statements the condition is introduced by the word if. Similarly in C, the keyword if introduces a condition. Based on whether the condition is true or false, the appropriate decision is taken.
Consider the following program, which decides which number is bigger among the two.
#include main() { int num1=10, num2=20; if(num1>num2) { printf(“num1 is big”); } if(num2>num1) { printf(“num2 is big”); } if(num1==num2) { printf(“The two are equal”); } }
The output of this program is num2 is big. Let us find out how we got this output:
- The first if statement checks whether the value in the variable num1 is greater than the value in num2. If it is, then the printf() statement within the braces prints “num1 is big” on the screen.
- The second if statement checks whether the value in the variable num2 is greater than that of num1. If it is, the printf() statement within the braces prints “num2 is big” on the screen. In our program, since the value in num2 is greater than that in num1, we got the output, as num2 is big.
- The third if statement checks whether the value in the variable num1 equals that in num2. If it does, the printf() statement within the braces prints “The two are equal”.
In general, the syntax of the if statement is as follows:
if(condition)
{
statement1;
statement2;
………………..;
…………………;
}
The compiler executes the statements within the braces only when the condition is evaluated to be true. If the condition is false, the control simply transfers to the statement next to the if statement.
Relational operators
Observe the three new symbols (>, <, and ==) in the above program. These are called relational operators. These are used to construct a condition. In the above program there are three conditions, num1>num2, num2>num1, and num1==num2. The first condition is evaluated to be true only when the value in the variable num1 is greater than that of num2, the second is reckoned as true only when the value in the variable num2 is greater than that of num1, and the third is evaluated to be true only when the values in both num1 and num2 are equal.
The following table gives a list of all the relational operators in C.
Operator | Description |
== | Equals to |
!= | Not equals to |
> | Greater than |
< | Less than |
>= | Greater than or equals to |
<= | Less than or equals to |
Note: = and == are different in C. If you write = in place of ==, it is a logical error.
Writing programs using if-else
Let us suppose you purchased a book on C programming from a bookstall. The shopkeeper bought that book from a distributor at Rs.X and he sold it to you at Rs.Y. The following C program helps you to decide whether the shopkeeper made a gain or suffered a loss in the transaction.
#include
main()
{
int cp,sp;
printf(“Enter cost price:”);
scanf(“%d”,&cp);
printf(“Enter selling price:”);
scanf(“%d”,&sp);
if(sp>cp)
printf(“Gain”);
else
printf(“Loss”);
}
Observe how the if-else statement is used in this program. This statement is used when there are only two alternatives and you have to choose one of them.
Try it out
1. Write a program to check whether a given number is even or odd? (Hint: An even number is a number which when divided by 2 gives the remainder 0. So use % operator to find the remainder and compare it with 0.)
2. Write a program to accept a number (other than 0) from a keyboard and display whether it is positive or negative.
No comments:
Post a Comment