1/50
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Simple Statements
a simple instruction that ends with asemicolon
Compound Statements
several instructions that aregrouped into block enclosed in braces ({ })
Selection Statements
check certain conditions before executing a set ofstatements.provide the branching mechanism which allows aprogram to choose from alternatives
- if statement
if-else statement
conditional operator
swich statement
examples of selection statements
if statement
used for single selection.
the statement will beexecuted if and only if theexpression evaluates to true.
if-else statement
The else keyword is used when there are two possible coursesof action after an if statement.
conditional operator
written as ?:, is a ternary operator,which means that it takes three argumentsThe general syntax is:
break
separates the execution of statement per labeed case so that these is no "fall through" into the other statements
default
executed when expression does not evaluate to any of the listed constans in the case section
Repetition Statements
In looping, a sequence of statements is executed until some condition is satisfied which is placed for termination of theloop.
body of the loop
control statement
A program loop consists of two segments
entry controlled loop (pretest loop)
where the conditions are tested first and if satisfied then only the body of loop is executed.
exit controlled loop (post test loop)
where the test ismade at the end of the body, so the body is executed unconditionally first time
- while loop
- do while loop
-for loop
looping can of the following form
while loop
entry controlled loop statement
while loop
loop control variable must be: (1) initialized, (2)tested, and (3) updated for the loop to execute correctly
initialize
variable is usually set to a starting value beforethe while statement is reached
test
variable is tested before the start of each loop repetition
update
variable is updated within the body of the whileloop during each execution or pass
do while
exit controlled loop
for loop
an entry-controlled loop thatprovides a more concise loop control structure.
for loop
allows the user to efficiently write a loop that needs to be executed for a specific number of times
initialization
Executed first and only once. Allows the user to declare and initialize anyloop control variables
test condition
If met, the body of the loop is executed. Otherwise, the flow of control jumps to the next statement after the loop
increment/decrement
Step size; after the body of the for loop is executed, it checks the step size of the iterations.
More than one variable can be initialized at a time in the for statement
for (p=1,n=0; n<18; ++n)
It is also permissible to use expressions in the assignment statements ofinitialization and increment sections.
for (w = (a+b); w>0; w=w/2) //is valid
Like the initialization section, the increment section may also havemore than one part.
for (a=2,b=30; n <= m; n=n+1,m=m-1)
{ p = b/a;
cout << p << endl;
}
Empty for loop is possible
int i = 0, max = 3;
for ( ; ; )
{ if ( i < max)
{ cout<<"Hello!\n";i++;
}
else
break;
}
Declare loop control variable inside the for
int sum = 0, fact = 1;
for ( int i = 1; i<=5 ; i++ )
{
sum += i; // i is known throughout the loop
fact *= i;
}
while
This is a good, solid looping process with applications to numerous situations
do-while
This is a good choice when you are asking a question, whose answer will determine if the loop is repeated
for
This is a good choice when the number of repetitions is known or can be supplied by the user
counter controlled loop
used to read in and process a sequence of data items when you know in advance the number of items to be read.
Sentinel Controlled Loop
the user enters a "unique" data value, called a sentinel value, after the last data item; the loop repetition condition tests each data item and causes the loop to exit when the sentinel value is read.
return 0;
It is possible to end a program (or function) prior toi ts normal termination by using an "extra" return 0; statement.
return 0;
The drawback to this strategy is that it ends the entire program (or function) at the point where the return 0; statement is placed.
exit ()
It requires the Standard Library header file, stdlib.
exit (value)
format for exit ()
break;
Gets you out of a loop.
break;
The program continues with the next statementimmediately following the loop.
True
Break does not break out of a "nested loop"
continue;
performs a "jump" to the next test condition in a loop
True
The continue statement may be used ONLY in iterationstatements (loops). It serves to bypass, or jump over,certain iteration sections of a loop. continue;
Zero Iteration Loop
There may be instances where thewhile loop will not execute because the test failed (False condition).
Flag Controlled Loop
control theexecution of a loop
Flag Controlled Loop
type of boolen
Falg Controlled Loop
the value of the loop is initialized
Nested Loop
placing of one loop inside the body of another loop
Nested Loop
most commonly used in for loops
True
When working with nested loops, the outer loop changes onlyafter the inner loop is completely finished (or is interrupted).