0% found this document useful (0 votes)
17 views24 pages

Lec 5 Flow Control

Uploaded by

dennismwenda342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views24 pages

Lec 5 Flow Control

Uploaded by

dennismwenda342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 5

Flow control – Decision making


Controlling the program flow
• Forms of controlling the
program flow:
– Executing a sequence Statement1
Statement2
of statements Statement3
– Repeating a sequence Statement4
Statement5
of statements (until Statement6
some condition is met) Statement7
(looping) Statement8

– Using a test to decide


between alternative
sequences (branching)
The if statement
if ( expression )
program statement

If expression is true
(non-zero), executes
statement.
If gives you the choice
no of executing statement
expression or skipping it.

yes

Program statement
Example - if

// Program to calculate the absolute value of an integer


int main (void)
{
int number;
printf ("Type in your number: ");
scanf ("%i", &number);
if ( number < 0 )
number = -number;
printf ("The absolute value is %i\n", number);
return 0;
}
The if-else statement
if ( expression )
program statement 1
else if-else statement:
program statement 2 enables you to
choose between
two statements

yes no
expression

Program statement 1 Program statement 2


Example: if-else
// Program to determine if a number is even or odd
#include <stdio.h>
int main ()
{
int number_to_test, remainder;
printf ("Enter your number to be tested: ");
scanf ("%i", &number_to_test);
remainder = number_to_test % 2;
if ( remainder == 0 )
printf ("The number is even.\n");
else
printf ("The number is odd.\n");
return 0;
}
Nested if statements
It is always legal in C programming to nest if-else statements,
which means you can use one if or else if statement
inside another if or else if statement(s).

if (number > 5)
if (number < 10)
printf(“1111\n"); Rule: an else
else printf(“2222\n"); goes with the most
recent if, unless
if (number > 5) { braces indicate
if (number < 10) otherwise
printf(“1111\n");
}
else printf(“2222\n");
Example: else-if
// Program to implement the sign function
#include <stdio.h>
int main (void)
{
int number, sign;
printf ("Please type in a number: ");
scanf ("%i", &number);
if ( number < 0 )
sign = -1;
else if ( number == 0 )
sign = 0;
else // Must be positive
sign = 1;
printf ("Sign = %i\n", sign);
return 0;
}
Switch Statement
A switch statement allows
a variable to be tested for
equality against a list of
values. Each value is
called a case, and the
variable being switched on
is checked for each switch
case.
The switch statement
switch ( expression )
{
case value1:
program statement The expression is
program statement successively compared
... against the values value1,
break; value2, ..., valuen. If a case is
case value2: found whose value is equal to
program statement the value of expression, the
program statement
program statements that follow
...
break; the case are executed.
...
case valuen:
program statement The switch test expression must be
program statement
... one with an integer value
break; (including type char) (No float !).
default: The case values must be integer-
program statement type constants or integer
program statement
... constant expressions (You can't use
break; a variable for a case label !)
}
The switch statement (cont)
•The value in switch statement has many cases.

int main()
{
int value;
scanf(“%d”,&value);
switch (value) {
case 1 :
printf(“1 received\n”);
break;
case 2 :
printf(“2 received\n”);
break;
default :
printf(“ values except 1 and 2 were
received.\n”);
break;
}
return 0;
}
Looping - The while statement
while ( expression )
program statement
Loop with the
test in the
beginning !
Body might
never be
executed !

Loop_expression
no
yes
statement
Example - while
// Program to reverse the digits of a number
#include <stdio.h>
int main (void)
{
int number, right_digit;
printf ("Enter your number.\n");
scanf ("%i", &number);
while ( number != 0 )
{
right_digit = number % 10;
printf ("%i", right_digit);
number = number / 10;
}
printf ("\n");
return 0;
}
The do statement
do
program statement
while ( loop_expression );

Loop with the


test at the end !
Body is
executed at least
once !
statement

yes
loop_expression

no
Example – do while
// Program to reverse the digits of a number
#include <stdio.h>
int main ()
{
int number, right_digit;
printf ("Enter your number.\n");
scanf ("%i", &number);
do
{
right_digit = number % 10;
printf ("%i", right_digit);
number = number / 10;
}
while ( number != 0 );
printf ("\n");
return 0;
}
The for statement
for ( init_expression; loop_condition; loop_expression )
program statement

1 init_expression

no
5 2 loop_condition

yes

3 Program statement

4 Loop expression
The for statement
no

1 2 5 4
yes
for ( n = 1; n <= 200; n = n + 1 )
triangularNumber = triangularNumber + n;
3
How for works
• The execution of a for statement proceeds as follows:
1. The initial expression is evaluated first. This expression usually
sets a variable that will be used inside the loop, generally
referred to as an index variable, to some initial value.
2. The looping condition is evaluated. If the condition is not satisfied
(the expression is false – has value 0), the loop is immediately
terminated. Execution continues with the program statement that
immediately follows the loop.
3. The program statement that constitutes the body of the loop is
executed.
4. The looping expression is evaluated. This expression is
generally used to change the value of the index variable
5. Return to step 2.
for example

#include <stdio.h>
int main ()
{
int a;
/* for loop execution */
for( a = 0; a < 20; a++)
{
printf("value of a: %d\n", a);
}
return 0;
}
Which loop to choose ?
• Criteria: Who determines looping
– Entry-condition loop -> for, while
– Exit-condition loop -> do
• Criteria: Number of repetitions:
– Indefinite loops ->while
– Counting loops -> for
• In C, you can actually rewrite any while as
a for and viceversa !
The break Statement
• Can be used in order to immediately
exiting from a loop
• After a break, following statements in the
loop body are skipped and execution
continues with the first statement after the
loop
• If a break is executed from within nested
loops, only the innermost loop is
terminated
The break statement
• Programming style: don’t abuse break !!!
...
while ( number != 0 ) {
// Statements to do something in loop
printf("Stop, answer 1: ");
scanf ("%i", &answer);
if(answer == 1)
break; // very bad idea to do this
}
The continue statement
• Similar to the break statement, but it does
not make the loop terminate, just skips to
the next iteration
The continue statement
Continue also not so good style!!!
...
while ( number != 0 ) {
// Statements to do something in loop
printf(“Skip next statements answer 1: ");
scanf ("%i", &answer);
if(answer == 1)
continue; // not so good idea…
// Statements to do something in loop
// If answer was 1 these statements are
// not executed. They are skipped.
// Go straight to the beginning of while
}

You might also like