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

Oop Assign

The document covers loops and conditional statements in C++ programming, detailing types of loops such as for, while, and do-while, along with their syntax and examples. It also explains conditional statements including if, else, else if, and the ternary operator, providing syntax and examples for each. Additionally, the document includes a group work assignment with student names and a simple program to display numbers between 10 and 20.

Uploaded by

judy john
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)
3 views8 pages

Oop Assign

The document covers loops and conditional statements in C++ programming, detailing types of loops such as for, while, and do-while, along with their syntax and examples. It also explains conditional statements including if, else, else if, and the ternary operator, providing syntax and examples for each. Additionally, the document includes a group work assignment with student names and a simple program to display numbers between 10 and 20.

Uploaded by

judy john
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

Unit: Object oriented programming.

Unit code: BBC 2204


GROUPWORK ASSIGNMENT.
1. ANGWENYI JUNE JUDY KERUBO - SCT222-0324/2022
2. VELMA BENTA ATIENO – SCT222-0586/2022
3. NEEMA JOMO GEKENE – SCT222-0326/2022
4. BETH WAMUGWE KAMAU – SCT222-0297/2022

1
1. Loops and conditional statements.
Loop
In C++, a loop is a sequence of instructions that is repeated until a certain condition is
reached.
There are mainly two types of loops in C++:
a) Entry Controlled loops: In this type of loop, the test condition is tested before
entering the loop body. For Loop and While Loop is entry-controlled loops.
b) Exit Controlled Loops: In this type of loop, the test condition is tested or evaluated at
the end of the loop body. Therefore, the loop body will execute at least once,
irrespective of whether the test condition is true or false. the do-while loop is exit
controlled loop.

For loop.
A For loop is a repetition control structure that allows us to write a loop that is executed a
specific number of times. The loop enables us to perform n number of steps together in one
line.
Syntax
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
Explanation of the Syntax:
Initialization statement: This statement gets executed only once, at the beginning of the for
loop. You can enter a declaration of multiple variables of one type, such as int x=0, a=1, b=2.
These variables are only valid in the scope of the loop. Variable defined before the loop with
the same name are hidden during execution of the loop.
Condition: This statement gets evaluated ahead of each execution of the loop body, and
abort the execution if the given condition get false.
Iteration execution: This statement gets executed after the loop body, ahead of the next
condition evaluated, unless the for loop is aborted in the body (by break, goto, return or an
exception being thrown.)
Example:
// C++ program to Demonstrate for loop
#include <iostream>
using namespace std;
int main()

2
{
for (int i = 1; i <= 5; i++) {
cout << "Hello World\n";
}
return 0;
}
The output of the code will be:
Hello World
Hello World
Hello World
Hello World
Hello World

While loop.
This loop is used when you don't know the number of iterations beforehand, but you
know the condition under which the loop should continue.
Syntax
initialization expression;
while (test_expression)
{
// statements

update_expression;
}

Example:
// C++ program to Demonstrate while loop
#include <iostream>
using namespace std;
int main()
{
// initialization expression
int i = 1;

3
// test expression
while (i < 6) {
cout << "Hello World\n";

// update expression
i++;
}

return 0;
}
The output will be:
Hello World
Hello World
Hello World
Hello World
Hello World

Do-while loop.
In Do-while loops also the loop execution is terminated on the basis of test conditions.
The main difference between a do-while loop and the while loop is in the do-while loop
the condition is tested at the end of the loop body, i.e do-while loop is exit controlled
whereas the other two loops are entry-controlled loops.
In a do-while loop, the loop body will execute at least once irrespective of the test
condition.
Syntax
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);

Example:
// C++ program to Demonstrate do-while loop
#include <iostream>
using namespace std;

4
int main()
{
int i = 2; // Initialization expression

do {
// loop body
cout << "Hello World\n";

// update expression
i++;

} while (i < 1); // test expression

return 0;
}

The output will be:


Hello World

Advantages of loops.
a) Code Reusability: Loops allow you to write code that can be executed multiple
times without duplicating the code. This promotes code reusability and makes
programs more concise and maintainable.

b) Efficiency: Loops enable you to perform repetitive tasks efficiently by automating


the process of iterating over data structures or executing a block of code multiple
times. This helps in optimizing the performance of programs, especially when
dealing with large datasets or complex algorithms.

c) Scalability: With loops, you can easily scale the number of iterations based on the
input data or requirements. Whether you need to process a small set of data or a
large dataset, loops provide a flexible way to handle varying workload sizes.

d) Reduced Errors: By using loops, you minimize the chances of introducing errors
that can occur when writing repetitive code manually. Loops ensure consistency in
execution and reduce the likelihood of making mistakes compared to writing the
same logic repeatedly.

e) Flexibility: C++ offers different types of loops such as for, while, and do-while
loops, each suited for specific scenarios. This flexibility allows you to choose the
most appropriate loop construct based on the requirements of your program,
enhancing code readability and maintainability.

f) Control Flow: Loops provide control flow mechanisms that enable you to dictate
the flow of execution within your program. You can use loop control statements
like break, continue, and nested loops to tailor the behavior of loops according to
specific conditions or criteria.

5
Conditional statement.

In C++, a conditional statement is a programming construct that allows you to execute


different blocks of code based on the evaluation of a condition. The most common types
of conditional statements in C++ are:
a) if -used to specify a block of code to be executed, if a specified condition is true.
b) else -used to specify a block of code to be executed, if the same condition is false.
c) else if -used to specify a new condition to test, if the first condition is false.
d) switch -used to specify many alternative blocks of code to be executed.

The if statement.
if statement is used to specify a block of C++ code to be executed if a condition is true.

Syntax
if (condition) {
// block of code to be executed if the condition is true
}

Example:
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}

In the example above we use two variables, x and y, to test whether x is greater than y
(using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18,
we print to the screen that "x is greater than y".

The else statement.


Else statement is used to specify a block of code to be executed if the condition is
false.

Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example:
int time = 20;
if (time < 18) {

6
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

In the example above, time (20) is greater than 18, so the condition is false. Because of
this, we move on to the else condition and print to the screen "Good evening". If the
time was less than 18, the program would print "Good day".

Else if statement.
Else if statement is used to specify a new condition if the first condition is false.

Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

Example
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

In the example above, time (22) is greater than 10, so the first condition is false. The
next condition, in the else if statement, is also false, so we move on to the else
condition since condition1 and condition2 is both false - and print to the screen "Good
evening".

However, if the time was 14, our program would print "Good day."

Short hand if else (ternary operator)


There is also a short-hand if else, which is known as the ternary operator because it
consists of three operands.
It can be used to replace multiple lines of code with a single line. It is often used to
replace simple if else statements.

Syntax

7
variable = (condition) ? expressionTrue :
expressionFalse;

Example:
Instead of writing this;

int time = 20;


if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
We can simply write:

int time = 20;


string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;

2. Write a program that displays or outputs numbers between 10 and 20.


#include <iostream>
// C++ program to display numbers between 10 and 20
int main() {
for (int i = 10; i <= 20; ++i) {
// Output the current number
std::cout << i << std::endl;
}

return 0;
}

You might also like