Chapter Four: Statements
Chapter Four: Statements
Statements
1 03/06/2025
Statements
Statements are fragments of the C++ program that are
executed in sequence.
Statements represent the lowest-level building blocks of
a program.
Each statement represents a computational step which
has a certain side-effect.
The body of any function is a sequence of statements.
For example:
int main() {
int n = 1; // declaration statement
n = n + 1; // expression statement
cout<< "n = " << n << '\n'; // expression statement
return 0; // return statement }
2 03/06/2025
Cont…
C++ categorize statements into these
groups:
Simple
Compound
Selection
Iteration
Jump
3 03/06/2025
Simple statements
A simple statement is a computation
terminated by a semicolon.
Variable definitions and semicolon-
terminated expressions are representatives
of this category.
int i; //declaration statement
x++; // Increment statement
4 03/06/2025
Compound statements
Also called a block
Set of statements within a pair of braces
Used to include multiple statements in body
For example:
{
int min, i = 10, j = 20;
min = (i < j ? i : j);
cout << min << '\n';
}
5 03/06/2025
Cont…
Compound statements are useful in two ways:
above example is from where they are defined till the closing
6 03/06/2025
Selection statements
Selection statements are used for specifying
alternate paths of execution, depending on
the outcome of a logical condition.
C++ supports two main types of selection
statements: if and switch. In addition, the ?
operator is an alternative to if in certain
circumstances.
1. if statement
2. If…else statement
3. Nested if statement
4. Switch statement
7 03/06/2025
If statement
specifies that a statement (or block of code)
9 03/06/2025
Syntax of if statement
if (expression)
statement;
First expression is evaluated, if the outcome is nonzero
(true) then statement is executed. Otherwise, nothing
happens
if(boolean_expression or condition)
statement 1;
Or
If(boolean_expression)
{
Statement 1;
Statement 2;
}
10 03/06/2025
Example
int grade=80;
if(grade>80)
cout<<“A”;
Or
int grade=80;
if(grade>80){
cout<<“A”
Cout<<“Congratulation”;
}
Exercise if statement
Write a C++ program to check whether a
given number is even or odd.
Write a C++ program to check whether a
given number is positive, negative or zero.
Write C++ code to find out the smallest
number between three numbers.
Write C++ code to find out the largest
number between three numbers.
12 03/06/2025
if-else statement
16 03/06/2025
If....else….if statement
17 03/06/2025
Example
19 03/06/2025
Example
20 03/06/2025
A switch statement
A switch statement allows a variable to be
tested for equality against a list of values.
Each value is called a case, and the
variable being switched on is checked for
each case.
Select one from the listed cases.
21 03/06/2025
Syntax:
22 03/06/2025
Switch…
23 03/06/2025
General form
Flow Diagram:
24 03/06/2025
25 03/06/2025
Default block
26 03/06/2025
Example
#include <iostream>
using namespace std;
int main () {
char grade ;// local variable declaration:
Cin>>grade;
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
cout << “Very Good!" << endl;
break;
27 03/06/2025
Cont…
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "try again" << endl;
break;
default :
cout << "Invalid grade" << endl; }
cout << "Your grade is " << grade << endl; return 0; }
28 03/06/2025
Repetition structure
A loop is a way of repeating a series of
instructions several times.
The loop is set up either to repeat a certain
number of times or to go round and round
until some condition is met.
There are three repetition structures in C+
+,
while loop
do-while loop.
for loop
29 03/06/2025
While loop
The while statement is used when the program needs to
perform repetitive tasks.
A while loop statement repeatedly executes a target
statement as long as a given condition is true.
while ( condition )
{
statement(s)
………………..
}
The braces are not required if the loop body contains only
a single statement
30 03/06/2025
Cont…
The program will repeatedly execute the
statement inside the while until the
condition becomes false (0).
If the condition is initially false, the
statement will not be executed.
It is not unusual for a while loop to have an
empty body (i.e., a null statement).
31 03/06/2025
Example
32 03/06/2025
Example
33 03/06/2025
Example
34 03/06/2025
Cont…
Always place “braces” around the body of
a while loop.
Advantages:
Easier to read
Will not forget to add the braces if you go
back and add a second statement to the loop
body
Less likely to make a semantic error
Indent the body of a while loop 3 to 5
spaces -- be consistent!
35 03/06/2025
The ‘do…while’ Statement
36 03/06/2025
Unlike for and while loops, which test the
loop condition at the top of the loop,
the do...while loop checks its condition at
the bottom of the loop.
A do...while loop is similar to a while loop,
except that a do...while loop is guaranteed
to execute at least one time.
37 03/06/2025
Cont…
First statement is executed and then
expression is evaluated.
If the outcome of the latter is nonzero
then the whole process is repeated.
Otherwise, the loop is terminated.
The do loop is less frequently used than the
while loop.
It is useful for situations where we need the
loop body to be executed at least once,
regardless of the loop condition.
38 03/06/2025
39 03/06/2025
Example
do {
int n=17;
cout << “Hello World ”<< endl;
}
while (n<=10);
40 03/06/2025
The ‘for’ Statement
42 03/06/2025
syntax
Expression3: is statement that will be
executed after each iteration.
So for loop can be represented as:
for(initialization ; condition ;
increase/decrease)
statement;
43 03/06/2025
Cont…
E.g. guess the output of the following code:
44 03/06/2025
Example
45 03/06/2025
46 03/06/2025
47 03/06/2025
48 03/06/2025
Jump statement
goto statements
In C++ programming, goto statement is
used for altering the normal sequence of
program execution by transferring control
to some other part of the program.
In modern programming, goto statement is
considered a harmful construct and a bad
programming practice.
The goto statement can be replaced in
most of C++ program with the use of
break and continue statements.
49 03/06/2025
break statement
The break statement has two uses.
You can use it to terminate a case in the
switch
You can also use it to force immediate
termination of a loop, bypassing the normal
loop conditional test.
If you are using nested loops (i.e., one loop
inside another loop), the break statement
will stop the execution of the innermost
loop and start executing the next line of
code after the block.
50 03/06/2025
Example
#.....
Using……
Int main(){
int t;
for(t=0; t<10; t++) {
Output
cout<<t<< endl; 012345
if(t==5) break;
}
return 0;
}
51 03/06/2025
C++ continue Statement
52 03/06/2025
Example
#.....
Using……
Int main(){
int t;
for(t=0; t<10; t++) {
Output
if(t==2) continue; 01 345
cout<<t<< endl ;
}
return 0;
}
53 03/06/2025
End!
54 03/06/2025