[go: up one dir, main page]

0% found this document useful (0 votes)
214 views38 pages

Loop Control Statements

The document discusses various loop control statements and decision making statements in C++. It covers the break, continue, and goto statements used in loops. It also covers the if, if-else, switch, and nested if statements used for decision making. It provides examples of using arrays to store data elements and accessing array elements using indexes. It also discusses basic input/output operations in C++ using streams like cout and cin for output and input respectively.

Uploaded by

Connor Walsh
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)
214 views38 pages

Loop Control Statements

The document discusses various loop control statements and decision making statements in C++. It covers the break, continue, and goto statements used in loops. It also covers the if, if-else, switch, and nested if statements used for decision making. It provides examples of using arrays to store data elements and accessing array elements using indexes. It also discusses basic input/output operations in C++ using streams like cout and cin for output and input respectively.

Uploaded by

Connor Walsh
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/ 38

Loop Control Statements:

break statement
The break statement has the following two usages
in C++:
When the break statement is encountered
inside a loop, the loop is immediately terminated
and program control resumes at the next
statement following the loop.
It can be used to terminate a case in the switch.

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.
Flow Diagram:
Example:
continue statement
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.
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.
Flow Diagram:
Example:
goto statement
A goto statement provides an
unconditional jump from the goto to a
labeled statement in the same function.
NOTE: Use of goto statement is highly
discouraged because it makes difficult
to trace the control flow of a program,
making the program hard to understand
and hard to modify. Any program that
uses a goto can be rewritten so that it
doesn't need the goto.
Flow Diagram:
Example:
Decision Making
Decision making structures require that
the programmer specify one or more
conditions to be evaluated or tested by
the program, along with a statement or
statements to be executed if the
condition is determined to be true, and
optionally, other statements to be
executed if the condition is determined
to be false.
C++ programming language provides following
types of decision making statements.
if statement

Flow Diagram:
Example:
The if...else Statement:
Example:
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.
The following rules apply to a switch statement:

The expression used in a switch statement


must have an integral or enumerated type, or
be of a class type in which the class has a single
conversion function to an integral or
enumerated type.
You can have any number of case statements
within a switch. Each case is followed by the
value to be compared to and a colon.
The constant-expression for a case must be
the same data type as the variable in the switch,
and it must be a constant or a literal.
When the variable being switched on is equal to
a case, the statements following that case will
execute until a break statement is reached.
When a break statement is reached, the switch
terminates, and the flow of control jumps to the
next line following the switch statement.
Not every case needs to contain a break. If no
break appears, the flow of control will fall through
to subsequent cases until a break is reached.
A switch statement can have an optional
default case, which must appear at the end of
the switch. The default case can be used for
performing a task when none of the cases is true.
No break is needed in the default case.
Flow Diagram:
Example:
nested if statements

It is always legal to nest if-else statements,


which means you can use one if or else if
statement inside another if or else if
statement(s).
Example:
Example:
Array
Array
C++ provides a data structure, the array,
which stores a fixed-size sequential
collection of elements of the same type. An
array is used to store a collection of data,
but it is often more useful to think of an array
as a collection of variables of the same type.
Instead of declaring individual variables,
such as number0, number1, ..., and
number99, you declare one array variable
such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to
represent individual variables. A specific
element in an array is accessed by an index.
Declaring Arrays:
To declare an array in C++, the
programmer specifies the type of the
elements and the number of elements
required by an array as follows:
This is called a single-dimension array. The arraySize must
be an integer constant greater than zero and type can be
any valid C++ data type. For example, to declare a 10-
element array called balance of type double, use this
statement:
Initializing Arrays:
You can initialize C++ array elements either one by
one or using a single statement as follows:

The number of values between braces { } can not


be larger than the number of elements that we
declare for the array between square brackets [ ].
Example:

This program makes use of setw() function to format the


output.
Basic Input/Output
The C++ standard libraries provide an extensive
set of input/output capabilities which we will see
in subsequent chapters. This chapter will
discuss very basic and most common I/O
operations required for C++ programming.
C++ I/O occurs in streams, which are
sequences of bytes. If bytes flow from a device
like a keyboard, a disk drive, or a network
connection, etc., to main memory, this is called
input operation and if bytes flow from main
memory to a device like a display screen, a
printer, a disk drive, or a network connection,
etc, this is called output operation.
I/O Library Header Files:
The standard output stream (cout):
The predefined object cout is an
instance of ostream class. The cout
object is said to be "connected to" the
standard output device, which usually is
the display screen. The cout is used in
conjunction with the stream insertion
operator, which is written as << which
are two less than signs.
The standard input stream (cin):
The predefined object cin is an
instance of istream class. The cin
object is said to be attached to the
standard input device, which usually is
the keyboard. The cin is used in
conjunction with the stream
extraction operator, which is written
as >> which are two greater than
signs as shown in the following
example.
#include <iostream>
using namespace std;
int main()
{
char name[50];
char surname[50];
cout << "Please enter your name: ";
cin >> name;
cout << "Please enter your Surname: ";
cin >> surname;
return 0;
}

You might also like