0% found this document useful (0 votes)
43 views21 pages

csc1023 Chapter5

The document discusses various control structures in C++ including boolean expressions, relational operators, logical operators, if/else statements, else if statements, switch statements, while loops, do-while loops, and for loops. It provides examples of how to use each control structure and explains their functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views21 pages

csc1023 Chapter5

The document discusses various control structures in C++ including boolean expressions, relational operators, logical operators, if/else statements, else if statements, switch statements, while loops, do-while loops, and for loops. It provides examples of how to use each control structure and explains their functionality.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

CHAPTER 5:

Control Statements
Boolean expressions and
relational operators
 In C++ the testing of conditions is done with the use of Boolean expressions which
yield bool values that are either true or false. The simplest and most common way to
construct such an expression is to use the so-called relational operators.

x==y true if x is equal to y


x!=y true if x is not equal to y
x>y true if x is greater than y
x<=y true if x is greater than or equal to y
x<y true if x is less then y
x<=y true if x is less than or equal to y
Combined boolean expressions
using logical operators
 If you need to test more than one relational expression at a
time, it is possible to combine the relational expressions
using the logical operators.

Operator C++ Symbol Example

AND && expression1 &&


expression2
OR || expression1 ||
expression2
NOT ! !expression
The IF selection control
statement
 The simplest and most common selection structure is the if
statement which is written in a statement of the form:

if ( boolean-expression )
statement;
The IF selection control
statement
 The if statement tests for a particular condition (expressed
as a boolean expression) and only executes the following
statement(s) if the condition is true. An example follows of
a fragment of a program which tests if the denominator is
not zero before attempting to calculate fraction.

if(total != 0)
fraction = counter/total;
 If the value of total is 0, the boolean expression above is
false and the statement assigning the value of fraction is
ignored.
The IF selection control
statement
 If a sequence of statements is to be executed, this can be
done by making a compound statement or block by
enclosing the group of statements in braces.
   if( boolean-expression )
   {
        statements;
   }
 An example of this is:
if(total != 0)
{
fraction = counter/total;
cout << "Proportion = " << fraction << endl;
}
The IF/ELSE selection control
statement
 Often it is desirable for a program to take one branch if the
condition is true and another if it is false.

if( boolean-expression )
        statement-1;
   else
        statement-2;
The IF/ELSE selection control
statement
 This can be done by using an if/else selection statement.
Again, if a sequence of statements is to be executed, this is
done by making a compound statement by using braces to
enclose the sequence:

if( boolean-expression )
{statements; }
else
{ statements; }
ELSE IF multiple selection
statement
 Occasionally a decision has to be made on the value of a
variable which has more than two possibilities. This can be
done by placing if statements within other if-else
constructions. This is commonly known as nesting and a
different style of indentation is used to make the multiple-
selection functionality much clearer. This is given below:
if( boolean-expression-1 )
statement-1;
else if( boolean-expression-2 )
statement-2;
else
statement-N;
ELSE IF multiple selection
statement
 Example:
#include <iostream.h> else if (age==100)
  {
main(){ cout<< “You are old\n”;
int age; }
  else
cout<< “Please input your {
age: ”; cout<< “You are really
cin>>age; old\n”;
  }
if (age<100){ }
cout<< “ Your are still
young!\n” ;}
Switch(x) statement
 Instead of using multiple if/else statements C++ also
provides a special control structure, switch.
 For a variable x the switch(x) statement tests whether x is
equal to the constant values x1, x2, x3, etc. and take
appropriate action. The default option is the action to be
taken if the variable does not have any of the values listed.
 The break statement causes the program to proceed to the
first statement after the switch structure. Note that the
switch control structure is different to the others in that
braces are not required around multiple statements.
Switch(x) statement
 Example:
   switch( x )
case x3:
   {
          statements3;
      casex1:
          break;
                                   
          statements1;
  default:
          break;
          statements4;
 
          break;
      case x2:
   }
          statements2;
          break;

      
WHILE repetition control
statements
 Repetition control statements allow the programmer to
specify actions which are to be repeated while some
condition is true. In the while repetition control structure:

While ( boolean-expression )
   {                             
       statements;
   }
WHILE repetition control
statements
 The boolean expression (condition) is tested and the statements (or
statement) enclosed by the braces are (is) executed repeatedly while the
condition given by the boolean expression is true. The loop terminates
as soon as the boolean expression is evaluated and tests false.
Execution will then continue on the first line after the closing brace.
 The true represents a boolean expression which could be x == 1 or
while ( x != 7 ) (x does not equal 7). It can be any combination of
boolean statements that are legal. Even, (while x ==5 || v == 7) which
says execute the code while x equals five or while v equals 7. Notice
that a while loop is the same as a for loop without the initialization and
update sections. However, an empty condition is not legal for a while
loop as it is with a for loop.
WHILE repetition control
statements
 Note that if the boolean expression is initially false the statements
(or statement) are not executed. In the following example the
boolean condition becomes false when the first negative number is
input at the keyboard. The sum is then printed.

#include <iostream.h> while (x<10)


{
using namespace std; cout<<x<<endl;
x++;
main() }
{
int x; }
x=0;
The DO..WHILE repetition
control statement
 DO..WHILE loops are useful for things that want
to loop at least once. The structure is:

do {                             
       statements;
   }
The DO..WHILE repetition
control statement
 The condition is tested at the end #include <iostream.h>
of the block instead of the
using namespace std;
beginning, so the block will be
executed at least once. If the main(){
condition is true, it jumps back to int x;
the beginning of the block and
x=0;
execute it again. A do..while
loop is basically a reversed while do {
loop. A while loop says "Loop cout<< “hello
while the condition is true, and there!\n”;
execute this block of code", a
do..while loop says "Execute this } while (x!=0);
block of code, and loop while the return;
condition is true". }
Increment and decrement
operators
 Increasing and decreasing the value of an integer variable is a
commonly used method for counting the number of times a loop is
executed. C++ provides a special operator ++ to increase the value of a
variable by 1. The following are equivalent ways of incrementing a
counter variable by 1.
count = count + 1;
count++; 

 The operator -- decreases the value of a variable by 1. The following


are both decrementing the counter variable by 1.
count = count - 1;
count--;
The FOR repetition control
statement @ LOOP
 Loops are used to repeat a block of code. Being able to
have your program repeatedly execute a block of code is
one of the most basic but useful tasks in programming.
 The syntax for a for loop is:

for (variable initialization; condition; variable update)


{
Execution is the condition is true
}
The FOR repetition control
statement @ LOOP
 The variable initialization allows you to either declare a variable and
give it a value or give a value to an already existing variable.
 Second, the condition tells the program that while the conditional
expression is true the loop should continue to repeat itself. The variable
update section is the easiest way for a for loop to handle changing of the
variable. It is possible to do things like x++, x = x + 10, or even x =
random ( ) or you could call other functions that do nothing to the
variable but still have a useful effect on the code.
 Notice that a semicolon separates each of these sections, that is
important. Also note that every single one of the sections may be empty,
though the semicolons still have to be there. If the condition is empty, it
is evaluated as true and the loop will repeat until something else stops it.
The FOR repetition control
statement @ LOOP
 This program is a very simple example of a for loop. x is set to zero,
while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until
the condition is met. Keep in mind also that the variable is
incremented after the code in the loop is run for the first time.

#include <iostream.h> for (int x; x<10; x++)


{
Using namespace std; cout<<x<<endl;
//so the program can see }
cout n endl }

main()
{

You might also like