C Programming - Decision Making - Branching
C Programming - Decision Making - Branching
C Programming - Decision Making - Branching
Branching
C Programming - Decision Making - Branching
In this tutorial you will learn about C Programming - Decision Making, Branching, if
Statement, The If else construct, Compound Relational tests, Nested if Statement, The
ELSE If Ladder, The Switch Statement and The GOTO statement.
Branching
The C language programs presented until now follows a sequential form of execution of
statements. Many times it is required to alter the flow of the sequence of instructions. C
language provides statements that can alter the flow of a sequence of instructions. These
statements are called control statements. These statements help to jump from one part of
the program to another. The control transfer may be conditional or unconditional.
if Statement:
The simplest form of the control statement is the If statement. It is very frequently used in
decision making and allowing the flow of program execution.
if (condition)
statement;
The statement is any valid C’ language statement and the condition is any valid C’
language expression, frequently logical operators are used in the condition statement. The
condition part should not end with a semicolon, since the condition and statement should
be put together as a single statement. The command says if the condition is true then
perform the following statement or If the condition is fake the computer skips the
statement and moves on to the next instruction in the program.
Example program
Sample Code
10.}
Copyright exforsys.com
The above program checks the value of the input number to see if it is less than zero. If it
is then the following program statement which negates the value of the number is
executed. If the value of the number is not less than zero, we do not want to negate it then
this statement is automatically skipped. The absolute number is then displayed by the
program, and program execution ends.
The if else is actually just on extension of the general format of if statement. If the result
of the condition is true, then program statement 1 is executed, otherwise program
statement 2 will be executed. If any case either program statement 1 is executed or
program statement 2 is executed but not both when writing programs this else statement
is so frequently required that almost all programming languages provide a special
construct to handle this situation.
Sample Code
11.}
Copyright exforsys.com
In the above program the If statement checks whether the given number is less than 0. If
it is less than zero then it is negative therefore the condition becomes true then the
statement The number is negative is executed. If the number is not less than zero the If
else construct skips the first statement and prints the second statement declaring that the
number is positive.
Syntax
The syntax in the statement ‘a’ represents a complex if statement which combines
different conditions using the and operator in this case if all the conditions are true only
then the whole statement is considered to be true. Even if one condition is false the whole
if statement is considered to be false.
The statement ‘b’ uses the logical operator or (//) to group different expression to be
checked. In this case if any one of the expression if found to be true the whole expression
considered to be true, we can also uses the mixed expressions using logical operators and
and or together.
Nested if Statement
The if statement may itself contain another if statement is known as nested if statement.
Syntax:
if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
The if statement may be nested as deeply as you need to nest it. One block of code will
only be executed if two conditions are true. Condition 1 is tested first and then condition
2 is tested. The second if condition is nested in the first. The second if condition is tested
only when the first condition is true else the program flow will skip to the corresponding
else statement.
Sample Code
1. #include <stdio.h> //includes the stdio.h file to your
program
2. main () //start of main function
3. {
4. int a,b,c,big //declaration of variables
15. }
Copyright exforsys.com
In the above program the statement if (a>c) is nested within the if (a>b). If the first If
condition if (a>b)
If (a>b) is true only then the second if statement if (a>b) is executed. If the first if
condition is executed to be false then the program control shifts to the statement after
corresponding else statement.
Sample Code
1. #include <stdio.h> //Includes stdio.h file to your
program
2. void main () // start of the program
3. {
4. int year, rem_4, rem_100, rem_400 // variable
declaration
5.
6. printf ("Enter the year to be tested") // message
for user
8.
9. rem_4 = year % 4 //find the remainder of year by 4
12.
13. if ((rem_4 == 0 && rem_100 != 0) rem_400 == 0)
16. else
17. printf ("No. It is not a leap year. \n") //print
the false condition
18.}
Copyright exforsys.com
The above program checks whether the given year is a leap year or not. The year given is
divided by 4,100 and 400 respectively and its remainder is collected in the variables
rem_4, rem_100 and rem_400. A if condition statements checks whether the remainders
are zero. If remainder is zero then the year is a leap year. Here either the year – y 400 is
to be zero or both the year – 4 and year – by 100 has to be zero, then the year is a leap
year.
The ELSE If Ladder
When a series of many conditions have to be checked we may use the ladder else if
statement which takes the following general form.
if (condition1)
statement – 1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else if (condition)
statement n;
else
default statement;
statement-x;
This construct is known as if else construct or ladder. The conditions are evaluated from
the top of the ladder to downwards. As soon on the true condition is found, the statement
associated with it is executed and the control is transferred to the statement – x (skipping
the rest of the ladder. When all the condition becomes false, the final else containing the
default statement will be executed.
/* Example program using If else ladder to grade the student according to the following
rules.
Marks Grade
70 to 100 DISTINCTION
60 to 69 IST CLASS
50 to 59 IIND CLASS
40 to 49 PASS CLASS
0 to 39 FAIL
Sample Code
1. #include <stdio.h> //include the standard stdio.h
header file
2. void main () //start the function main
3. {
4. int marks //variable declaration
5.
6. printf ("Enter marks\n") //message to the user
8.
9. if (marks <= 100 && marks >= 70) //check whether
marks is less than 100 or greater than 70
10. printf ("\n Distinction") //print
Distinction if condition is True
17. else
18. printf ("Fail") //If all condition fail apply
default condition print Fail
19.}
Copyright exforsys.com
The above program checks a series of conditions. The program begins from the first if
statement and then checks the series of conditions it stops the execution of remaining if
statements whenever a condition becomes true.
In the first If condition statement it checks whether the input value is lesser than 100 and
greater than 70. If both conditions are true it prints distinction. Instead if the condition
fails then the program control is transferred to the next if statement through the else
statement and now it checks whether the next condition given is whether the marks value
is greater than 60 If the condition is true it prints first class and comes out of the If else
chain to the end of the program on the other hand if this condition also fails the control is
transferred to next if statements program execution continues till the end of the loop and
executes the default else statement fails and stops the program.
Unlike the If statement which allows a selection of two alternatives the switch statement
allows a program to select one statement for execution out of a set of alternatives. During
the execution of the switch statement only one of the possible statements will be executed
the remaining statements will be skipped. The usage of multiple If else statement
increases the complexity of the program since when the number of If else statements
increase it affects the readability of the program and makes it difficult to follow the
program. The switch statement removes these disadvantages by using a simple and
straight forward approach.
Switch (expression)
{
Case case-label-1;
Case case-label-2;
Case case-label-n;
………………
Case default
}
When the switch statement is executed the control expression is evaluated first and the
value is compared with the case label values in the given order. If the label matches with
the value of the expression then the control is transferred directly to the group of
statements which follow the label. If none of the statements matches then the statement
against the default is executed. The default statement is optional in switch statement in
case if any default statement is not given and if none of the condition matches then no
action takes place in this case the control transfers to the next statement of the if else
statement.
Sample Code
1. #include <stdio.h>
2. void main ()
3. {
4. int num1, num2, result
5. char operator
6.
7. printf ("Enter two numbers")
11.
12. switch (operator)
13. {
14. case '+':
15. result = num1 + num2
16. break
19. break
22. break
26. else
27. {
28. printf ("warning : division by zero \n")
29. result = 0
30. }
31. break
32. default:
33. printf ("\n unknown operator")
34. result = 0
35. break
36. }
37. printf ("%d", result)
38.}
Copyright exforsys.com
In the above program the break statement is need after the case statement to break out of
the loop and prevent the program from executing other cases.
The goto statement is simple statement used to transfer the program control
unconditionally from one statement to another statement. Although it might not be
essential to use the goto statement in a highly structured language like C, there may be
occasions when the use of goto is desirable.
Syntax
a> b>
goto label;
label; …………
………… …………
………… …………
………… goto
Label; label;
Statement;
The goto requires a label in order to identify the place where the branch is to be made. A
label is a valid variable name followed by a colon.
The label is placed immediately before the statement where the control is to be
transformed. A program may contain several goto statements that transferred to the same
place when a program. The label must be unique. Control can be transferred out of or
within a compound statement, and control can be transferred to the beginning of a
compound statement. However the control cannot be transferred into a compound
statement. The goto statement is discouraged in C, because it alters the sequential flow of
logic that is the characteristic of C language.
Sample Code