C : Programming Language

Control Flow

See, the written software code are expected to execute in a sequence but we don't want all the code to run in a sequence always because there are conditions where one set of instructions or code is expected to run while in other condition we want another piece of code to run. Lets take a real life example. We are writing code for an elevator/lift. Now if the elevator is going upward it should keep checking which all floors it has to stop and which all it has to avoid. So the code will execute till the elevator has completed its all jobs and to do that it will repeat some set of instruction to check whether it has to stop at next floor or move on.

Assume a building of 30 story and all people get onboard in the elevator at ground floor and they pressed their destination floors. Say floor 1, 6, 9, 13 and 24 are selected. Let us assume, for simplicity, that elevator does not take input once it leave ground floor.

//Initial condition
floorInput =  [1, 6, 9, 13, 24]
currentFloor = 0
Repeat:
          if currentFloor is Equal To Any Floor In Floor Input List then make a stoppage at floor
          else if currentFloor is equal To 29 [maximum it could go] then move to ground floor and stop
          else move to nextFloor and make currentFloor = currentFloor + 1

This code above is  just a pseudo-code to understand how a sequence of code could execute. See we have words like Repeat, if and else. Repeat is asking the computer/cpu to execute certain piece of code again and again till it meets certain condition if found and job is supposed to be done. Inside Repeat block there are some conditions based on which either the elevator will stop at any floor or not. In our case to simply the code, the elevator will stop at a floor given as input.

This is how computer programmes needs to behave. So now lets check how swift helps us in achieving this goal. In swift we have followings control flow mechanisms:

Conditional Branching Flow

Repetitional Flow

Execution Break / Termination Flow

IF

if ( condition ) {

statements
}

Example:
int a = 80;
char grade = 'F';
if ( a > 75 ) {
grade = 'A';
}


IF-ELSE

if ( condition ) {

statements

} else if ( condition ) {

statements

}else {

statements

}

Example:

int a = 80;
char grade = 'F';
if ( a > 75 ) {
grade = 'A';
} else if ( a > 60 ) {
grade = 'B';
} else if ( a > 40 ) {
grade = 'C';
} else {
grade = 'F';
}


SWITCH-CASE

switch ( some value to consider ) {

case value1 :

statements for value 1
break;

case value2 :

statements for value 2
break;

default :

statements for default
break;

}

Example:
switch ( grade ) {
case 'A':
a = 85
break;

case 'B':
a = 70
break;

case 'C':
a = 50
break;

default :
a = 35
break;
}

FOR

For loop block is simple. It has three components initialization, conditions and increment. Initialization component is executed once before the loop fires then it checks for condition. If condition is met then it goes inside the loop block and executes the statements then it executes the increment component again it checks for condition, executes statements inside loop block, executes increment component and keep repeating till condition is met. If condition is written in a way it will meet every time then loop will execute infinite time.

for ( initialization ; condition ; increment ) {

statements
}

Example:
int i;
for ( i = 0; i < 10; i++ {
printf("%d", i);
}

These components are not mandatory and any component could be missing but to work with this loop properly these components has to be present somewhere within the loop like initialization could occur before and condition check could occur within loop block with if and there should be a break or return statement to terminate the loop when job is supposedly done.

Example:

int i = 0 ;
for ( ; ; ) {
if ( i < 10 )
printf("%d", i);
else
break;
}
If you see in the above example and compare with example before it then you will find both are different. But if you run the code you won't see any difference in output.

WHILE

while condition {

statements
}

Example:
int i = 0;
while ( i < 10 ) {
printf("%d", i);
i++;
}

DO-WHILE

do {

statements

} while ( condition ) ;

Example:
int i = 10 ;
do {
printf("%d", i) ;
i-- ;
} while ( i > 0 );

Practice:

  1. Remember the keyword, if, else, for, in, repeat, while, break, exit, return etc.
  2. Experiment with different combination of loops, if-else, break, switch-cases.
  3. Experiment removing braces and adding extra braces and see what error you get.


References:

  1. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html