CH-3
CH-3
The order in which the statements in your program are performed is called flow of control.
In this chapter we learn about:-
Boolean expression
Boolean logic.
And (&&):-returns true if all arguments are true
Or (||):-returns false if all arguments are false
!(negation):-returns true if results are false(inverse)
Relation expression.
>(greater than)
<(lessthan)
==(equal)
>=(greaterthan or equal)
<=(lessthan or equal)
Else if statement
Nested if statement
Switch
Iteration (loop)
For condition
While condition
Do while condition
Out put
Let the user enter number is 4
Nothing happens because the condition/expressions are
false
IF ELSE STATEMENT
General syntax:-
OR
IF ELSE STATEMENT
The if-else statement is slightly different version of if
statement.
Here if the condition is found to be false other
statements are executed.
If the condition or expression evaluates to true the
statement (single)/ block of statements following if is
executed, if it evaluates to false then the
statement(single)/block of statement s following the
keyword else is executed.
The expressions/conditions are the same as described
above in if statement and may be relational
expressions.
IF ELSE STATEMENT EXAMPLE
Instruction.
C++ offers the following Loop Control Instructions:
1. For
2. while
3. do while
FOR LOOP
The for loop allows to specify three things in a single
line:
Create and initialize loop counter variable
Test condition
every iteration .
For loop syntax:-
OR
FOR LOOP
Flow chart of for loop
FOR LOOP EXAMPLE
.
condition is an integer expression just like the one we have seen in the before.
When the system reads the keyword while it evaluates the expression that follows.
If the expression is evaluated as true the statement following the condition is
executed.
It may be a simple statement or a compound statement enclosed in parenthesis
{ }.
Condition Check: In a while loop, the condition is checked before the execution of
the loop body.
Execution: If the condition is initially false, the loop body may never execute.
WHILE LOOP EXAMPLE
.
The do while loop executes in the same way as the while loop.
The only difference is that in do executed at least once even if the condition is
evaluated to be false.
A do-while loop will execute the block of code at least once before checking the loop
condition
After the block is executed, the loop checks the condition specified in the while
statement.
If the condition is true, the block of statements is executed again.
If the condition is false, the loop terminates, and the program continues with the
statement following the do-while block.
The do while statement is always terminated with a semicolon (;) after the loop ends.
DO WHILE LOOP
Break statement
Normally a while loop, a do..while loop, or a for loop will terminate only at the
beginning or at the end of the complete sequence of statements in the loop’s block.
But sometimes there may be situations when you require the control to exit the loop.
This can be fulfilled by using the break keyword.
A break statement terminates a loop.
The control is transferred to the first statement immediately following the loop
construct.
We have seen the use of break statement in switch statement.
When a match is found the statements under the case are executed and then break
statement causes the switch loop to terminate.
A break statement is usually associated with a condition, so there will be an if
statement.
BREAK AND CONTINUE
General form of break statement
BREAK AND CONTINUE
Example
out put
BREAK AND CONTINUE
continue statement
Sometimes there may be situations when you require the control
to exit the loop or skip the particular iteration and go to the next.
This can be done using the keyword continue.
A continue statement causes the rest of the body of the loop to
be omitted, for the current iteration.
The control is transferred to the code that evaluates the normal
test condition for loop termination.
The continue statement is similar to the break statement but
instead of terminating the loop, it transfers execution to the next
iteration of the loop.
It continues the loop after skipping the remaining statements in
its current iteration.
BREAK AND CONTINUE
continue statement
General form of continue statement:
BREAK AND CONTINUE
Exampel
Out put
REVIEW QUESTION
1. What is flow control?
2. Compare while and do while loop?