KEMBAR78
PF Lecture 1b | PDF | Control Flow | Php
0% found this document useful (0 votes)
8 views31 pages

PF Lecture 1b

Uploaded by

i246509
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)
8 views31 pages

PF Lecture 1b

Uploaded by

i246509
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/ 31

PROGRAMMING

FUNDAMENTALS
LECTURE 01B
DO WHILE LOOP
• In a while loop, the test expression is evaluated at the
beginning of the loop.
• If the test expression is false when the loop is entered, the loop
body won’t be executed at all.
• In some situations, this is what you want. But sometimes you
want to guarantee that the loop body is executed at least once,
no matter what the initial state of the test expression.
• When this is the case you should use the do loop, which places
the test expression at the end of the loop.
Flow Chart
of Do While
Loop
SYNTAX OF
DO WHILE
LOOP
EXAMPLE 01
EXAMPLE 2A
• Where to use do while loop
- Validity Check
EXAMPLE 2B
WHEN TO USE WHICH LOOP
• The for loop is appropriate when you know in advance how
many times the loop will be executed.
• The while and do loops are used when you don’t know in
advance when the loop will terminate (the while loop whenyou
may not want to execute the loop body even once, and the do
loop when you’re sure you want to execute the loop body at
least once).
OTHER CONTROL STATEMENTS IN C++
• BREAK:

• To break the current flow (loop, case, if block etc)


• CONTINUE:

• Used in Loops to skip the remaining statement and go to


start of the iteration
• GOTO

• To get to certain point in program


