0% found this document useful (0 votes)
11 views

PW Skills A-14: Q1. What Are Conditional Statements? Explain Conditional Statements With Syntax and Examples

Uploaded by

shubhangiv05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

PW Skills A-14: Q1. What Are Conditional Statements? Explain Conditional Statements With Syntax and Examples

Uploaded by

shubhangiv05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PW SKILLS A-14

Q1. What are conditional statements? Explain conditional


statements with syntax and examples.
Ans.

Conditional statements control behavior in JavaScript and


determine whether or not pieces of code can run. There are multiple
different types of conditionals in JavaScript including: “If” statements:
where if a condition is true it is used to specify execution for a block of
code.
Conditional syntax in programming languages is the structure used to
represent conditional statements, which are statements that establish a
relationship between two ideas. The basic structure of a conditional statement
in pseudocode is:

• If: A Boolean condition


• Then: The consequent
• Else: The alternative
• End If: The end of the statement
Example:
// conditional--
let num = 20;

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");
}

Q3. What are loops, and what do we need them? Explain


different types of loops with their syntax and examples.
Ans.
A loop in programming is a sequence of instructions that repeats until a
specific condition is met. Loops are a fundamental programming concept that
are used to save time, reduce errors, and make code more organized.

Here are some benefits of using loops:

• Create dynamic programs: Loops allow programmers to create programs that


can perform a variety of tasks
• Shorten code: Loops can reduce hundreds of lines of code to just a few
• Write code once: Programmers can write code once and repeat it as many
times as needed.
Here are some types of loops and their syntax:

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;
}

Q4. Generate numbers between any 2 given numbers.


Ex:
const num1 = 10;
const num2 = 25;
Output: 11, 12, 13, …., 25
Ans.

function getAllNumbersBetween(x, y) {

var numbers = [];

for (var i = x; i < y; i++) {

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;
}

You might also like