|
Page 1 of 2
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.
The If structure has the following syntax
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
Calculate the absolute value of an integer # include <stdio.h> //Include the stdio.h file void main () // start of the program { int numbers; // declare the variables printf ("Type a number:"); // message to the user scanf ("%d", &number); // read the number from standard input if (number < 0) // check whether the number is a negative number number = -number; // if it is negative then convert it into positive printf ("The absolute value is %d \n", number ); // print the value }
%23%20include%20%3Cstdio.h%3E%20%2F%2FInclude%20the%20stdio.h%20file%0D%0Avoid%20main%20%28%29%20%2F%2F%20start%20of%20the%20program%0D%0A%7B%0D%0A%20%20%20%20%20int%20numbers%3B%20%20%20%2F%2F%20declare%20the%20variables%0D%0A%20%20%20%20%20printf%20%28%22Type%20a%20number%3A%22%29%3B%20%20%2F%2F%20message%20to%20the%20user%0D%0A%20%20%20%20%20scanf%20%28%22%25d%22%2C%20%26number%29%3B%20%20%2F%2F%20read%20the%20number%20from%20standard%20input%0D%0A%20%20%20%20%20if%20%28number%20%3C%200%29%20%20%2F%2F%20check%20whether%20the%20number%20is%20a%20negative%20number%0D%0A%20%20%20%20%20number%20%3D%20-number%3B%20%20%20%2F%2F%20if%20it%20is%20negative%20then%20convert%20it%20into%20positive%0D%0A%20%20%20%20%20printf%20%28%22The%20absolute%20value%20is%20%25d%20%5Cn%22%2C%20number%29%3B%20%20%2F%2F%20print%20the%20value%0D%0A%7D
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 construct:
The syntax of the If else construct is as follows:-
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.
Program find whether a number is negative or positive #include <stdio.h> //include the stdio.h header file in your program void main () // start of the main { int num; // declare variable num as integer printf ("Enter the number"); // message to the user scanf ("%d", &num); // read the input number from keyboard if (num < 0) // check whether number is less than zero printf ("The number is negative"); // if it is less than zero then it is negative else // else statement printf ("The number is positive"); // if it is more than zero then the given number is positive }
%23include%20%3Cstdio.h%3E%20%20%2F%2Finclude%20the%20stdio.h%20header%20file%20in%20your%20program%0D%0A%20%20void%20main%20%28%29%20%20%20%2F%2F%20start%20of%20the%20main%20%0D%0A%20%20%7B%0D%0A%20%20%20%20int%20num%3B%20%20%2F%2F%20declare%20variable%20num%20as%20integer%0D%0A%20%20%20%20printf%20%28%22Enter%20the%20number%22%29%3B%20%20%2F%2F%20message%20to%20the%20user%0D%0A%20%20%20%20scanf%20%28%22%25d%22%2C%20%26num%29%3B%20%20%20%2F%2F%20read%20the%20input%20number%20from%20keyboard%20%0D%0A%20%20%20%20if%20%28num%20%3C%200%29%20%20%20%2F%2F%20check%20whether%20number%20is%20less%20than%20zero%0D%0A%20%20%20%20%20%20%20printf%20%28%22The%20number%20is%20negative%22%29%3B%20%20%20%2F%2F%20if%20it%20is%20less%20than%20zero%20then%20it%20is%20negative%20%0D%0A%20%20%20%20else%20%20%2F%2F%20else%20statement%0D%0A%20%20%20%20%20%20%20printf%20%28%22The%20number%20is%20positive%22%29%3B%20%20%2F%2F%20if%20it%20is%20more%20than%20zero%20then%20the%20given%20number%20is%20positive%20%0D%0A%7D
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.
Compound Relational tests:
C language provides the mechanisms necessary to perform compound relational
tests. A compound relational test is simple one or more simple relational tests
joined together by either the logical AND or the logical OR operators. These
operators are represented by the character pairs && // respectively. The
compound operators can be used to form complex expressions in C.
Syntax
a> if
(condition1 && condition2 && condition3)
b> if (condition1 // condition2 // condition3) |
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.
Nested if Statment Example #include <stdio.h> //includes the stdio.h file to your program main () //start of main function { int a,b,c,big; //declaration of variables printf ("Enter three numbers"); //message to the user scanf ("%d %d %d", &a, &b, &c); //Read variables a,b,c, if (a > b) // check whether a is greater than b if true then if (a > c) // check whether a is greater than c big = a ; // assign a to big else big = c ; // assign c to big else if (b > c) // if the condition (a > b) fails check whether b is greater than c big = b; // assign b to big else big = c; // assign c to big printf ("Largest of %d, %d & %d = %d", a,b,c,big ); //print the given numbers along with the largest number }
%23include%20%3Cstdio.h%3E%20%2F%2Fincludes%20the%20stdio.h%20file%20to%20your%20program%0D%0A%20%20%20main%20%28%29%20%2F%2Fstart%20of%20main%20function%0D%0A%20%20%20%7B%0D%0A%20%20%20%20%20int%20a%2Cb%2Cc%2Cbig%3B%20%2F%2Fdeclaration%20of%20variables%0D%0A%20%20%20%20%20printf%20%28%22Enter%20three%20numbers%22%29%3B%20%2F%2Fmessage%20to%20the%20user%0D%0A%20%20%20%20%20scanf%20%28%22%25d%20%25d%20%25d%22%2C%20%26a%2C%20%26b%2C%20%26c%29%3B%20%2F%2FRead%20variables%20a%2Cb%2Cc%2C%0D%0A%20%20%20%20%20if%20%28a%20%3E%20b%29%20%2F%2F%20check%20whether%20a%20is%20greater%20than%20b%20if%20true%20then%0D%0A%20%20%20%20%20if%20%28a%20%3E%20c%29%20%2F%2F%20check%20whether%20a%20is%20greater%20than%20c%0D%0A%20%20%20%20%20%20%20%20big%20%3D%20a%20%3B%20%2F%2F%20assign%20a%20to%20big%0D%0A%20%20%20%20%20else%20big%20%3D%20c%20%3B%20%2F%2F%20assign%20c%20to%20big%0D%0A%20%20%20%20%20else%20if%20%28b%20%3E%20c%29%20%2F%2F%20if%20the%20condition%20%28a%20%3E%20b%29%20fails%20check%20whether%20b%20is%20greater%20than%20c%0D%0A%20%20%20%20%20%20%20%20big%20%3D%20b%3B%20%2F%2F%20assign%20b%20to%20big%0D%0A%20%20%20%20%20else%20big%20%3D%20c%3B%20%2F%2F%20assign%20c%20to%20big%0D%0A%20%20%20%20%20printf%20%28%22Largest%20of%20%25d%2C%20%25d%20%26%20%25d%20%3D%20%25d%22%2C%20a%2Cb%2Cc%2Cbig%29%3B%20%2F%2Fprint%20the%20given%20numbers%20along%20with%20the%20largest%20number%0D%0A%20%20%20%7D
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.
Program to determines if a year is a leap year using compound if else construct #include <stdio.h> //Includes stdio.h file to your program void main () // start of the program { int year, rem_4, rem_100, rem_400; // variable declaration printf ("Enter the year to be tested"); // message for user scanf ("%d", &year); // Read the year from standard input. rem_4 = year % 4; //find the remainder of year by 4 rem_100 = year % 100; //find the remainder of year by 100 rem_400 = year % 400; //find the remainder of year by 400 if ((rem_4 == 0 && rem_100 != 0) || rem_400 == 0) //apply if condition 5 check whether remainder is zero printf ("It is a leap year. \n"); // print true condition else printf ("No. It is not a leap year. \n"); //print the false condition }
%23include%20%3Cstdio.h%3E%20%2F%2FIncludes%20stdio.h%20file%20to%20your%20program%0D%0Avoid%20main%20%28%29%20%2F%2F%20start%20of%20the%20program%0D%0A%7B%0D%0A%20%20%20int%20year%2C%20rem_4%2C%20rem_100%2C%20rem_400%3B%20%2F%2F%20variable%20declaration%0D%0A%0D%0A%20%20%20printf%20%28%22Enter%20the%20year%20to%20be%20tested%22%29%3B%20%2F%2F%20message%20for%20user%0D%0A%20%20%20scanf%20%28%22%25d%22%2C%20%26year%29%3B%20%2F%2F%20Read%20the%20year%20from%20standard%20input.%0D%0A%0D%0A%20%20%20rem_4%20%3D%20year%20%25%204%3B%20%2F%2Ffind%20the%20remainder%20of%20year%20by%204%0D%0A%20%20%20rem_100%20%3D%20year%20%25%20100%3B%20%2F%2Ffind%20the%20remainder%20of%20year%20by%20100%0D%0A%20%20%20rem_400%20%3D%20year%20%25%20400%3B%20%2F%2Ffind%20the%20remainder%20of%20year%20by%20400%0D%0A%0D%0A%20%20%20if%20%28%28rem_4%20%3D%3D%200%20%26%26%20rem_100%20%21%3D%200%29%20%7C%7C%20rem_400%20%3D%3D%200%29%20%20%20%0D%0A%20%20%20%20%20%20%2F%2Fapply%20if%20condition%205%20check%20whether%20remainder%20is%20zero%0D%0A%20%20%20%20%20%20printf%20%28%22It%20is%20a%20leap%20year.%20%5Cn%22%29%3B%20%2F%2F%20print%20true%20condition%0D%0A%20%20%20else%0D%0A%20%20%20%20%20%20printf%20%28%22No.%20It%20is%20not%20a%20leap%20year.%20%5Cn%22%29%3B%20%2F%2Fprint%20the%20false%20condition%0D%0A%7D
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.
|