Conditional STMT
Conditional STMT
MAKING
AND
BRANCHING
All programming languages support at least one construct that
supports selection, i.e., decision making. This construct specifies
what to do if one or more conditions are met, and also what to do
otherwise. Complex situations are handled by chaining together a
number of such constructs.
The if statement is a powerful decision making statement and is used to control the flow of execution of
statement and is used in conjunction with an expression.
It takes the following form
if(test expression)
It allows the computer to evaluate the expression first and then depending on whether the value of the
expression is true or false, it transfer the control to a particular statement.
Examples are:
if (bank balance is zero)
borrow money
if (room is dark)
put on lights
The if statement can be implemented in different forms depending
on the complexity of conditions to be tested. The different forms are:
simple if statement
if……else statement
Nested if……else statement
else if ladder
SIMPLE IF STATEMENT :
The general form of simple if statement is
if(test expression)
{
statement-block;
}
statement-x;
The statement-block may be single statement or a group of statements.
If the test expression is true, the statement-block will be executed, otherwise
the statement-block will be skipped and the execution will jump to the
statement=x.
Example:
………………………………..
if(category == SPORTS)
{
marks=marks + bonus_marks;
}
printf(“%f”, marks);
………………………………………
Program1: The program here reads four values a, b, c, and d from the terminal and evaluates the ratio of
la+b) to (c-d) and prints the result, if c-d is not equal to zero.
main()
{
int a, b, c, d;
float ratio;
printf("Enter four integer values\n");
scanf (“%d %d %d %d“, &a,&b, &c, &d);
if (c-d 1- 0) /*Execute statement block */
{
ratio = (float)(a+b)/(float) (c-d);
printf("Ratio - in", ratio);
}
}
Output:
Enter four integer values : 12 23 34 45
Ratio - -3.181818
Program2: The program counts the number of boys whose weight is less than 50 kg and height greater than
170 cm.
Main()
{
int count, i;
float weight, height ;
count 0;
printf("Enter weight and height for 10 boys\n");
for (1 -1; 1 ce 10; 1++)
{
scanf(fp, weight, Height);
if (weight < 50 3 height > 170)
count = count + 1;
}
printf("Number of boys with weight < 50 kg\n”);
printf(and height > 190cm= %d\n”, count);
}
Output:
IF(test expression)
{
true-block statements(s)
}
else
{
false-block statements(s)
}
statements-x
Example:
The first test determines whether or not the student is a boy. If yes, the number of boys is increased
by 1 and the program continues to the second test. The second test again determines whether the
student is a girl. This is unnecessary. Once a student is identified as a boy, there is no need to test
for a girl. A student can be either a boy or a girl, not both.
……………………………………………….
if (code == 1)
boy = boy + 1;
else
girl = girl + 1;
……………………………………………….
Program to check number is negative or not.
#include<stdio.h>
main()
{
int num;
printf("Enter the number:");
scanf("%d", num); /* check whether the number is negative number */
if (num < 0)
printf("The number is negative.");
else
printf("The number is positive.");
}
Output:
Enter the number: 3
The number is positive.
NESTING OF IF…..ELSE STATEMENTS
When an if else statement is present inside the body of another “if” or “else” then this is
called nested if else.
Program to find largest of three numbers using nested if…else statement.
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: "); Output:
scanf("%lf %lf %lf", &n1, &n2, &n3);
Enter three numbers:
if (n1 >= n2){
if (n1 >= n3) -4.5
printf("%.2lf is the largest number.", n1); 3.9
else 5.6
printf("%.2lf is the largest number.", n3);
}
5.60 is the largest number.
else{
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0;
}
ELSE IF LADDER :
There is another way of putting ifs together when multipath decisions are involved. A multipath
decision Is a chain of ifs in which the statement associated with each else is an if it takes the
following general form as shown in figure.
This construct is known as the else if ladder. The conditions are evaluated from the top downwards.
As soon as a true condition is found the statement associated with it is executed and the control is
transferred to the statement –x. When all the n conditions become false, then the final else
containing the default statement will be executed, below figure shows the logic of execution else if
ladder statements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float m;
printf("enter the students marks\n");
scanf("%f",&m);
printf("\n the grades is ");
if(m<=39)
printf("F");
else if(m<=49)
printf("E");
else if(m<=59)
printf("D");
else if(m<=69)
printf("C");
else if(m<=79)
printf("B");
else if(m<=89)
printf("A");
else
printf("S");
return 0;
}
RULES FOR INDENTATION
When using control structures, a statement often controls many other statements that follow it. In Such a
situations it is a good practice to use indentation to show that the Indented statements are dependent on the
preceding controlling statement. Some guidelines that could be followed while using indentation are listed
below:
Indent statements that are dependent on the previous statements, provide at least three spaces of
indentation.
Align vertically else clause with their matching if clause.
Use braces on separate lines to Identify a block of statements.
Indent the statements in the block by at least three spaces to the right of the braces.
Align the opening and closing braces.
Use appropriate comments to signify the beginning and end of blocks.
Indent the nested statements as per the above rules.
Code only one clause or statement on each line.