2017 Chapter Three Computer Programming (ECEg 1052)
2017 Chapter Three Computer Programming (ECEg 1052)
1
Email:[email protected]
Computer Programming -ECEG 1052
Introduction
✓ in c++ control structures control the flow of execution of a program.
✓ They allow you to control the order in which statements are executed based on certain conditions or to
repeat a set of statements multiple times.
✓ Control structures are fundamental to implementing logic and decision-making in your programs.
✓ Control structures in C++ can be broadly categorized into three types:
▪ Sequential control structure
▪ Selection control structure
▪ Iteration(reptation) control structure
2
Email:[email protected]
Computer Programming -ECEG 1052
Sequence Structure
Sequential control structures are the most basic form of control flow, where statements are executed in the order
they appear in the program.
3
Email:[email protected]
Computer Programming -ECEG 1052
Selection/Branching/conditional Structure
▪ Selection control structures allow the program to make decisions and execute different
blocks of code based on whether certain conditions are true or false.
▪ There are various ways to do selection in C++.
▪ if statement
▪ if-else statement
▪ if-else-if ladder
▪ nested if statement
▪ switch statements
4
Email:[email protected]
Computer Programming -ECEG 1052
if statement
Example:-Write a c++ code to check if a
▪ The if statement is the most basic form of selection given number is equal to zero.
control. It executes a block of code if a specified
condition is true. #include <iostream>
using namespace std;
Flowchart
int main()
if (condition) { {
// Code to execute if condition is true int num;
} cout<<"please enter any number to compare with
zero";
Example: cin>>num;
int score = 85; if(num==0)
if (score >= 60) { cout<<"the number is equal to zero“<<endl;
cout << "Pass" << endl; return 0;
} }
If score is greater than or equal to 60, it prints "Pass".
5
Email:[email protected]
Computer Programming -ECEG 1052
if …else statement
The if-else statement allows you to execute one block of
code if the condition is true and another block if the
condition is false. Flowchart
Syntax
if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}
6
Email:[email protected]
Computer Programming -ECEG 1052
if …else statement
int score = 55;
if (score >= 60)
{
cout << "Pass" << endl;
}
else
{
cout << "Fail" << endl;
}
Output:
Pass
7
Email:[email protected]
Computer Programming -ECEG 1052
Class Work
• What is the printout of the code in following if number is 30? What if number is 35?
8
Email:[email protected]
Computer Programming -ECEG 1052
Else-if Ladder
The else-if ladder allows you to check multiple conditions in sequence. If the first condition is
false, it checks the next one, and so on.
int score = 75;
if (condition1)
Syntax: if (score >= 90)
{
{
// Code to execute if condition1 is true
cout << "Grade A" << endl;
}
}
else if (condition2)
else if (score >= 80)
{
{
// Code to execute if condition2 is true
cout << "Grade B" << endl;
}
}
else if (condition3)
else if (score >= 70) {
{
cout << "Grade C" << endl;
// Code to execute if condition3 is true
} else {
}
cout << "Grade D" << endl;
else
}
{ // Code to execute if all conditions are false }
9
Email:[email protected]
Computer Programming -ECEG 1052
if(num>0){
cout<<"the number is greater than zero"<<endl;
}
else if(num<0){
cout<<"the number is less than zero"<<endl;
}
else {
cout<<"the number is equal to zero"<<endl;
}
return 0;
}
10
Email:[email protected]
Computer Programming -ECEG 1052
11
Email:[email protected]
Computer Programming -ECEG 1052
#include <iostream>
using namespace std;
int main() {
int age,income;
cout<<"Enter age:";
cin>>age;
cout<<"\nEnter income";
cin>>income;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
if (income > 20000) {
cout << "You are also eligible for a credit card." << endl;
} else {
cout << "You do not meet the income requirement for a credit card." << endl;
}
} else {
cout << "You are not eligible to vote." << endl;
}
return 0;
} 12
Email:[email protected]
Computer Programming -ECEG 1052
switch Statement
The switch statement is used to execute different blocks of code based on the value of a variable.
It is particularly useful when you have multiple conditions based on the same variable.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// You can have as many cases as needed
default:
// Code to execute if expression doesn't match any case
}
13
Email:[email protected]
Computer Programming -ECEG 1052
Example
14
Email:[email protected]
Computer Programming -ECEG 1052
Class work
Write a C++ program that prompts the user to enter two numbers and select an operation
(+, -, *, or /). The program should then perform the chosen operation and display the result.
If division by zero is attempted, display an error message. If an invalid operator is entered,
print "Invalid operation". Use a switch statement to implement the operations and handle
edge cases.
15
Email:[email protected]
Computer Programming -ECEG 1052
Syntax:
int a = 10;
int b = (a >= 10) ? 100 : 200;
cout << "b is: " << b << endl;
16
Email:[email protected]
Computer Programming -ECEG 1052
17
Email:[email protected]
Computer Programming -ECEG 1052
for Loop
Previous statement in the program
The for loop is used when you know the exact number of
iterations you want to perform. It consists of three parts: Initialization
initialization, condition, and increment/decrement.
False
Condition
Syntax:
for (initialization; condition; increment/decrement) { True
// Code to execute in each iteration Body
}
Update
Example Explanation:
Initialization (int i = 0): Sets the starting value of the loop counter i to 0
#include <iostream> Condition (i < 5): Checks if i is less than 5. If true, the loop continues.
using namespace std; Increment (i++): Increases the value of i by 1 after each iteration.
Code Block: Prints the current iteration number.
int main() { output
for (int i = 0; i < 5; i++) {
cout << "Iteration " << i << endl; Iteration 0
} Iteration 1
Iteration 2
return 0; Iteration 3
} Iteration 4
19
Email:[email protected]
Computer Programming -ECEG 1052
Example 2
#include <iostream> Output
using namespace std; 9
8
int main() 7
{ 6
5
for(int i=9;i>=0;i--){
4
cout<<i<<endl; 3
} 2
1
return 0; 0
}
20
Email:[email protected]
Computer Programming -ECEG 1052
21
Email:[email protected]
Computer Programming -ECEG 1052
while Loop
Example
The while loop is used when the number of iterations is
not known and depends on a condition. The loop #include <iostream>
continues as long as the condition is true. using namespace std;
}
23
Email:[email protected]
Computer Programming -ECEG 1052
24
Email:[email protected]
Computer Programming -ECEG 1052
do-while Loop
The do-while loop is similar to the while loop, but the condition is checked after the code block is executed,
ensuring that the code block is executed at least once.
#include <iostream>
Syntax: using namespace std;
do {
// Code to execute at least once, then as long as condition is true int main() {
} while (condition); int i = 0;
do {
cout << "Iteration " << i << endl;
i++;
} while (i < 5);
return 0;
}
25
Email:[email protected]
Computer Programming -ECEG 1052
Example
#include <iostream>
using namespace std; Output
int main() 9
{ 8
int i = 9; 7
do { 6
5
cout<<i<<endl;
4
i--; 3
} 2
while(i>=0); 1
return 0; 0
}
26
Email:[email protected]
Computer Programming -ECEG 1052
Nested Loops
Loops can be nested within each other to handle more complex scenarios, such as iterating over
multi-dimensional arrays or performing operations that require multiple levels of repetition.
Example:
#include <iostream>
using namespace std; (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2)
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "(" << i << "," << j << ") ";
}
cout << endl;
}
return 0;
} 27
Email:[email protected]
Computer Programming -ECEG 1052
break statement
Email:[email protected]
Computer Programming -ECEG 1052
do{
Syntax
Statement/s
If(test expression) while (test expression)
{ {
break; Statement/s
} If(test expression) For (initial expression; test expression;
Statement/s { update expression)
} break;
While(test expression); }
Statement/s {
}
If(test expression)
{
break;
}
Statement/s
}
Email:[email protected]
Computer Programming -ECEG 1052
Write C++ program to add all number entered until the user
enters 0.
Email:[email protected]
Computer Programming -ECEG 1052
Class Work
#include <iostream>
using namespace std;
int main(){
int a = 10; // Local variable declaration
value of a: 10
do { value of a: 11
value of a: 12
cout << "value of a: " << a << endl;
value of a: 13
a = a + 1; value of a: 14
if( a > 15 ) break; value of a: 15
} while( a < 20 );
return 0;
}
Email:[email protected]
Computer Programming -ECEG 1052
continue statement
• continue statement causes the loop to skip the current iteration and goes for
next iteration.
• For the for loop, continue causes the conditional test and increment portions
of the loop to execute.
• For the while and do...while loops, program control passes to the conditional
tests.
• It is an error to use the continue statement outside a loop.
Email:[email protected]
Computer Programming -ECEG 1052
Syntax
Email:[email protected]
Computer Programming -ECEG 1052
Email:[email protected]
Computer Programming -ECEG 1052
return statement
• The return statement enables a function to return a value to its caller. It has the general
form:
• return expression;
• where expression denotes the value returned by the function. The type of this value should
match the return type of the function.
• For a function whose return type is void, expression should be empty:
• return;
Email:[email protected]
Computer Programming -ECEG 1052
Cont….
▪ The only function we have discussed so far is main, whose return type is
always int.
▪ The return value of main is what the program returns to the operating system
when it completes its execution.
▪ For example, its conventional to return 0 from main when the program
executes without errors. Otherwise, a non-zero error code is returned.
Email:[email protected]
Computer Programming -ECEG 1052
Email:[email protected]
Computer Programming -ECEG 1052
• #include <iostream>
• using namespace std;
• int main() {
• int rows;
cout << "Enter number of rows: ";
• cin >> rows;
for(int i = 1; i <= rows; ++i) {
• for(int j = 1; j <= i; ++j) {
• cout << "* "; }
• cout << "\n"; }
• return 0; }
Email:[email protected]
Computer Programming -ECEG 1052
Email:[email protected]
Computer Programming -ECEG 1052
goto
• The code goto is used for moving back and forth in the program. Therefore, for using goto
statement one needs to put in a label.
• Syntax:
Email:[email protected]
Computer Programming -ECEG 1052
Write program calculates the average of numbers entered by user. If user enters negative number, it ignores the
number and calculates the average of number entered before it
Email:[email protected]