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

CS-114 Fundamentals of Computer Programming: Control Statements - IV

This document provides a summary of control statements in C++, specifically the "for" loop. It discusses the basic structure of a for loop including initialization, test condition, and update expressions. It provides examples of using for loops to iterate through numbers, print outputs, and nest loops. The break and continue statements are also introduced as ways to alter normal loop flow.

Uploaded by

Abdullah Riasat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

CS-114 Fundamentals of Computer Programming: Control Statements - IV

This document provides a summary of control statements in C++, specifically the "for" loop. It discusses the basic structure of a for loop including initialization, test condition, and update expressions. It provides examples of using for loops to iterate through numbers, print outputs, and nest loops. The break and continue statements are also introduced as ways to alter normal loop flow.

Uploaded by

Abdullah Riasat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

CS-114

Fundamentals of Computer
Programming

Lecture 09
Control Statements – IV

Course Instructor: NUST Institute of Civil


Aisha Shabbir Institute of Geographical Information Systems
Engineering (NICE)
Road Map for Today
• Control Statements
 Selection
• if
• switch
 Iteration
• while
• do…while
• for
 Jump
“for” loop

A for loop is also a “pre test” loop - the condition


is tested before the loop body is executed

Executes a specified block of code a specified


number of times, and keeps track of the value of
the variable
“for” loop
Note absence
of semi-colon

for (initialization; test; update)


{
statement(s);
}

Semi-colons not to denote end of


executable statements But to
separate three parts inside for ( ….. )
Lets discuss each part separately
“for” loop – initialization
• Initialization expression Lets say we have a
• Control variable
• Declaration & definition variable counter
of the control variable
int counter = 1;

for (initialization;
(int counter = 1;test;
test;update)
update)
{
statement(s);
}
“for” loop – loop condition
Lets say we want to
execute the loop body 3 • Loop continuation condition
• Number of times the loop
times:
body should execute
counter <= 3;

for (int
for (int counter
counter = 1; =counter
1; test; update)
<= 3;update)
{
statement(s);
}
“for” loop – update
• Instructions to execute at
end of every iteration
++counter; • Increment/decrement of
the control variable

for IMPORTANT!!
for (int
(int counter
counter= =1;1;counter
counter
<= <= 3; update)
3;++counter)
{
The update is performed AFTER
statement(s);
} the body of the loop
“for” loop - Example
#include <iostream> Output
using namespace std;
1
int main () 1 2 4
2
{ 3
for (int counter = 1; counter <= 3; ++counter)
{
cout << counter << endl; 3
}

return 0;
}
“for” loop - Example

A program that displays the numbers 1


through 10 and their squares.
“for” loop - Example
#include <iostream>
using namespace std;

int main()
{
cout << "Number\t\tNumber Squared\n";
cout << "-------------------------\n";

for (int num = 1; num <= 10; num++)


{
cout << num << "\t\t" << (num * num) << endl;
}

return 0;
}
“for” loop - Example
#include <iostream> Output
using namespace std;
Number Number Squared
int main() -------------------------------------------
1 1
{ 2 4
cout << "Number\t\tNumber Squared\n"; 3 9
1 2 4
cout << "-------------------------\n"; 4 16
5 25
for (int num = 1; num <= 5; num++)
{
cout << num << "\t\t" << (num * num) 3
<< endl;
}

return 0;
}
“for” loop - Examples
Vary the control variable from 100 down to 1 in
decrements of 1.
for ( int i = 100; i >= 1; --i )

Vary the control variable from 7 to 77 in steps of 7.


for ( int i = 7; i <= 77; i += 7 )

Vary the control variable from 20 down to 2 in steps


of -2.
for ( int i = 20; i >= 2; i -= 2 )
“for” loop - Variants

for (int num = 1; // initialization


num <= 5; // loop continuation condition
num++)
{
// loop body
}
“for” loop - Variants
int num;
for (num = 1; num <= 5; num++)
{
// loop body
}

int num = 1;
for (; num <= 5; num++)
{
// loop body
}
“for” loop - Variants

int num = 1;
for (; num <= 5;)
{
// loop body The update is performed
AFTER
num++;
the body of the loop
}
“for” loop - Variants

int num = 1; Is this Correct or


for (;;) Incorrect?
{
// loop body
num++;
}

This is syntax wise correct but without any


condition it becomes an INFINITE loop
“for” loop - Variants

for (int num = 1; num <= 5; num++);

A semicolon here
represents that the body
of for loop is empty and is
a logical error
“for” loop - Variants

int i, j;
for (i = 5,j = 10;i+j<20;i++,j++)
{
cout << "i + j = " << (i + j)
<< "\n";
}
Using multiple variables in
a for loop is allowed
Nested “for” loop
for(int row = 0; row < 5; row++)
{ //begin outer loop
for(int col = 0; col < 20; col++)
{ //begin inner loop
// some statements
} //end inner loop
} //end outer loop
Deciding Which Loop to Use
The while Loop The do-while Loop
• A pre-test loop. • A post-test loop.
• Use when you do not want the loop • Use if you always want the loop to
to iterate if the condition is false from iterate at least once.
the beginning.

The for Loop


• A pre-test loop.
• Automatically executes an update expression at the end of
each iteration.
• Ideal for situations where a counter variable is needed.
• Used when the exact number of required iterations is
known.
Exercise

Write a program which prints ten asterisks (*) in


line (**********) first using while loop and
then using a for loop.
Exercise

Write a program which repeatedly prints the


value of the variable x, decreasing it by 1 each
time, as long as xValue remains positive.
Exercise

Write a program which repeatedly prints the


value of the variable x, decreasing it by 0.5 each
time, as long as xValue remains positive.
Jump Statements
(break & continue)
break & continue

In C++, there are two statements


break; and continue;
to alter the normal flow of a program.
When to use break & continue?

• Sometimes, it is desirable
– to skip the execution of a loop for a certain test
condition or
– terminate it immediately without checking the
condition.
break Statement

The break statement


terminates a loop (for, while
and do…while loop) and
a switch statement immediately
when it appears.
break Statement - syntax

break;
How break statement works?
1 2

3
break Statement – Example

C++ program to add all numbers entered


by user
until user enters 0.
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;
}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
break Statement – Example
#include <iostream>
using namespace std;

int main() {
float number, sum = 0.0;

// test expression is always true


while (true) {
cout << "Enter a number: ";
cin >> number;

if (number != 0.0) {
sum += number;
counter=sum/ number;

}
else {
// terminates the loop if number equals 0.0
break;
}
}
cout << "Sum = " << sum;
return 0;
}
continue Statement

It is sometimes necessary to skip a certain test


condition within a loop.

In such case, continue statement is used


continue Statement - syntax

continue;
How continue statement works?
1 2

3
continue Statement – Example
C++ program to display integer from 1 to 10
except 6 and 9.
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
continue Statement – Example
#include <iostream>
using namespace std;

int main()
{

for (int i = 1; i <= 10; ++i)


{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}

return 0;
}
Project Titles
The application you are going to develop as your
project – give it a name/title

Project Abstract – one page summary of your project


 Introduction/ Background
 The problem that your application addresses –
Motivation
 Your approach to solving that problem/ How your
project is going to solve the problem

Deadline: To be submitted on LMS by 27th Jan, 2022 2355


hrs. Only the Team Lead should upload the abstract on LMS.

You might also like