Branching Statment
The C language programs follows a sequential form of execution of statements.
Many times it is required to alter the flow of sequence of instructions. C language
provides statements that can alter the flow of a sequence of instructions. These
statements are called as control statements. To jump from one part of the
program to another,these statements help. The control transfer may be
unconditional or conditional. Branching Statemnt are of following categories:
Types Of branching Statemnt
1. If Statement
2. If else Statement
3. Nested if Statement
4. Switch Statement
5.if elseif...if
1. If Statement
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed.
If the Boolean expression evaluates to false, then the first set of code after the end of the
'if' statement (after the closing curly brace) will be executed.
Syntax:
if(condition)
statement;
Example:
#include <stdio.h>
#include <conio.h>
int main () {
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
2. If...else statment
An if statement can be followed by an optional else if statement,
which is very useful to test various conditions using single if...else if statement.
C programming language assumes any non-zero and non-null values as true,
and if it is either zero or null, then it is assumed as false value.
Syntax:
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
else {
/* statement(s) will execute if the boolean expression is false */
}
Example:
#include <stdio.h>
#include <conio.h>
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else {
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
3. Nested if statment
The nested if statement in C programming language is used when multiple conditions need to be tested.
The inner statement will execute only when outer if statement is true otherwise control won't
even reach inner if statement.
Syntax:
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
Example:
#include <stdio.h>
#include <conio.h>
int main(){
int num;
printf("Enter a numbers\n");
scanf("%d", &num);
/* Using nested if statement to check two conditions*/
/* Outer if statement */
if(num < 500){
printf("First Condition is true\n");
if(num > 100){
printf("First and Second conditions are true\n");
}
}
printf("This will print always\n");
getch();
return(0);
}
4. Switch Statement
The if..else..if ladder allows you to execute a block code among many alternatives.
If you are checking on the value of a single variable in if...else...if, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also,
the syntax of switch statement is cleaner and easy to understand.
Syntax:
switch (n)
รข€‹{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
Example:
#include <stdio.h>
#include <conio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
5. if elseif...if
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed.
If the Boolean expression evaluates to false, then the first set of code after the end of the
'if' statement (after the closing curly brace) will be executed.
Syntax:
Syntax:
if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
}
else {
/* executes when the none of the above condition is true */
}
Example:
#include <stdio.h>
#include <conio.h>
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10\n" );
}
else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20\n" );
}
else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30\n" );
}
else {
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}
<< PREVIEW >>
<< NEXT >>
Comments
Post a Comment