Title: CONTROL STRUCTURES
1CONTROL STRUCTURES
Two Roads Diverged into the woodswhich one
should I chose? I chose basing on some
condition!!!! Control structures provide
alternatives to a sequential program Execution
and are used to alter the sequential flow of
execution. One control structure is SELECTION
Expression
True
False
Statement 1
Statement 2
2Logical Example
Suppose that Julia Roberts is my girlfriend and I
am about to go on a date with her. Well Julia
does not like rain. She wants to go to the
beach. But if it rains, she wants to go to the
movie to watch RUNAWAY BRIDE..she claims to be
acting in thatI dont believe!!
IF( Julia is in the movie) I believe her!!!
IF( Weather is raining) I am not going to the
beach or going to the movie OTHERWISE I am going
to the beach
3SELECTION STATEMENT IN C
One way selection if(condition)
Two Way selection if(condition) else
Selection statements
Multi way selection switch case
4RELATIONAL OPERATORS
Try out this. A20, B30 AB ? ? A ! B ?
? A lt B ? ? A lt B ? ? A gt B ? ? A gt B ? ?
- Equal to
- ! Not Equal to
- lt Less than
- lt Less than or equal to
- gt Greater than
- gt Greater than Equal to
5Lets try our Hand in strings!!!!!!
string name1 Julia string name2
Roberts String name3 Julia name1 name2
? ? name1 name3 ? ? name1 ! name2 ?
? name1 gt name2 ? ? name1 lt name2 ? ? name1 lt
name2 ? ?
6Logical Operators!!!
Operator Description
! Not
And
Or
A20, B30 String name ranjit
!(A lt B) ? ? (A lt B) (string ranjit) ?
? (A gt B) (string ranjit) ? ? ((A gt B)
string julia) ( (AltB) string
ranjit)
7Order of Precedence
- Consider the expression
- (11gt5) (6 lt 15) (7 gt8 )
- Evaluate the operator first ..?
- Evaluate the operator first ?
- Are the results the same????
-
- So does order of precedence of logical operators
matter?
Incase your still thinking YES it does matter!!!
8TABLE THAT YOU SHOULDNT FORGET TILL YOU
DIE.!!!!! ?
Operators Precedence
!, , - First
, /, Second
, - Third
lt, lt, gt, gt Fourth
, ! Fifth
Sixth
Seventh
(assignment) LAST
9IF and IF ELSE statements
One Way Selection This is when I may or may not
execute a statement basing on some condition If
(condition) .. Notice That I dont have a
statement which executes when the Condition
turns out to be FALSE Basically, One way
selection alters the execution only when my given
condition is TRUE Its all in the example!!!
EXAMPLE 1
10TWO WAY SELECTION
I have two ways and I select one of the ways
basing on a condition
if (condition) else ..
If my condition is TRUE, the statements below the
IF limiters execute If my condition is FALSE,
the statements under the ELSE limiters execute.
LETS DO Example 2 and Example 3.
11Find the Largest of two numbers!!! Please do
Example 4 Hurray!!! You now know how to find
largest to two numbers!!! So now find me the
biggest of three numbers!!! Ok who ever does
this first!!! Can stand up and shout. HURRAY
AM THE GREATEST IN THIS WORLD!!!!!!! ?
12Multiple Selections!!! NESTED IF
What if I have multiple selections!!! Look at the
syntax below if (condition) if
(condition) else else if
(condition) else
Lets do Example 5
13Multiple Selections SWITCH CASE
Wooffff!!! That previous problem was a bit
confusing!!! So theres another way!!!! switch
(expression) case value1 statement1 break ca
se value2 statement 2 break .. .. default
statements So when do I use SWITCH and when
do I use IFELSE Notice in SWITCH ? expression is
not a logical condition Notice in IF ? condition
is a logical Lets do example 6
14FUNCTIONS
Int main() coutltlthello coutltlthi coutltlthow
coutltltare coutltltyou? coutltlthello cout
ltlthi coutltlthow coutltltare coutltltyou?
Int main() testfunction() Int
testfunction() coutltlthello coutltlthi coutltlt
how coutltltare coutltltyou?
Which is better??? You decide????
15Value Returning functions
Functions
VOID functions
Order of the three parts FUNCTION Header Int
main() .. Function call .. Function()
- A function contains the following
- A function header
- A function call
- The function..
VOID FUNCTIONS
Has no return value. Its all in the example
7.Lets do it A new hello world program.