BREAK
FLOWCHA
RT
TRY THIS EXAMPLE
#include <iostream>
using namespace std;
#include <conio.h> //for getche()
int main()
{
const unsigned char WHITE = 219; //solid color (primes)
const unsigned char GRAY = 176; //gray (non primes)
unsigned char ch;
//for each screen position
for(int count=0; count<80*25-1; count++)
{
ch = WHITE; //assume it’s prime
for(int j=2; j<count; j++) //divide by every integer from
if(count%j == 0) //2 on up; if remainder is 0,
{
ch = GRAY; //it’s not prime
break; //break out of inner loop
}
cout << ch; //display the character
}
getch(); //freeze screen until keypress
return 0;
}
EFFECT OF BREAK
• In previous example, there is a nested for loop while break is
applied in internal loop.
• Notice that break only takes you out of the innermost loop.
• no matter what constructions are nested inside each other:
break only takes you out of the construction in which it’s
embedded.
• If there were a switch within a loop, a break in the switch would
only take you out of the switch, not out of the loop.
CONTINUE STATEMENT
• The break statement takes you out of the bottom of a loop.
Sometimes, however, you want to go back to the top of the
loop when something unexpected happens.
• Executing continue has this effect. (Strictly speaking, the
continue takes you to the closing brace of the loop body, from
which you may jump back to the top.)
CONTINUE
FLOWCHART
TRY THIS EXAMPLE
#include <iostream>
using namespace std;
int main()
{
long dividend, divisor;
char ch;
do {
cout << “Enter dividend: “; cin >> dividend;
cout << “Enter divisor: “; cin >> divisor;
if( divisor == 0 ) //if attempt to
{ //divide by 0,
cout << “Illegal divisor\n”; //display message
continue; //go to top of loop
}
cout << “Quotient is “ << dividend / divisor;
cout << “, remainder is “ << dividend % divisor;
cout << “\nDo another? (y/n): “;
cin >> ch;
} while( ch != ‘n’ );
return 0;
}
BREAK AND CONTINUE STATEMENTS
NOTE: “Continue” statement applies only to loops
BREAK AND CONTINUE STATEMENTS
for(int INT X=1;
for(int x=1;x<10;x++)
{
x=1;x<10;x++) WHILE(X<10)

if(x%2==0) { {
continue; if(x%2==0) IF(X%2==0)
else continue; CONTINUE;
cout<<x<<“ else ELSE
“; COUT<<X<<ENDL;
// cout<<“hi\n”;
} cout<<x<<" ";
X++;
cout<<"hi\
}
Output: n";
} Output
1 hi
Output:
1359 ???
3 hi
5 hi
7 hi
9 hi
GOTO STATEMENT
• Although it is not a good idea to use it , GOTO is used to jump
to any section of code where a label is placed.
• It is a key word followed by label name.
• It takes the control to the label.
• The label is always terminated by a colon.

goto SystemCrash;
// other statements
SystemCrash:
// control will begin here following goto
THE SWITCH STATEMENT
• If you have a large decision tree, and all the decisions depend
on the value of the same variable, you will probably want
to consider a switch statement instead of a ladder of if...else or
else if constructions.
• Switch statements are useful when multiple decisions are
based on value of a single variable.
• Such variable is called switch variable.
• The keyword switch is followed by switch variable inside
parenthesis
Switch(switch variable)
SYNTAX OF
THE SWITCH
STATEMENT
TRY THIS EXAMPLE
#include <iostream>
using namespace std;
int main()
{
int speed; //turntable speed
cout << “\nEnter 33, 45, or 78: “;
cin >> speed; //user enters speed
switch(speed) //selection based on speed
{
case 33: //user entered 33
cout << “LP album\n”;
break;
case 45: //user entered 45
cout << “Single selection\n”;
break;
case 78: //user entered 78
cout << “Obsolete format\n”;
break;
}
return 0;
}
THE SWITCH STATEMENT
• Before entering the switch, the program should assign a value
to the switch variable.
• This value will usually match a constant in one of the case
statements.
• The data type of the case constants should match that of the
switch variable.
• When this is the case, the statements immediately following
the keyword case will be executed, until a break is reached.
• Switch structure has a break statement at the end of each case
section.
THE SWITCH STATEMENT
• The break keyword causes the entire switch statement to exit.
• Control goes to the first statement following the end of the
switch construction.
• Don’t forget the break; without it, control passes down (or “falls
through”) to the statements for the next case, which Is not
usually desirable.
• If the value of the switch variable doesn’t match any of the
case constants, control passes to the end of the switch without
doing anything.
• A Default case is used in this scenario.
THE SWITCH STATEMENT
• This keyword gives the switch construction a way to take an
action if the value of the loop variable doesn’t match any of the
case constants.
• A switch statement is a common approach to analyzing input
entered by the user. Each of the possible characters is
represented by a case.
• It’s a good idea to use a default statement in all switch
statements, even if you don’t think you need it. A construction
such as
default:
cout << “Error: incorrect input to switch”; break;
OPERATION
OF THE
SWITCH
STATEMENT
SWITCH STATEMENT WITH CHARACTER VARIABLES
#include <iostream>
using namespace std;
#include <conio.h> //for getche()
int main()
{
char dir=’a’;
int x=10, y=10;
while( dir != ‘\r’ )
{
cout << “\nYour location is “ << x << “, “ << y;
cout << “\nEnter direction (n, s, e, w): “;
dir = getche(); //get character
switch(dir) //switch on it
{
case ‘n’: y--; break; //go north
case ‘s’: y++; break; //go south
case ‘e’: x++; break; //go east
case ‘w’: x--; break; //go west
case ‘\r’: cout << “Exiting\n”; break; //Enter key
default: cout << “Try again\n”; //unknown char
} //end switch
} //end while
return 0;
} //end main
SWITCH VERSUS IF...ELSE
• When do you use a series of if...else (or else if) statements,
and when do you use a switch statement?
• In an else if construction you can use a series of expressions
that involve unrelated variables and are as complex as you
like.
if( SteamPressure*Factor > 56 )
// statements
else if( VoltageIn + VoltageOut < 23000)
// statements
else if( day==Thursday )
// statements
else
// statements
SWITCH VERSUS IF...ELSE
• In a switch statement, however, all the branches are selected
by the same variable.
• the only thing distinguishing one branch from another is the
value of this variable. You can’t say
case a<3:
// do something
break;
• The case constant must be an integer or character constant,
like 3 or ‘a’, or an expression that evaluates to a constant, like
‘a’+32.
THE CONDITIONAL OPERATOR
• It is decision operator also called ternary operator (?).
• A variable is given one value if something is true and another
value if it’s false.
• It can easily replace the if/else structure
if( alpha < beta )
min = alpha;
else
min = beta;
• This operator consists of two symbols, which operate on three
operands.
• It’s the only such operator in C++; other operators operate on
one or two operands.
min = (alpha<beta) ? alpha : beta;
THE CONDITIONAL OPERATOR

• The part of this statement to the right of the equal sign is called the
conditional expression:
(alpha<beta) ? alpha : beta // conditional expression
• The question mark and the colon make up the conditional operator.
• The expression before the question mark is the test expression.
• min and alpha and beta are the three operands.
• If the test expression is true, the entire conditional expression takes on
the value of the operand following the question mark: alpha in this
example.
• f the test expression is false, the conditional expression takes on the
value of the operand following the colon: beta.
• The parentheses around the test expression aren’t needed for the
compiler, but they’re customary; they make the statement easier to read

You might also like