0% found this document useful (0 votes)
34 views25 pages

Chapter 3 - Control Structures in C++

C++

Uploaded by

Shafi Esa
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)
34 views25 pages

Chapter 3 - Control Structures in C++

C++

Uploaded by

Shafi Esa
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/ 25

Exit Exam Tutorial

Part 1: Computer Programming


Episode 3: Control Statements in C++
1.3 Control Statements in C++
- A running program spends all of its time executing
instructions or statements in that program.
- The order in which statements in a program are executed is
called flow of that program.
- Programmers can control which instruction to be executed
in a program, which is called flow control.
- Statement in a program execute one after the other in the
order in which they are written, this is called sequential
execution.
1.3 Control Statements in C++
Selection statements Iteration statements
- If (single selection - While (Entry-controlled
structure) loop)
- If/else (double selection - Do-While (Exit-controlled
structure) loop)
- Switch (multiple selection - For
structure)
1.3 Control Statements in C++
1.3.1 If statements
- The “If” statement allows conditional execution.
Syntax: if (condition) {
// statement
}
1.3 Control Statements in C++
1.3.1.1 If/else statements
- The “If…else” statement causes one of two alternative
statements to execute whether the condition is true.
Syntax: if (condition) {
// statement 1
}
else {
// statement 2
}
1.3 Control Statements in C++
#include <iostream>
using namespace std;
int main() {
int weight = 20;

if (weight > 18) {


cout << “You are over weight”;
}
else {
cout << “You are under weight”;
}
return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
#include <iostream>
using namespace std;
int main() {
int score;

cout << “Enter your score “ << endl;


cin >> score;

if (score > 100) {


cout << “Your score is out of range”;
}
else if (score >= 90) {
cout << “Your grade is A”;
}
else if (score >= 80) {
cout << “Your grade is B”;
}
else if (score >= 70) {
cout << “Your grade is C”;
}
1.3 Control Statements in C++
else if (score >= 60) {
cout << “Your grade is D”;
}
else if (score >= 0) {
cout << “Your grade is F”;
}
else {
cout << “Your score is out of range”;
}

return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
1.3.2 The SWITCH statement
- The switch statement can be used instead of the else if
construct to implement a sequence of parallel alternatives.
- Syntax: switch (expression) {
case (constant1):
statementList1;
break;
case (constant2):
statementList2;
break;

default:
noStatementList0;
}
1.3 Control Statements in C++
#include <iostream>
using namespace std;
int main() {
int score;

cout << “Enter your score “ << endl;


cin >> score;

switch (score/10) {
case 10:
case 9:
cout << “Your grade is A “ << endl;
break;
case 8:
cout << “Your grade is B “ << endl;
break;
case 7:
cout << “Your grade is C “ << endl;
break;
1.3 Control Statements in C++
case 6:
cout << “Your grade is D “ << endl;
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
cout << “Your grade is F “ << endl;
break;

default:
cout << “Error! The Score is invalid or out of range\n “;
}
cout << “GOODBYE” << endl;
return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
1.3.3 Repetition/Iteration (loop) statement
- Repetition statements control a block of code to be
executed repeatedly for a fixed number of times or until a
certain condition fails.
- There are three C++ repetition statements:
1) The for loop
2) The while-loop
3) The do … while loop
1.3 Control Statements in C++
1.3.3.1 The for loop
- For (initialization; condition; increase) statement; its main function is
to repeat statement while condition remains true, like the while loop.
- But in addition, for provides places to specify an initialization
instruction and an increase instruction.
- So, this loop is specially designed to perform a repetitive action with a
counter.
It works in the following way:
1. Initialization is executed. Generally, it is an initial value setting for a
counter variable. This is executed only once.
2. Condition is checked, if it is true the loop continues, otherwise the
loop finishes and statement is skipped.
3. Statement is executed. As usual, it can be either a single instruction
or a block of instructions enclosed within curly brackets {}.
4. Finally, whatever is specified in the increase field is executed and
the loop gets back to step 2.
1.3 Control Statements in C++
#include <iostream>
using namespace std;
int main() {
int n;

cout << “Enter a positive integer: “ << endl;


cin >> n;
long sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << “The sum of the first “ << n << “
integers is “ << sum << endl;
return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
1.3.3.2 The while loop
#include <iostream>
using namespace std;
int main() {
int n, i = 1;
cout << “Enter a positive integer “ << endl;
cin >> n;
long sum = 0;
while (i <= n) {
sum += i++;
}
cout << “The sum of the first “ << n << “
integers is “ << sum;
return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
1.3.3.3 The do-while loop
- Its functionality is exactly the same as the while loop except that
condition in the do-while is evaluated after the execution of
statement instead of before, granting at least one execution of
statement even if condition is never fulfilled.
1.3 Control Statements in C++
#include <iostream>
using namespace std;
int main() {
int n, i = 0;

cout << “Enter a positive integer: “ << endl;


cin >> n;
long sum = 0;
do {
sum += i++;
} while (i <= n);
cout << “The sum of the first “ << n << “ integers
is “ << sum << endl;
return 0;
}
1.3 Control Statements in C++
Sample output:
1.3 Control Statements in C++
Special Thanks to the publisher and author with:
TOPICS AND THE CONCEPTS:
Control Structures
Selection Statements
If Statements
If … else Statements
Switch Statements
Repetition Statements
For Loop
While Loop
Do-While Loop

REFERENCES:
C++ Programming: From Problem Analysis to Program Design (D.S. Malik)
Fundamental of Programming in C++ (Walter J. Savitch)

PRESENTED BY:
Mohammed Nebil

HISTORY OF THE PROGRAMMING:


Dennis Ritchie
Bjarne Stroustrup
Anders Hejlsberg

SPECIAL THANKS:
Digital Library of Educations
Federal Democratic Republic of Ethiopia, Ministry of Educations
Ethiopian Education Short Note

You might also like