Technical Training
C TutorialsTable of Contents
C Programming - Decision Making - Branching
C Programming - Decision Making - Branching - Page 2
C Programming - Decision Making - Branching - Page 3C Programming - Decision Making - Branching Page - 2
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:
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:
- If(a==1)
- Printf(“1”);
- Else
- Printf(“none”);
- Instead of:
- If(a==1)
- { printf(“1”); }
- else
- { Printf(“none”);}
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:
- int number;
- printf("Please Enter a number greater than or equal to 5?n");
- scanf("%d",&number);
- if(number>=5)
- {
- printf("nit is valid");
- }
- else
- {
- printf("nit is invalid");
- }


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 – |
|
!= |
Not equal to |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater than or equal to |
|
<= |
Less than or equal to |
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:
- int A;
- printf("Please Enter a number greater than 4 and less than or equal to 8?n");
- scanf("%d",&A);
- if(A>4 && A<=8)
- {
- printf("nyes");
- }

Figure 3 code 3
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
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)”.Hints:
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







