Professional Documents
Culture Documents
Packet 3-16 - Control Structures
Packet 3-16 - Control Structures
Packet 3-16 - Control Structures
Slide Set #3
Program Control Structures
if ( <condition> )
{
<code that is executed only if the
condition is true>
}
else
{
<code that is executed only if the
condition is false>
}
Short-Circuit Evaluation
Short-Circuit Evaluation
For example:
Lets say
float a = 10.0;
float b = -10.0;
if ( (a == 10.0) || (b > 0.0) )
printf(whatever);
The program will never evaluate the
(b>0.0) expression because (a==10.0)
turns out to be true, making the
compound expression true.
Short-Circuit Evaluation
Likewise,:
float a = 10.0;
float b = -10.0;
if ( (a != 10.0) && (b > 0.0) )
printf(whatever);
The program will never evaluate the
second expression because the first
turns out to be false, making the entire
expression false.
Short-Circuit Evaluation
Example:
m>0 ? m+5 : m*2;
switch statements
if(x == 1) {
<statement 1>;}
else if(x == 2) {
<statement 2>; }
else if(x == 3) {
<statement3>; }
else if (x == 4)
..
else {
<statement n>; }
switch statements
switch(controlling expression)
{
case label_1:
<statement 1>;
break;
case label_2:
<statement 2>;
break;
case label_3:
default:
<statement n>;
}
switch statements
Error In Code
Repetition Structures
Repetition Structure
Repetition Structure
Stopping Criteria
for loops
for(<init>; <condition>; <increment>)
{
<statements>;
}
Infinite Loops
Shorthand Assignment
Loops
Do-while loops
Interesting Note
Random Numbers
Random Numbers