LAS Q3 WEEK 2 For Uploading
LAS Q3 WEEK 2 For Uploading
Region VIII
Division of Eastern Samar
GUIUAN NATIONAL HIGH SCHOOL
Guiuan, Eastern Samar
COMPUTER PROGRAMMING
INTRODUCTION
What is an IF statement?
➢ The IF/ELSE statement executes a block of code if a specified condition is
true. If the condition returns false, another block of codes can be executed.
Example:
In this particular example, if the value of age is above or equal to 18, then
Javascript displays “You’re of legal age.”.
• CONDITION STATEMENT
-specifies the condition in which the IF statement checks to see
whether it’s true or not.
1
• BLOCK OF CODES
-to be executed when the condition is true or false.
if(condition){
*codes to execute if the condition is true
}
Ex:
var age = 15;
if(age<18){
console.log(“You are a minor.”);
}
Ex:
if(is the food delicious?){
*codes to be executed if the condition is true
}
else if(is food homecooked?){
*codes to be executed if the condition is true
}else{
*codes to be executed if the condition is false.
}
Ex:
if(do you to school?){ 1st statement/argument
if(do you go to Guiuan National High School?){ 2nd argument
*codes to be executed if the condition is true
}
2
else{ part of the 2nd argument
*codes to execute if condition is false.
}
}
else{ part of the 1st argument
*codes to be executed if condition is false
}
In this nested IF statement, the only time that the 2nd argument will get checked is
if the 1st argument returns true. If the 1st argument returns true, the else part of the
1st argument will get executed.
Sample Problem 1:
Create a code that will say “You passed” if the inputted grade is 75 and above
and “You failed” if the inputted grade is 74 below.
var grade = 75; let’s just initialize grade with the value of 75
if(grade>=75){
console.log(“You Passed”);
}
else{
console.log(“You failed”);
}
Sample problem 2:
Create a Nested statement that if your grade is below 75, it will say “You failed”.
If it’s above 75, check if it’s 75-85, 86-94, 95-100. If it’s 75-85, display “with
honors”. If it’s 86-94, display “with high honors”. If it’s 95-100, display “with
highest honor”. If it’s above 100, display “Grade above limit”.
3
Codes for Problem 2:
Solution1:
var grade = 90; let’s initialize value to 90. You can change it if you want
if(grade>=75){
if(grade>=75 && grade<=85){
console.log(“with honors!”);
}else if(grade>=86 && grade<=95){
console.log(“with high honors!”);
}else if(grade>=96 && grade<=100){
console.log(“with highest honors!”);
}else{
console.log(“Grade above limit!”);
}
}else{
console.log(“You failed!”);
}
Solution 2:
var grade = 90; let’s initialize value to 90. You can change it if you want
if(grade>=75){
if(grade>=95){
console.log(“with highest honor”);
}
else if(grade>=85){
console.log(“with high honor”);
}
else if(grade>=75){
console.log(“with honor”);
}
}
else{
console.log(“you failed”);
}
4
Sample Problem 3:
Catalina is a business woman. Create a code that will keep track of her daily
business expenses, monthly expenses, daily income and her monthly income.
Also, display if she is gaining from her business or taking a loss. If monthly
income is the same as monthly_expenses, display “Business is not gaining nor
losing money”.
if(monthly_income==monthly_expenses){
console.log(“Business is not gaining nor losing money.”);
}
else if(monthly_income>monthly_expenses){
console.log(“Catalina is gaining from her business.”);
}
else{
console.log(“Catalina is taking a loss in her business.”);
}