Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Decision Making - Branching Page - 2

Page 2 of 3
Author: Exforsys Inc.     Published on: 4th Apr 2006    |   Last Updated on: 28th Aug 2011

C Programming - Decision Making - Branching

The If - Else Construct

“if else construct” is the upgraded version of “if statement”. Unlike “if statement” where you could only specify code for when condition is true; for “if else statement” you can also specify code for when the condition is not True (false). So the definition for the “if else statement” is:

Ads

Definition:

if(condition){ code when true } else { code when not true }

Tips :

{ } can be removed if only one line of code is to be executed. For example:

Sample Code
  1. If(a==1)
  2.         Printf(1);
  3. Else
  4.         Printf(“none”);
  5.  
  6. Instead of:
  7.  
  8. If(a==1)
  9.         { printf(1); }
  10. else
  11.         { Printf(“none”);}
Copyright exforsys.com


Example:

imagine the case where we need an input “greater than or equal to 5” and we want to warn the user if they use invalid input (less than 5). We can use the code below:

Code:

Sample Code
  1. int number;
  2.  
  3.         printf("Please Enter a number greater than or equal to 5?n");
  4.         scanf("%d",&number);
  5.         if(number>=5)
  6.         {
  7.                 printf("nit is valid");
  8.         }
  9.         else
  10.         {
  11.             printf("nit is invalid");
  12.         }
Copyright exforsys.com


Figure 2 code 2 screenshots

Compound Relational Tests

In order to check the conditions within the programs, C has number of relational operators. The table 1 summarises these operators using variables a=10 and b=5.

==

Is Equal to –
a==a returns 1, a==b returns 0

!=

Not equal to
a!=b returns 1 , a!=a returns 0

Greater than
a>breturns 1 , b>a returns 0
a>10 returns 0, b>5 returns 0

Less than
a<b returns 0 , b<a returns 1
a<10 returns 0, b<5 returns 0

>=

Greater than or equal to
a>=10 returns 1 , b>=5 return 1

<=

Less than or equal to
a<=10 returns 1 , b<=5 return 1

Table 1 relational operators

Tips:

Relational operators can only compare two variables, for example: ‘a==a’ is correct, ‘a==a==b’ is incorrect syntax.

‘Logical operators’ are used in conditional statements such as ‘if statement’ to create a single testing condition. However some conditional statements require more than one testing conditions. For example: if we are asked to write program which output the word “yes” if the user’s input value is between 4 and 8 including number 8, we obviously can’t program this condition using only one testing condition; therefore we require logical operators to link those conditions. There are three logical operators: ‘!’,‘&&’ and ‘||’.

‘&&’ denotes to an operation of logical AND gate. ‘||’ denotes to an operation of logical OR gate. Table 2 and 3 describe the behaviour of && and || respectively.

Condition A

Condition B

Result

0

0

0

0

1

0

1

0

0

1

1

1

Table 2 && operation

Condition A

Condition B

Result

0

0

0

0

1

1

1

0

1

1

1

1

Table 3 || operation

Example #1: using && operator

Here we will refer back to the example we had before. We want to write a program which outputs “yes” if the user’s input is between 4 and 8 (including number 8). In order to write this we have to program the following condition in our ‘if statement’.

Input is greater than 4 AND less than or equal to 8.

Condition A                Condition B

As you can see above, we have two conditions for our ‘if statement’; so we need to separate these two conditions in order to program the conditions in C.

Condition A is ‘greater than 4’; we program this in C assuming that the variable ‘A’ is the user input, now we have:

Condition A: A>4

We do the same for the second condition and we get:

Condition B: A<=8

Our two conditions are ready. We need to link them together using logical operators. For this example we need to use && operator. Therefore our final condition becomes (A>4 && A<=8).

Code:

Sample Code
  1. int A;
  2.  
  3. printf("Please Enter a number greater than  4 and less than or equal to 8?n");
  4. scanf("%d",&A);
  5. if(A>4 && A<=8)
  6. {
  7.         printf("nyes");
  8. }  
Copyright exforsys.com


Figure 3 code 3

Ads

Description:

We have evaluated the written description of the problem and we produced the C code from that. As you can see above, the testing condition “A>4 && A<=8” is the same as mathematical expression “4

Hints:

Be extra careful with the way you format your conditions together. Using brackets to separate each condition from another is the best practice to avoid confusions for complex testing conditions. For example: we can write “A>4 && A<=8” as “(A>4) && (A<=8)”.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments