#java , #javaprogramming #ifelse #ifelsestatement
code link:-https://docs.google.com/document/d/1z...
previous video link:- • Practical Question Set-1 Using Operators
What is Decision Making Statements?
It decides the direction of flow of program
Decision-making or control statements are the statements in a program that can control the execution order of the statements, i.e., the order in which the instructions in a program get executed.
We may classify the programming statements into three categories:
Sequence: simple programming statements executed one after another.
Decision-making or Selection: consists of two or more block of statements and only
One of them get executed based on a decision-making condition( if-else, switch, etc.)
Loops: Used to repeat a group of statements a certain number of times(for, while, do-while, goto, foreach etc.)
WHY ARE DECISION-MAKING STATEMENTS NEEDED ?
Suppose we have to code a program in which the user enters a number, and if the number is less than 10, we print the massage “number is less than 10” or “number is less than 10” otherwise. In such a case, we have to make a decision (based on the number) and print the message accordingly, and here the decision-making statements come in use
if
The if() statement checks the given condition and if the condition is true then the block of code/statements will execute otherwise not.
Syntax:
If(condition)
{
statements
}
if else
It consists of two parts (block of statements), and only one of them gets executed based on the test-condition. If the condition is true, then the code(statements) block of else gets executed
Syntax:
If(condition)
{
Statements ,executed when the condition is true
}
else{
Statements, executed when the condition is false
}
if-else-if ladder
The if-else-if ladder is used to evaluate multiple conditions. In this ladder structure, test conditions are evaluated in top to down manner. As soon as a true condition is encountered, the statement associated with it gets executed, and the rest of the ladder is skipped or bypassed.
If all the conditions are false, the final else statement gets executed. The last else acts as a default condition; that is, if all other test evaluations get failed, then the last else statement is executed.
If(condition1)
{
Code to be executed if condition1 is true
}
Else if(condition2)
{
Code to be executed if condtion2 is true
}
Else{
Code to be executed if all the conditions are false
}