0% found this document useful (0 votes)
15 views8 pages

Chapter four

Uploaded by

Husein
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Chapter four

Uploaded by

Husein
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter four (4)

Control statements
In computer programming, control statements are essential constructs that govern the execution
flow within a program. They dictate which sections of code are executed and in what order,
enabling programs to make decisions, iterate through tasks, and exhibit dynamic behavior. Here's
a breakdown of the primary control statement categories:
A) Conditional Statements: These statements facilitate decision-making based on
predefined conditions. Classic examples include if, else if, and switch statements. For
instance, an if statement might evaluate a user's eligibility to vote (based on age) and
conditionally execute code blocks that display appropriate registration or voting
information.
B) Iteration Statements: These statements enable the controlled repetition of code blocks.
Common examples include for, while, and do-while loops. A for loop might be used to
iterate through a dataset ten times, performing calculations on each element. A while
loop, on the other hand, might be employed to continuously prompt the user for input
until they provide a valid response.
C) Jump Statements: These statements alter the program's flow by abruptly transferring
control to a designated code section. A prominent example is the break statement, which
facilitates exiting loops prematurely. Imagine a loop searching through an extensive list
for a specific item. Upon finding the item, a break statement can be used to terminate the
loop, optimizing performance by avoiding unnecessary iterations.

The strategic use of control statements empowers programmers to construct intricate and
versatile programs capable of adapting to various conditions, performing repetitive tasks
efficiently, and exhibiting intelligent behavior.

1. Conditional Statements
Conditional statements are the foundation for decision-making in programming and logic. They
allow you to control the flow of a program's execution based on whether a certain condition is
true or false. Here's a breakdown of how they work:
1. Condition: This is an expression that evaluates to either true or false. It often involves
comparisons (like greater than, less than, equal to) or checking the state of a variable.
2. Consequence: This is the block of code that gets executed if the condition is true.

A) IF statement
There are different types of conditional statements, but the most basic one is the if statement.
Here's the general format:
if (condition) {
// Code to execute if the condition is true
}

For example:
age = 18
if (age >= 18) {
print("You are eligible to vote.")
}

In this case, if the age variable is 18 or greater, the message "You are eligible to vote" will be
printed.

B) IF ELSE statement
Another common type is the if-else statement. This allows you to specify what happens if the
condition is false:
Syntex
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

For example:
weather = "sunny"
if (weather == "sunny") {
print("Go outside and enjoy the weather!")
} else {
print("Bring an umbrella, it might rain.")
}

C) ELSE IF and SWITCH statements


There are also more complex conditional statements like else if for handling multiple conditions
and switch statements for handling a set of possible values.
Syntax for else if conditional statement
if (condition1) {
// code block 1
}
else if (condition2) {
// code block 2
}
else {
// code block 3
}

Syntax for switch conditional statement


switch (expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
.
default:
// default statements
}

Conditional statements are essential for writing programs that can adapt and respond to different
situations. They are used in everything from simple games to complex applications.
2. Iteration Statements
Iteration statements, also called loops, are a fundamental concept in programming. They allow
you to execute a block of code repeatedly until a certain condition is met. This is useful for tasks
that need to be done multiple times, such as iterating through a list of items, calculating a sum, or
guessing a number. There are typically three main types of iteration statements in most
programming languages:
1. While loop: This loop continues to execute the code block as long as a specified
condition is true. The condition is checked before each iteration of the loop.

2. Do-while loop: This loop is similar to a while loop, but it guarantees that the code block
will be executed at least once, even if the condition is initially false. The condition is
checked after each iteration of the loop.
3. For loop: This loop is used for a predetermined number of iterations. It combines
initialization, condition checking, and increment/decrement steps into a concise syntax.
The choice of which loop to use depends on the specific situation. Here are some general
guidelines:
 Use a while loop when you don't know the exact number of iterations beforehand, but
you have a condition that determines when to stop.
 Use a do-while loop when you want to ensure the code block executes at least once, even
if the condition is initially false.
 Use a for loop when you know the exact number of times you want to iterate and want to
manage the loop counter in a single statement.
Knowing how to use iteration statements effectively is essential for writing efficient and well-
structured programs.

A) FOR loop
The syntax of for-loop is:
for (initialization; condition; update) {
// body of-loop
}
Here,
 initialization - initializes variables and is executed only once
 condition - if true , the body of for loop is executed if false , the for loop is
terminated
 update - updates the value of initialized variables and again checks the condition
The above diagram depicts Flowchart of for Loop in C++
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
Here is how this program works
Iteration Variable i Action
1st i=1 true 1 is printed. i is increased to 2.
2nd i=2 true 2 is printed. i is increased to 3.
3rd i=3 true 3 is printed. i is increased to 4.
4th i=4 true 4 is printed. i is increased to 5.
5th i=5 true 5 is printed. i is increased to 6.
6th i=6 false The loop is terminated

B) While loop
The syntax of for-loop is:
while (condition) {
// code block to be executed
}
Here,
 A while loop evaluates the condition
 If the condition evaluates to true , the code inside the while loop is executed.
 The condition is evaluated again.
 This process continues until the condition is false .
 When the condition evaluates to false , the loop terminates.
The above diagram depicts Flowchart of while Loop
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;

// while loop from 1 to 5


while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}
Here is how this program works
Iteration Variable i <= 5 Action
1st i=1 true 1 is printed and i is increased to 2.
2nd i=2 true 2 is printed and i is increased to 3.
3rd i=3 true 3 is printed and i is increased to 4
4th i=4 true 4 is printed and i is increased to 5.
5th i=5 true 5 is printed and i is increased to 6.
6th i=6 false The loop is terminated

C) Do …. While loop
The syntax of do … while loop is:
do {
// body of loop;
}
while (condition);
Here,
 The body of the loop is executed at first. Then the condition is evaluated.
 If the condition evaluates to true , the body of the loop inside the do statement is executed
again.
 The condition is evaluated once again.
 If the condition evaluates to true , the body of the loop inside the do statement is executed
again.
 This process continues until the condition evaluates to false . Then the loop stops.
The above diagram depicts Flowchart of do...while Loop
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;

// do...while loop from 1 to 5


do {
cout << i << " ";
++i;
}
while (i <= 5);
return 0;
}
Here is how the program works.
Iteration Variable i <= 5 Action
i = 1 not checked 1 is printed and i is increased to 2
1st i = 2 true 2 is printed and i is increased to 3
2nd i = 3 true 3 is printed and i is increased to 4
3rd i = 4 true 4 is printed and i is increased to 5
4th i = 5 true 5 is printed and i is increased to 6
5th i = 6 false The loop is terminated

3. Jump Statements
 break: This statement is used to terminate a loop or switch statement prematurely. Once
the break is encountered, the loop or switch will immediately stop iterating and
execution will jump to the code following the loop or switch.
 continue: This statement is used within loops to skip the remaining code in the current
iteration and immediately jump to the beginning of the next iteration. Essentially, it
allows you to continue looping but tells the current iteration to stop processing and move
on to the next one.
 goto: This statement is used less frequently in modern programming due to concerns
about code readability. It allows you to jump to a specific labeled location in your code.
While powerful, excessive use of goto can make code hard to understand and follow.
 return: This statement is used to exit a function. It can optionally be used to return a
value back to the place where the function was called. Once a function encounters a
return statement, the function stops executing and control jumps back to the line where
the function was called.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy