{
steps in sequence
}
if ( condition )
{
Steps executed if condition was true
}
else
}
Steps executed if condition was false
}
if ( condition )
{
Steps executed if condition was true
}
if(condition1)
case1
else if(condition2)
case2
else if(condition3)
case3
else
case4
if(A)
{
if(B)
{// A and B
...
}
else
{// A and not B
...
}//if(B)
}
else // not A
{// not A
if(B)
{// not A and B
...
}
else
{// not A and not B
...
}//if(B)
}//if(A)
if( i = 1 )when you want to test for equality. The above compiles and runs but (1) the condition is always true and (2) it changes the variable i to be 1! Instead write
if( i == 1 )
When do I use if statements
Any time that you need to make a program choose between
two (or more) alternative sequences of actions.
You should expect to be using if in every program you
write.
Can you use if statements for things other than sales
YES. We discussed calculations of date, pressures in veessels,
tax rules, .... etc etc .
The example discounts in the book are wrong
Quite possibly.
Is there a limit on the number of if statements in a program
No. You can use as many as you need to make it work
the way it should work.
Is C++ used in Graphics, Games, Special Effects, Movies
Yes.
However in Movies the C++ will be hidden inside an interface
for movie makers to use.
Do you terminate an if with something like an ENDIF
No. An if-else ends at the end of the statement
after the else ... which may be a sequence inside {braces}.
This is historical and saves typing.
What does -= mean on page 45
This is the "subtract from" operator. It one of a series
of operators:
| Short form | Long form |
|---|---|
| x -= e; | x = x - e; |
| x += e; | x = x + e; |
| x *= e; | x = x * e; |
| x /= e; | x = x / e; |
| etc. |
if(A)
B
else
C
if(D)
E
else
F
x = e;
cin >> x...;
cout << e ....;So sometimes that will be appear before a brace, but not always. Statements can also end with braces and so you can see things like this
}
}
}Sometimes I've even done this
} } }
if( day == THU )
{
if( time == 12 )
{
class = 201;
}
else if( time == 12 )
{
class = 375;
}
else
{
class = 0;
}
}
if( day == THU )
if( time == 12 )
class = 201;
else if( time == 12 )
class = 375;
else
class = 0;
This is what the next readings and class will be about: [ 05.html ] Logic.
Next -- Comparison and Logical Operators
[ 05.html ]
with project 1 (straight forward program) due and quiz 2.