Control Structure in Javascript
Chapter 15 · Tamil Nadu State Board
- 7 min read
- 4-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains how JavaScript control structures choose which statements run and how loops repeat actions. Branching statements select actions according to conditions, while loops repeat code. It also teaches how break and continue change a loop’s flow. These structures help programmers control the order and repetition of instructions.
Written by AI from this lesson. How we check it
Key points
- Conditional statements can run or skip statements depending on a condition’s value.
From your textbook
“Conditional statements execute or skip one or set of statements depending on the value of a specified conditional expression.”
- JavaScript branching includes if, if...else, else if, and switch statements.
From your textbook
“• if statement • if … else statement • else if statement • switch statement”
- An if...else structure provides a separate path for when its condition is false.
From your textbook
“which provides an alternate path for execution to follow if the condition evaluates to false.”
- A switch checks an expression against case labels and can use default when none match.
From your textbook
“The default structure is can be at the end of a switch structure if the result of the expression that do not match any of the case labels.”
- JavaScript has for, while, and do...while loops for repeating code.
From your textbook
“JavaScript supports three kinds of looping statements. They are • for loop • while loop • do..while loop”
- The for loop uses initialization, a condition, and an increment or decrement to control repetition.
From your textbook
“The first part of the loop initialize a variable which is also called as control variable.”
- Break ends a loop early, while continue skips to the next iteration.
From your textbook
“The break statement will terminate the loop early.”
Formulas in this chapter
- if statement formif (condition) { True block; }Explained here
- if...else formif (expression) { statements if true } else { statements if false }Explained here
- switch structureswitch(expression) { case label1: statements1; break; case label2: statements2; break; default: statements; }Explained here
- for loop syntaxfor(initialization; condition; increment/decrement)Explained here
- while loop syntaxwhile (condition)Explained here
- do..while loop syntaxdo body of the loop } while (expression);Explained here
Conditional Statements and Branching
JavaScript normally runs statements in the order they appear. A conditional statement can run or skip statements based on a condition. The two control types are branching or selection, and looping or repetitive control.
Definition
- Branching
- Branching transfers control from the current statement to another statement or construct, changing the execution sequence.
Branching statements
- if statement
- if ... else statement
- else if statement
- switch statement
if, if...else, and else if Statements
An if statement checks a condition. If it is true, JavaScript runs the statements inside the true block. If it is false, the if form does not run that block.
if (condition) { True block; }
condition is the expression being checked. The true block contains the statements that run when the condition is true.
Worked example
Test whether an entered age is at least 16.
Try it first, then show the solutionSolution
- Get the age using prompt("Please enter your Age :", "0").
- Check whether age>=16.
- If the condition is true, show the message "You Are Eligible to Vote ....".
Answer: The eligibility message appears when age>=16.
Use if...else when the program needs one action for a true condition and another for a false condition. An else if clause checks a new condition when the earlier condition is false. Further else if clauses can check more conditions, and a final else can handle the remaining case.
if (expression) { statements if true } else { statements if false }
expression is the condition. The first block runs if it is true; the else block runs if it is false.
Worked example
Decide whether an entered age is enough to get a Driving Licence.
Try it first, then show the solutionSolution
- Get the age using prompt("Please enter your Age :", "0").
- Check whether age>=18.
- If true, show "You Are Eligible to get Driving Licence..".
- Otherwise, show "You Are Not Eligible to get Driving Licence..".
Answer: The program shows the message that matches the condition.
Choosing a condition structure
| Structure | When it is useful |
|---|---|
| if | Run a block only when a condition is true. |
| if...else | Choose between two paths, one for true and one for false. |
| else if | Check a new condition when the earlier condition is false. |
Using else if to Assign Grades
A chain of else if clauses checks conditions in order. The program follows the matching path. This form is easier to read than writing the same choices as deeply nested if statements.
Grade conditions in the program
- marks>90: Outstanding
- (marks>70) && (marks<=90): Excellent
- (marks>50) && (marks<=70): Good
- (marks>40) && (marks<=50): Satisfectory
- Otherwise: Poor and have to re-appear Exam
Worked example
Choose a grade message from entered marks out of 100.
Try it first, then show the solutionSolution
- Read marks using prompt("Please enter your Marks/100 :", "0").
- Check each condition in order, starting with marks>90.
- Use the message for the first matching condition.
- If none of the conditions match, use the final else message.
Answer: The program writes the grade message that matches the entered marks.
Remember: In an else if chain, each later condition is checked only when the earlier conditions are false.
switch, case, break, and default
A switch statement is another way to choose among possible results of an expression. JavaScript compares the expression's result with the case labels. It runs the statements for a matching label.
switch(expression) { case label1: statements1; break; case label2: statements2; break; default: statements; }
expression is compared with the case labels. A matching case runs its statements. default provides statements for a result that matches no case label.
Definition
- break
- break exits the switch statement after the appropriate case has been handled.
Definition
- default
- default provides the path to run when the expression does not match any case label.
The grade example first assigns a number to grade based on marks. The switch then uses grade to choose the matching message. Its cases use break to exit, while default handles the remaining grade.
Remember: Use break to leave a switch case, and use default for results that match no case label.
JavaScript loops
Definition
- Loop
- A loop repeatedly runs the same part of a program, often with different values.
Three looping statements
- for loop
- while loop
- do..while loop
A for loop is useful when the number of repetitions is set in advance. Its parentheses contain three parts, separated by semicolons. These set the starting value, the condition for continuing, and how the control variable changes.
for(initialization; condition; increment/decrement)
initialization sets the control variable; condition decides whether the loop continues; increment/decrement changes the control variable.
Worked example
What multiplication table does the for loop example display when no1 is 3?
Try it first, then show the solutionSolution
- The loop starts with no2=0 and continues while no2<=10.
- Each turn displays no1 multiplied by no2.
- The value of no2 increases by 1 after each turn.
Answer: It displays the products from 3 x 0 = 0 through 3 x 10 = 30.
Remember: In a for loop, the condition controls how many times the loop runs.
break and continue statements
How break and continue change a loop
| break | continue |
|---|---|
| Ends the loop early. | Ends the current iteration and starts the next one. |
| In the example, the loop stops when n is 5, so it displays 0 1 2 3 4. | In the example, the value 5 is skipped, so it displays 0 1 2 3 4 6 7 8 9 10. |
Definition
- break statement
- The break statement terminates the loop before its condition would normally end it.
Definition
- continue statement
- The continue statement skips the rest of the current iteration and moves on to the next iteration.
Remember: break exits the loop; continue skips only the current iteration.
while loop
Definition
- while loop
- A while loop repeatedly runs its body as long as its condition is true.
while (condition)
The condition is checked before the body runs.
The interpreter checks the condition first. If it is true, the body runs and control returns to the condition. If it is false, the body is skipped and the program moves on.
Worked example
What values does the while loop example display?
Try it first, then show the solutionSolution
- no2 starts at 0.
- The loop continues while no2<=5.
- It displays no2 and then adds 1 to it.
Answer: It displays 0 1 2 3 4 5.
do..while loop
Definition
- do..while loop
- A do..while loop runs its body before checking the condition, so the body runs at least once.
do body of the loop } while (expression);
The body runs first; expression is checked at the end to decide whether to repeat.
When the condition is checked
| while loop | do..while loop |
|---|---|
| Checks the condition before running the body. The body may not run. | Checks the condition after running the body. The body runs at least once. |
Worked example
What values does the do..while example display?
Try it first, then show the solutionSolution
- no2 starts at 0.
- The body displays no2 and adds 2.
- The loop repeats while no2<=10.
Answer: It displays 0 2 4 6 8 10.
Remember: The key difference is when the condition is checked: before the body in while, and after the body in do..while.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is branching in JavaScript?
Branching transfers control from the current statement to another statement or construct. This changes the order in which the program runs. JavaScript branching statements include if, if...else, else if, and switch.
What is the difference between if and if...else in JavaScript?
An if statement runs its block only when the condition is true. If the condition is false, that block is skipped. An if...else statement provides another block, which runs when the condition is false.
How does an else if statement work in JavaScript?
An else if clause checks a new condition when the earlier condition is false. A program can use several else if clauses to check choices in order. A final else can handle the case when none of the conditions match.
What do break and default do in a JavaScript switch statement?
A matching case in a switch runs its statements. break exits the switch after that case is handled. The default structure runs when the expression does not match any of the case labels.
What are the three types of loops in JavaScript?
JavaScript supports three looping statements: the for loop, the while loop, and the do..while loop. Each repeats a body of code. They differ in how the loop is controlled and when its condition is checked.
What are the three parts of a JavaScript for loop?
The for loop has initialization, condition, and increment/decrement parts. Initialization sets the control variable. The condition determines whether the loop continues. Increment/decrement changes the control variable after each iteration.
Show 2 more questions
What is the difference between break and continue in JavaScript?
break terminates the loop early. continue ends the current iteration and moves to the next one. In the given examples, break stops before displaying 5, while continue skips 5 but the loop carries on.
What is the difference between while and do..while loops?
A while loop checks its condition before running the body, so the body may be skipped. A do..while loop checks its condition after the body runs, so its body always runs at least once.
Book back answers
18 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
Choose the correct answers
Which conditional statement is used to transfer control from current statement to another statement?
- (a) Branching
- (b) Sequencing
- (c) Looping
- (d) Interating
Try it, then show the answerAnswer
(a) Branching
Branching transfers control from the current statement to another statement or construct.
From the book: “Branching is a transfer of control from the current statement to another statement”
_______ statement can be used as alterative to if-else statement.
- (a) While
- (b) If
- (c) Else-if
- (d) Switch
Try it, then show the answerAnswer
(d) Switch
The chapter describes switch as an alternative to using an if...else structure.
From the book: “JavaScripts offers the switch statement as an alternate to using if...else structure.”
Which statement in switch case is used to exit the statement once the appropriate choice is found?
- (a) Exit
- (b) Default
- (c) Case
- (d) Break
Try it, then show the answerAnswer
(d) Break
The break statement exits the switch once the appropriate choice is found.
From the book: “The break statement is also used commonly within switch to exit the statement once the appropriate choice is found.”
Which of the following is not a looping statement?
- (a) Switch
- (b) While
- (c) Do-While
- (d) For
Try it, then show the answerAnswer
(a) Switch
The chapter lists for, while, and do..while as looping statements; switch is a branching statement.
From the book: “JavaScript supports three kinds of looping statements. They are • for loop • while loop • do..while loop”
Practice
What does a conditional statement use to decide whether statements run?
Exam questions
Practice questions with model answers. These are not from an official paper.
What are the two main types of control structures described in this lesson?2 marks
They are Branching / Selection and Looping / repetitive.
How does an if...else statement work?2 marks
It evaluates a condition and executes the true path if the condition is true; otherwise, it follows the alternate path in the else clause.
Differentiate between break and continue in a loop.2 marks
Break terminates the loop early. Continue ends the current iteration and moves on to the next iteration.
Explain the difference between while and do..while loops.5 marks
A while loop checks its condition before running the body, so the body may be skipped. A do..while loop checks its expression at the end, so its body runs at least once.
Watch out
- Do not confuse break with continue: break ends the loop, while continue advances to the next iteration.
- Do not assume while and do..while check their conditions at the same time; do..while checks at the end.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →