Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
112 views

Control Flow Statements-Conditional Statements

The document discusses different types of control flow statements in programming including if, if-else, nested if, and switch-case statements. It provides examples of how each statement works and when they should be used. If statements execute code conditionally based on boolean expressions while switch-case statements allow executing different code blocks based on the value of a variable. Control flow statements are essential for conditional execution of code in a program.

Uploaded by

mitul sathawara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Control Flow Statements-Conditional Statements

The document discusses different types of control flow statements in programming including if, if-else, nested if, and switch-case statements. It provides examples of how each statement works and when they should be used. If statements execute code conditionally based on boolean expressions while switch-case statements allow executing different code blocks based on the value of a variable. Control flow statements are essential for conditional execution of code in a program.

Uploaded by

mitul sathawara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Dr. Balu L.

Parne
Assistant Professor,
CoED, SVNIT Surat.
Control Flow Statements:
 When we need to execute a set of statements based on a condition
then we need to use control flow statements.
 In programming, it's often desirable to execute a certain section of
code based upon whether the specified condition is true or false
(which is known only during the run time). For such cases, control
flow statements are used.
 if statement
 nested if statement
 if-else statement
 if-else-if statement
If statement :
 If statement consists a condition, followed by statement or a set of statements
as shown below:

 Here expression is a boolean expression (returns either true or false).


 If the expression is evaluated to true, statement(s) inside the body of if
(statements inside parenthesis) are executed.
 If the expression is evaluated to false, statement(s) inside the body of if are
skipped from execution.
If statement :
• The statements gets executed
only when the given
condition is true.
• If the condition is false then
the statements inside if
statement body are
completely ignored.
How if statement works?
Example of if statement:
If statement :
Nested if statement :
 When there is an if statement inside another if statement then it is
called the nested if statement.

 Statement1 would execute if the condition_1 is true. Statement2 would


only execute if both the conditions( condition_1 and condition_2) are
true.
If else statement:
 The if statement executes a certain
section of code if the test expression
is evaluated to true. The if statement
can have optional else statement.
Codes inside the body of else
statement are executed if the test
expression is false.
If else statement:
if-else-if Statement:
 if-else-if statement is used when we need to check multiple conditions.
 In this statement we have only one “if” and one “else”, however we can
have multiple “else if”.
 It is also known as the if-else-if Ladder.
 The most important point to note here is that in if-else-if statement, as
soon as the condition is met, the corresponding set of statements get
executed, rest gets ignored. If none of the condition is met then the
statements inside “else” gets executed.
if(condition_1) {
/*if condition_1 is true execute this*/ if (expression1)
statement(s); {
} // codes
else if(condition_2) { }
/* execute this if condition_1 is not met and else if(expression2)
* condition_2 is met */ {
statement(s); // codes
} }
else if(condition_3) { else if (expression3)
/* execute this if condition_1 & condition_2 are {
* not met and condition_3 is met */ // codes
statement(s); }
} .
. .
. else
else { {
/* if none of the condition is true // codes
* then these statements gets executed }
*/
statement(s);
}
if-else-if Statement:
Problem:
 The marks obtained by a student in 5 different subjects are input
through the keyboard. The student gets the division as per the
following rules:
 Percentage above or equal to 60 – First division
 Percentage between 50 and 59 – Second division
 Percentage between 40 and 49 – Third division
 Percentage less than 40 – Fail.
Write a program to calculate the division obtained by the student.
if (per >= 60)
{
printf(“First Division”);
}
else
{
if(per >=50)
{
printf(“Second Division”);
}
else
{
if (per>=40)
{
printf(“Third Division”);
}
else
{
printf(“Fail”);
}
}
}
 The above program uses nested if-elses.
 As the number of condition go on increasing the level of
indentation also goes on increasing. As a result, the whole
program creeps to the right.
 Care need to be exercised to match the corresponding ifs
and elses.
 Care needs to be exercised to match the corresponding pair
of braces.
if (per >= 60)
{
printf(“First Division”);
}
if((per >=50) && (per < 60))
{
printf(“Second Division”);
}
if ((per>=40) && (per <50))
{
printf(“Third Division”);
}
if (per < 40)
{
printf(“Fail”);
}
 In the above code we use the logical operators.
 The matching of the ifs with their corresponding elses in the
previous code get avoided, since there are no elses in this
program.
 The program doesn’t creep to the right.
 Moreover, no worry about the matching if elses and
corresponding braces.
 But, here even if the first condition turns out to be true, still
all other conditions are checked. This will increase the time
of execution of the program.
if (per >= 60)
{
printf(“First Division”);
}
else if(per >= 50 && per <60)
{
printf(“Second Division”);
}
else if(per >= 40 && per <50)
{
printf(“Third Division”);
}
else
{
printf(“Fail”);
}
 In the above program, we have used the else if ladder.
 The program reduces the indentation of the statements.
 In this case, every else is associated with its previous if. The last else
goes to work only if all the conditions fail.
 Moreover, if the first condition is satisfied, other conditions are not
checked.
Conditional Operator Revisited:
 It’s not necessary that the conditional operators should be used only in
arithmetic statements.
Switch Case statement:
 Switch case statement is used when we have number of options
(or choices) and we may need to perform a different task for
each choice.
 It provides an easy way to dispatch execution to different parts
of your code based on the value of an expression.
 Switch Case statement is mostly used with break statement even
though it is optional.
 First the variable, value or expression which is provided in the
switch parenthesis is evaluated and then based on the result, the
corresponding case block is executed that matches the result.
Switch Case statement:
 The switch statement evaluates
it's expression (mostly variable)
and compares with values of each
case label.
 The switch statement executes all
statements of the matching case
label.
 Suppose, the variable / expression
is equal to value2. In this case, all
statements of that matching case
is executed.
Switch Case statement:
 Case doesn’t always need to have order 1, 2, 3 and so on. It can
have any integer value after case keyword.
 Also, case doesn’t need to be in an ascending order always, you
can specify them in any order based on the requirement.
 You can also use characters in switch case.
 The expression given inside switch should result in a constant
value otherwise it would not be valid.
 Nesting of switch statements are allowed, which means you can
have switch statements inside another switch. However nested
switch statements should be avoided as it makes program more
complex and less readable.

You might also like