Showing posts with label Iteration. Show all posts
Showing posts with label Iteration. Show all posts

Monday, March 13, 2017

C++ Part 21

While and do-while Statements
A while STATEMENT WITH A SINGLE-STATEMENT BODY



while (Boolean_Expression)
Statement
A while STATEMENT WITH A MULTISTATEMENT BODY
while (Boolean_Expression)
{
Statement_1
Statement_2
.
.
.
Statement_Last
}
A do-while STATEMENT WITH A SINGLE-STATEMENT BODY
do
Statement
while (Boolean_Expression);
A do-while STATEMENT WITH
A MULTISTATEMENT BODY
do
{
Statement_1
Statement_2
.
.
.
Statement_Last
} while (Boolean_Expression);
Do not forget
the final
semicolon.  

Sunday, March 12, 2017

C++ Part 15

Iteration

while: execute a statement as long as some condition is true

do while: similar to while but check condition after executing the statement.

for: execute a statement on every element of some data, or some fixed number of times.



while {loop body}

int count = 0 ;
while ( count < 3) { cout << ” Hello ” << count << endl ; count ++; }

Output :


Hello
Hello
Hello

C++ Part 13


Types of Control

  • Selection: Execute a different instruction depending on the result of evaluating some [Boolean] condition.
    • if/else, multiway if, switch, ? :
  • Iteration: Repetitively execute some instruction until some condition is met.
    •  for, while, do while.

Conditions

A condition is a Boolean expression - i.e., one that evaluates to true or false.
Most commonly either a relational operator or a logical combination of conditions:
Relational (Comparison) Operators:
== != < > <= >=
Logical operators:
! && ||
For example :  
  • n <= 100 && n >= 0  // n less than equal to AND n must be greater than 0
  • n< 101 && n >= 0  // n less than 101 AND n greater than equal to 0
  • grade> ’c’ && grade <= ’f’ // grade greater than 'c' ( character c ) AND less than equal to 'f'
  • y< x && y >= 0   // 

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...