Even before computers, humans already created a program which affects outcome like programming a student’s mind about their grades, how they could pass or fail. in this lesson, we will talk about conditioning a program, a software how it will produce an output or outcome base on the given input.
Example: If a student always attends classes the student will pass the school year or else he will fail.
Example: If a student always attends classes the student will pass the school year or else he will fail.
The statement above presents a condition about how a student could pass or fail a class and in the same manner and principle, we could create a simple statement that will determine the output of a program in computer programming.
There are two types of a conditional statement in computer programming which will use ternary operators where the first operand is a Boolean expression, and the second and third operand values.
- IF ELSE STATEMENT
- SWITCH CASE STATEMENT
An operator will compare a given value to a specific value or constant. When a condition meet the program will execute a command or perform a task for an output display or value to return.
List of Operators (Boolean Expressions)
- > Greater Than
- < Less Than
- = or == Equal To
- != or <> Not Equal To
- <= Less Than or Equal To
- >= Greater Than or Equal To
Basic Mathematical Operators
- + Addition
- - Subtraction
- / Division
- * Multiplication
In a conditional statement, we will use the Boolean expression
Example:
5 > 3
Where 5 is the second operand or the left operand and a value, 3 is the third operand and also a value while > or greater than is the first operand or a Boolean expression.
Example (IF ELSE STATEMENT):
C/C++/Java
If (attendance_count = 180) {
remarks = "Pass";
}Else{
remarks = "Fail";
}
VB.Net
If attendance_count = 180 then
remarks = ""Pass";
Else
remarks = "Fail";
End If
Explanation:
In the example above, If (attendance_count = 180) { or If attendance_count = 180 then this line will evaluate the value of variable attendance_count to see if it is equal to 180 which is the number of days or less than. In this case, if the value is 180, it will put a value “Pass” in variable remarks and “Fail” if it does not reach 180. So if this lines If (attendance_count = 180) { or If attendance_count = 180 then return True it performs the next line which is giving remarks of Pass and if it returns False it will perform the line after Else.
Example (SWITCH CASE STATEMENT):
C/C++/Java
Switch (Grades){
case 95:
remarks = “Excellent”;
break;
case 85:
remarks = “Very Good”;
break;
case 75:
remarks = “Pass”;
break;
case 74:
remarks = “Failed”;
break;
}
VB.Net
Select Grades
case 95
remarks = “Excellent”
break
case 85
remarks = “Very Good”
break
case 75
remarks = “Pass”
break
case 74
remarks = “Failed”
break
End Select
Explanation:
Just like IF ELSE STATEMENT, SWITCH CASE or SELECT CASE is a conditional algorithm using Boolean result to perform specific task. The process Is the program will read the value of variable Grades and compare it to different cases values like Switch Grades to case 1. It means that if the value of Grades variable is equal to 95 then variable remarks will be assign with a value “Excellent”, “Very Good” for 85, “Pass” for 75 and “Failed” for 74.
break; = this line of code will tell the program to execute until there only, it will break the operation without performing the next lines of codes.
Example Program in Java:
IF ELSE STATEMENT
public class BooleanExpression{
public static void main(String[] args){
//Start Declaration
int Grade;
String remarks;
//End Declaration
//Start Initialization
Grade = 85;
Remarks = “”;
//End Initialization
//Start If Else Condition
If (Grade == 95){
remarks = “Excellent”;
remarks = “Excellent”;
}else if(Grade == 85){
remarks = “Very Good”;
}else if(Grade == 75){
r emarks = “Pass”;
}else if(Grade == 74){
remarks = “Failed”;
}
//End Condition
}
}
}
SWITCH CASE
public class BooleanExpression
public static void main(String[] args){
//Start Declaration
int Grade;
String remarks;
//End Declaration
//Start Initialization
Grade = 85;
Remarks = “”;
//End Initialization
//Start Condition
//Start Condition
Switch (Grade){
case 95:
remarks = “Excellent”;
break;
case 85:
remarks = “Very Good”;
break;
case 75:
remarks = “Pass”;
break;
case 74:
remarks = “Failed”;
break;
}
//End Condition
}
}
No comments:
Post a Comment