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

CSC 202 Session 2

The document is a lecture note for a Computer Programming II course focusing on loops and decision-making structures in C++. It explains the concept of loops, including for loops and while loops, and provides programming tasks to practice these concepts. Additionally, it includes reading assignments and study questions to enhance understanding of control structures in programming.

Uploaded by

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

CSC 202 Session 2

The document is a lecture note for a Computer Programming II course focusing on loops and decision-making structures in C++. It explains the concept of loops, including for loops and while loops, and provides programming tasks to practice these concepts. Additionally, it includes reading assignments and study questions to enhance understanding of control structures in programming.

Uploaded by

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

Ladoke Akintola University of Technology

Open and Distance Learning Centre


Computer Science Department
CSC 202- Computer Programming II
(Object Oriented Programming using C++)

SECOND LECTURE NOTE


LOOPS AND DECISION-MAKING STRUCTURES IN C ++
1. INTRODUCTION
Not all programs will be executed in a sequential manner. Sometimes there may be a need
to transfer the flow of control from one part of the program to another. This can be
achieved with the help of control structures. There are two major categories: loops and
decisions.
❖ LOOPS: This allows you to repeat a segment of code in your program in a specified
number of times. This iteration will continue until the set condition is false.
❖ For every iterative control structures, four things must be put in place:
• A variable must be used to keep track of the iteration e.g. variable “count”
• The variable must have an initial value. e.g. 0 or 1
• The variable must be incremented after each iteration e.g. count++;
• There must be a way to determine if the iteration has been completed.
❖ Loops in C++ could be For loop, while loop, and while do loop.
❖ For Loop Syntax: it’s the simplest as it captures all the four conditions on a single line
For (initialization; condition; increment)
{
Statements
}
as in

1|Page
For (count=1; count<= num; count++)
{
Statements
}
❖ While Loop: in a while loop, the four conditions (variable declaration, initialization,
increment are scattered throughout the code. Its Syntax is:
While (condition)
{
▪ Statements
▪ Increment statement
}
❖ The while loop evaluates the condition.
❖ If the condition is true, statements inside the body of while loop is
evaluated.
❖ Increment the control variable and test the condition again. This process
goes on until the condition is false.
❖ When the condition is false, while loop is terminated

A palindrome is a word that reads the same backwards as forwards

2|Page
Programming Task
• Write and run a program that uses a for loop to compute and print the
sum of the squares between 1 and a given number (1 and the given number
inclusive)
o Display the squares of each number before you display the sum in this
format:
o 1: The squares of 1 is 1
o 2: The squares of 2 is 4
o 3: The squares of 3 is 9
o The sum of the squares is 14
• Repeat the same for a While … Loop
• This code employs if control structure to generate the factorial of a
number. Use while … do control structure instead.
#include <iostream>
using namespace std;
int main()
{
int n;
Long int factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
For ( int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
• Upload your solutions to the cms platform before 1:00 pm, 1st Feb., 2018.

3|Page
4|Page
READING ASSIGNMENT II
A. Textbooks to Consult:
i. Chapter Two of Object-Oriented Programming in C++
ii. Chapter Two of Shaum’s Outline Series
B. Instructions:
i. You are expected to answer all these in your own words.
ii. You are strongly advised against copying words for words from the textbook.
iii. Discuss among yourselves but don’t be tempted to allow your friend to copy your
solutions
C. Study Questions
i. Why do you need to employ control structures in your programs?
ii. Explain the use of “set width” command of the “iomanip” header file
iii. What is (are) the advantages/disadvantages of defining variables inside a For Loop
declaration. Is it a good programming practice?. Give your reasons.
iv. Is it possible to have more than one expression in the initialization part of the for
statement? If yes, discuss with an example.
v. Is it possible to have more than one conditional/test expression in a For loop. If yes,
discuss with an example.
vi. Which loop is perfect for instances where you know the number of iterations in
advance. Discuss with examples
vii. Write a program to print the powers of 100 from 1 to 1000000000000000000 on
separate lines as shown below:
1
100
10000
1000000
100000000
10000000000

5|Page
1000000000000
100000000000000
10000000000000000
1000000000000000000
(note that the powers of 100 may be outside the int type)
viii. Implement the code in listing 6.8. what do you observe? Do you get the expected
results? What could be responsible and what can you do to fix that?

ix. Study and modify the code in listing 7.5 to generate an addition table. Use while
do control structure

References
1. Robert Lafore (2002), “Object-Oriented Programming in C++”, Sams Publishing
Fourth Edition
2. John R. Hubbard (2000), “Schaum’s Outline of Theory and Problems of
Programming with C++”, McGraw-Hill Publishers, Second Edition.

SOLUTION
#include <iostream>
using namespace std;
int main()

int FirstNum, SecNum, m = 60, n = 7;

cout << "Supply your first number " << endl;

6|Page
cin >> FirstNum;

cout << "Supply your second number " << endl;

cin >> SecNum;

cout << "Their sum is " << (FirstNum + SecNum) << endl;

cout << "Their difference is " << (FirstNum - SecNum) << endl;

cout << "Their product is " << (FirstNum * SecNum) << endl;

cout << "Their quotient is " << (FirstNum / SecNum) << endl;

cout << "Their remainder is " << (FirstNum % SecNum) << endl;

7|Page

You might also like