0% found this document useful (0 votes)
4 views54 pages

Chapter Four: Statements

Chapter Four discusses various types of statements in C++, including simple, compound, selection, iteration, and jump statements. It explains how these statements are the building blocks of C++ programs, detailing their syntax and usage through examples. The chapter also covers control flow mechanisms like if-else statements, switch statements, and loops such as while, do-while, and for loops.

Uploaded by

tadasatokuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views54 pages

Chapter Four: Statements

Chapter Four discusses various types of statements in C++, including simple, compound, selection, iteration, and jump statements. It explains how these statements are the building blocks of C++ programs, detailing their syntax and usage through examples. The chapter also covers control flow mechanisms like if-else statements, switch statements, and loops such as while, do-while, and for loops.

Uploaded by

tadasatokuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter Four

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:

1. They allow us to put multiple statements in places.

2. They allow us to introduce a new scope (part of the program

text within which a variable remains defined in the program) in

the program. For example, the scope of min, i, and j in the

above example is from where they are defined till the closing

brace of the compound statement. Outside the compound

statement, these variables are not defined

 It is also called a block.

 A block does not need a semi colon.

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)

will be executed if and only if a certain


Boolean statement is true.
The Boolean expression part of a statement

should evaluate to a Boolean value. That


means that the execution of the condition
should either result to a value of true or a
false.
8 03/06/2025
Flow diagram of If statement

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

Used when we want to execute a certain


statement if a condition is true, and a
different statement if the condition is false.
If-else syntax
if(boolean_expression){
statement 1;
statement 2;
}
else {
Statement 3;
Statement 4;
}
Example
int grade=80;
if(grade>80){
cout<<“A”
cout<<“Congratulation!”;
}
else{
cout<<“sorry you failed”;
}
Example

16 03/06/2025
If....else….if statement

17 03/06/2025
Example

if ( grade >= 90 ) { // 90 and


above
cout << "A"};
else if ( grade >= 80 ){ // 80-89
cout << "B";}
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
18 03/06/2025
Nested if else
you can use one if or else if statement inside
another if or else if statement(s).
 One inside another, test for multiple cases
 Once condition meet, other statements skipped

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

The do statement (also called do loop) is


similar to the while statement, except that
its body is executed first and then the loop
condition is examined.
The general form of the do statement is:
do
statement;
while (expression);

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

The “for” statement (also called loop) is


used to repeatedly execute a block of
instructions until a specific condition fails.
A for loop is a repetition control structure
that allows you to efficiently write a loop
that needs to execute a specific number of
times.
The General Syntax is:
for(expression1 ; expression2 ;
expression3)
statements;
41 03/06/2025
Cont…
The for loop has three expressions:
 expression1: is statement that will be
executed only once and before the looping
starts.
 Expression2: is the part that decides
whether to proceed with executing the
instructions in the loop or to stop.
Expression2 will be evaluated each time
before the loop continues.

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

It is sometimes necessary to skip some


statement/s inside the loop.
In that case, continue; statement is used in
C++ programming.
The continue statement works somewhat
like the break statement.
 Instead of forcing termination, however,
continue forces the next iteration of the
loop to take place, skipping any code in
between.

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

You might also like