KEMBAR78
Loops and Conditional Statements | PDF | Computer Engineering | Programming Paradigms
0% found this document useful (0 votes)
100 views4 pages

Loops and Conditional Statements

This document contains an assignment for a computer programming course at JKUAT. It includes code samples for displaying numbers between 10-20 using a for loop and explanations of common loop types (for, while) and conditional statements (if, if-else) in object-oriented programming. Key benefits of using loops and conditionals in OOP are listed such as automation, flexibility, and code reusability.

Uploaded by

retsen30002
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)
100 views4 pages

Loops and Conditional Statements

This document contains an assignment for a computer programming course at JKUAT. It includes code samples for displaying numbers between 10-20 using a for loop and explanations of common loop types (for, while) and conditional statements (if, if-else) in object-oriented programming. Key benefits of using loops and conditionals in OOP are listed such as automation, flexibility, and code reusability.

Uploaded by

retsen30002
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/ 4

INSTITUTION: JKUAT

UNIT: COMPUTER PROGRAMING.

UNIT CODE: SMA 2175

COURSE: ELECRICAL AND ELECTRONIC ENGINEERING.

ASSIGNMENT 1

GROUP MEMBERS

1.PAUL MWENDWA ENE211-0142/2022

2.NESTER OKOTH ENE211-0035/2022

3.JOEL MUKIU ENE211-0026/2022

4 BALDWINE KWENDO ENE211-0248/2022

5.IAN OTIENO ENE211-0055/2022


1. Code that displays numbers between 10 and 20

#include<iostream>
using namespace std;
int main(){
for(int i=11;i<20;i++){
cout<<i<<endl;
}
return(0);
}

2. Loops

Loops enable you to execute a block of code repeatedly, often iterating through elements
within objects or processing data associated with them. Here's a breakdown of common loop
types in OOP:

 For loops:
These loops iterate over a collection of items within an object's attributes, typically an array or
list. They are efficient for processing each item in a sequential manner. Here's a simplified
example :
for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
}

 While loops:
While loops execute a code block as long as a certain condition remains true. They offer more
flexibility for iterating until a specific criteria is met within an object's state. Here's an example:
while (condition) {
// Code to be executed in each iteration
}
3. Conditional Statements

Conditional statements, like if-else structures, allow you to control the flow of your program
based on the state of objects or their attributes. Here are common types:

 If statements:
These check a condition and execute a code block only if the condition is true. They are
essential for making decisions based on object properties. Here's an example:
if (condition) {
// run this code if condition is true
}

 If_else statements:
These allow for checking multiple conditions sequentially. You can chain multiple else if
statements to handle different scenarios based on an object's state. Example:
if (condition) {
// run this code if condition is true
} else {
// if the condition above is false, run this code
}

Key Points to Consider:


 Loops and conditionals often work together. You might use a loop to iterate through an
object's collection and use conditional statements within the loop to make decisions
based on each item's properties.
 Object-oriented programming encourages modularity. Loops and conditionals can be
encapsulated within object methods, promoting code reusability and making your
program more organized.
 When using loops, ensure there's a clear condition for termination to avoid infinite
loops.
Benefits of Loops and Conditionals in OOP
 Automation: Loops automate repetitive tasks on objects, improving efficiency.
 Flexibility: Conditionals allow for dynamic behavior based on an object's state, making
programs more adaptable.
 Code Reusability: Well-structured loops and conditionals within object methods can be
reused across different objects, reducing code redundancy.

You might also like