Control Flow
- Decisions - if then else
- More decisions - switch
- Loops - while, do while, for
- Keyword-break
- Keyword-continue
Decisions - if then
- Parentheses surround the test
- One statement becomes the “then part”
- If more are required, braces must be used
if then else
- An optional else may be added
- One statement by default, if more are required, braces must be used
Nesting if-s
- else associates with the nearest if
Example :
Result : i is reasonable
Example :
Result : i is negative
switch
C supports a switch for multi-way decision making
while Loop
- The simplest C loop is the while
- Parentheses must surround the condition
- One statement forms the body of the loop
- Braces must be added if more statements are to be executed
Example:
do while
do while guarantees execution at least oncefor Loop
for encapsulates the essential elements of a loop into one statement
Example :
Result :
Example :
Result :
Stepping With for
Unlike some languages, the for loop is not restricted to stepping up or down by 1
Extending the for Loop
The initial and update parts may contain multiple comma separated statements
The initial, condition and update parts may contain no statements at all!
- if you dont want to use initial part than you can use as shown below Example :
1
- if you dont want to use initial part and update part than you can use as shown below Example :
1
- and if you dont want to use initial part,condition and update part than it creates an infinite loop so you can use as shown below Example :
1