Chapter 3-Computer Programming-Control Statements
Chapter 3-Computer Programming-Control Statements
Unit-3
Control
Statements
Admas Abtew
Faculty of Computing and
Informatics
Jimma Technology Institute, Jimma
[email protected]
+251912499102
Looping
Outline
Control flow
The 3 Basic Structures in
Programming Languages
Sequence
Selection/conditional
Loop
1.Control of Flow
True
Condition? Statement
False
Statement
True
Condition?
False
if (Boolean_expression)
{
statement(s);
}
It executes an action if and only if the condition is true.
statement(s);
}
else
{
statement(s);
}
}
return 0;
}
CP Unit 3-Control Statements 16
1.3 Nested if Statement
An if statement can be inside another if statement to
form a nested if statement.
Its syntax is:
// outer if statement
if (condition1)
{
// statements
// inner if statement
if (condition2)
{
// statements
}
}
switch (condition)
The switch {
case value1:
Statement statement11;
...
Multiple selection break;
structure case value2:
statement21;
Performs different actions ...
for different test break;
expression values
...
Less general than nested
if-else statement case valueN:
integer test expression statementN1;
...
only break;
test for equality only default:
statementD;
...
}
switch (expression)
{ The switch Statement
case value1:
statement11;
(cont’d)
... Uses 4 keywords
break;
case value2: switch
statement21; identifies
... start of switch
break; statement
expression in parentheses
...
(switch expression) evaluated
case valueN: & its value compared to
statementN1; various alternative (case)
...
break; values within compound
default: statement that follows
statementD;
...
}
if (x > 0) y=x>0?
1 : -1; You might want to assign a variable
y = 1; a value that is restricted by certain
else conditions.
y = -1; E.g. int max = (a>b) ? a : b;
4. Write a program that accepts three numbers from the user and prints "increasing" if
the numbers are in increasing order, "decreasing" if the numbers are in decreasing
order and "Neither increasing nor decreasing order" otherwise.
5. Write a program to calculate sum, average and check your grade status, if pass or
fail.
Hint: accept at least three course marks then calculate the total, and average of your
mark, the status will be based on average value. Display the total mark, average and
status.
2. while statements,
3. do … while statements,
#include <iostream.h>
int main()
{
int counter;
for (counter = 1; counter <= 10; counter++)
cout << counter << endl;
return(0);
}
#include<iostream>
using namespace std;
int main(){
for (int i=1;i<=100;i++){
if(i %2==0){
cout<<i <<endl;
}
}
}
counter = 0;
while (counter < 5)
{
cout << "\nI love ice cream!";
counter++;
}
{
{
(single statement or
body
compound statement) statement(s);
}
while (condition);
while
keyword condition
(enclosed in must
parenthesis) Have ;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
return 0;
}
• continue statement causes the loop to skip the rest of its body and
immediately retest its condition prior to reiterating.
• 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.