9. statements (conditional statements)
TRANSCRIPT
Statement is instruction that execute at run time
Type of statements
1. Sequential statement
2. Conditional statement
3. iterative/looping statement
4. Branching statement
way2ITech
Sequential statement • These are those statements
which execute starting from line1 to line 2 and so on.
• Till now the programs we have done are example of sequential statements as lines/statements in programs are executed in the sequence as they are written.
way2ITech
Conditional statement
• Are those statements which will depend on condition for their execution.
Types of Conditional Statements
• If condition
• if-else condition
• Nested if-else
• If-else ladder
way2ITech
if condition
if(condition)
{
statement1;
statement2;
..........
}
Stament1 and statement2 are conditional statements as they depend on condition for their executing.
way2ITech
WAP to find greatest of 2 numbers using if condition
• lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
way2ITech
if-else condition
• if evaluates to true. It does nothing when the expression evaluates to false.
• Else is used when we want some statements to be executed when if evaluates to be false. So if the condition is true, if is executed else , else is executed.
way2ITech
Program
• WAP to enter Small Case Character And Output Upper Case Character and When input character is Upper case, Output small case character
way2ITech
Nested if-else
• if-else construct within either the body of the if statement or the body of an else statement or both. This is called nesting‘ of ifs.
way2ITech
If-else Ladder
• It is used to check multiple conditions.
• Every else is associated with its previous if.
• The last else goes to work only if all the conditions fail.
• Even in else if ladder the last else is optional.
way2ITech
• WAP to calculate tax for entered salary. If salary > 10000, tax is 2 %. If salary lies between 5000 and 10000, tax is 1.5 %. If salary is in between 3000 to 5000 , Tax is 1 % and if salary is lesser then 3000, Tax is 0%
way2ITech