PW Skills A-14: Q1. What Are Conditional Statements? Explain Conditional Statements With Syntax and Examples
PW Skills A-14: Q1. What Are Conditional Statements? Explain Conditional Statements With Syntax and Examples
if (num % 2 === 0) {
console.log("Given number is even number.");
}
if (num % 2 !== 0) {
console.log("Given number is odd number.");
};
Q2. Write a program that grades st9dents based on their
marks.
- If greater than 90 then A Grade
- If between 70 and 90 then a B grade
- If between 50 and 70 then a C grade
- If below 50 then an F Grade
Ans.
// GRADE BASED ON THEIR MARKS--
let marks = 75;
if(marks > 90){
console.log("Grade-A");
}
else if(marks>70 && marks<80){
console.log("Grade-B");
}
else if(marks>50 && marks<70){
console.log("Grade-C");
}
else if(marks<50){
console.log("Grade-F");
}
For loop
A loop that runs for a specific number of times. It uses a counter to increment or decrement
with each iteration. The syntax for a for loop in C is for (i = 0; i < 10; i++)
{ printf("%d\n", i+1); }.
While loop
A loop that repeats as long as a condition is true. The syntax for a while loop in C is while
(i <= 10) { printf("%d\n", i); i++; }.
Do-while loop
A loop that repeats until a condition becomes false. It always checks conditions at the end
of the loop. The syntax for a do-while loop in C is do { printf("%d\n", i+1); i++}
while (i < 10);.
Nested loop
A loop that appears inside another loop. For example, for(int i=0; i<7; i++) { for
(int j=8; j>4; j--) { print (i); print (j); } }.
Example--
for(i=0; i<=5; i++){
console.log(i);
}
j = 10;
while(j>=1){
console.log(j);
j -= 1;
}
function getAllNumbersBetween(x, y) {
numbers.push(i);
return numbers;
}
console.log(getAllNumbersBetween());
Q5. Use the while loop to print numbers from 1 to 25 in
ascending and descending order.
Ans.
// WHILE LOOP---
let i = 1;
while(i<=25){
console.log(i);
i += 1;
}
let j = 25;
while(j>=1){
console.log(j);
j -= 1;
}