You are on page 1of 22

Chapter 4

Control Flow

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 2

Chapter Contents
 if statement

 Conditional operator - “ ? : ”

 Nested if

 switch statement

 break – continue

 Summary

 Exercise

The if statement provides us with a method for


choosing between different possible courses
of action.

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 3

Control Flow
 C language is a procedural language.
That means that the program executes command after
command, from top to bottom.
 Control sentences enables to alter the flow of the program, skip
over commands or repeat commands according to the different
conditions in the program.
 One of the basic flow controls is the if sentence.
 The if sentence enables us to skip commands or execute
commands according to a condition.
The condition is an expression that returns the following
value:
 0 if the expression is false.
 1 if the expression is true.
The expression usually contain relational and logical
operators (&& || > != == etc.)

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 4

if – Flow Diagram
 if statement:
F test T
expression

statement(s)

 if … else statement: test


F T
expression

statement(s) statement(s)

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 5

if Statement - Syntax
if (expression)
statement; A single statement.

if (expression)
{
statements; A block of statements.
}

if (expression)
statement; Single statement in the if and a
else
statement; single statement in the else.

if (expression)
statement;
A single statement in the if and a block
else
{ of statements in the else.
statements;
}

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 6

Nested if

if (expr1)
?expr1
st1
T F
else if (expr2)
st2 st1 ?expr2
else if (expr3) T F
st3 st2 ?expr3
else T F
st4… st3 st4

 In the nested if, else is related to the nearest if that is not


enclosed in a block.

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 7

Examples: balance.c
start

float balance
int amount

F T
balance > 0
Print ‘must have Get amount
positive balance’

amount>1000
F T
or
amount<100
Print ‘amount
F balance – T
must be
amount < 0 100-1000’
Balance -= Print ‘amount
amount must be bigger
than balance’
Print ‘successful’

end
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 8

Examples: balance.c – cont’d


1. /* balance.c
2. This program is an example for if_else statements
3. The program an AutomaticTellerMachin like */

4. #include <stdio.h>

5. void main (void)


6. {
7. float balance;
8. int amount;

9. printf("Enter your bank balance --> ");


10. scanf("%f",&balance);
11. if (balance > 0) /*the balance is positive*/
12. {
13. printf("What is the amount you wish to
withdrawal"
14. " (100-1000)? --> ");
15. scanf("%d",&amount);

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 9

Examples: balance.c – cont’d


16. if ((amount > 1000) || (amount < 100))
17. /*the withdrawal amount is more than 1000*/
18. {
19. printf("The amount must be 100-1000\
n");
20. }
21. else if ((balance - (float)amount) < 0 )
22. /*the withdrawal amount is between 100-1000
but
23. higher than the balance */
24. {
25. printf ("You can't withdrawal more than
"
26. "you have in your balance\n");
27. }

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 10

Examples: balance.c – cont’d


28. else /*the withdrawal amount is between 100-1000
29. and less than the balance */
30. { /*decrement the amount from the balance*/
31. balance -= (float)amount;
printf("Your withdrawal for %d was "
32. "successful, your new balance is:"
33. " %0.2f\n",amount,balance);
34. }
35. }
36. else /*the balance is not positive*/
37. {
38. printf("Sorry, you must have a positive "
39. "balance for cash withdrawal\n");
40. }
41. }

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 11

Conditional Operator
 The operator “ ? : ” is the only operator that takes three
operands, each of which is an expression.

 The value of the whole expression equals the value of expr2 if


expr1 is true, or equals the value of expr3 if expr1 is false.

 Syntax:

expr1 ? expr2 : expr3

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 12

Operator “?:” – How Does It Work ?

x = abs (y)
y < 0 x = -y

y >= 0 x = y

(y < 0) ? -y : y

x = abs(y)

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 13

Operator “?:” – Example


Explanation
start

get 2 numbers

get an oper, + or -

N Y
oper is ‘+’ or ‘-’

print error N Y
message Oper is ‘+’

Subtract the 2 add the 2 numbers


numbers and print and print

end
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 14

Example: con-oper.c
1 /* con-oper.c
2 This program illustrates the conditional operator ?:
3 two numbers from the user will be added/subtracted and printed. */
4 #include <stdio.h>
5 void main(void)
6 {
7 int n1, n2;
8 char oper; /* will be '+' or '-', the user will decide*/
9

10 printf("Enter 2 numbers => ");


11 scanf ("%d %d", &n1, &n2);
12 getchar(); /* clean the 'enter' from the keyboard */
13 printf("\nEnter : '+' for adding, '-' for
subtracting"
14 " => ");
15 oper = getchar();
16 if(oper == '+' || oper == '-')
17 printf("The result is : %d\n", oper == '+' ?
18 n1+n2 : n1-n2);
19 else
20 printf ("Illegal input\n");
21 } © Copyright: Spymek Software Pvt. Ltd.
Logical Operators
C1 Ch03 - Operators and Expressions – 15

Programming C Style

if (stam != 0) if (stam)

if ((p==0)&&(q ==0)) if (!p && !q)

if ((p==0)&&(q ==0))
res = 1;
res = (!p && !q)
else
res = 0;

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 16

switch Statement
 The nested if can become complicated and unreadable.
 The switch statement is an alternative to the nested if.
 Syntax: switch(expression)
{
case constant expr:
statement(s);
[break;]
case constant expr:
statement(s);
[break;]
case constant expr:
statement(s);
[break;]
default :
statement(s);
[break;]
}
 Usually, but not always, the last statement of a case is break.
 default case is optional.
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 17

switch Statement - Explanation


The value of expression will be looked for in
switch(expression) the cases. The case that matches the value of
expression will be executed.

expr must be constant, known at compilation


time.
Once the matching case had been found, all
case constant expr the statements from that point downwards are
executed, until the end of the switch
statement.

The break statement is optional. It prevents a


break flow through to the next case.

The default statement is optional. If no


default matching case was found, the statements in
the default will be executed.

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 18

Example: switch.c
1 /* switch1.c - This program illustrates the use of switch statement.
The user will enter 2 numbers and an operation (add, divide etc.).
The program will calculate and print the result.*/
2 #define quit 0
3 #define mult 1
4 #define div 2
5 #define add 3
6 #define sub 4
7 #include <stdio.h>
8 void main(void)
9 {
10 int oper; /* a mathematical operation, chosen by the user
*/
11 int num1, num2; /* the operands for oper */
12
13 printf("Enter two numbers : ");
14 scanf("%d %d", &num1, &num2);
15 printf("Enter a mathematical operation for the 2"
16 "numbers:\n");
17 printf("0)Quit\n1)Multiply\n2)Divide\n3)Add\n4)"
18 "Subtract\n");
19 scanf("%d", &oper);
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 19

Example: switch.c – cont’d


20. switch(oper)
21. {
22. case quit:
23. printf("The program is terminated by "
24. "your request.\n");
25. break;
26. case mult:
27. printf("%d * %d = %d\n", num1, num2,
28. num1 * num2);
29. break;
30. case div:
31. if ( num2 == 0 ) /* division by 0 is not
32. allowed ! */
33. printf("Cannot divide by 0, "
34. "sorry.\a\n");
35. else
36. printf("%d / %d = %f\n", num1,
37. num2,(float)num1/num2);
38. break;

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 20

Example: switch.c – cont’d


39. case add:
40. printf("%d + %d = %d\n", num1, num2,
41. num1 + num2);
42. break;
43. case sub:
44. printf("%d - %d = %d\n", num1, num2,
45. num1 - num2);
46. break;
47. default:
48. printf ("Illegal input. Sorry.\n");
49. break;
50. }
51. }

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 21

Summary
 The if statement provides us with a method of choosing
between different possible courses of action.
if (expression)
 The general syntax:
{statements;}
else
{statements;}

 The operator “ ? : ” was introduced (conditional operator). It is


the only operator to take three operands. It’s syntax is:

expr1 ? expr2 : expr3

and the value of the whole expression is:


expr2 if expr1 is true, otherwise expr3.

© Copyright: Spymek Software Pvt. Ltd.


C1 Ch03 - Operators and Expressions – 22

Summary – cont’d
 Several courses of action can be expressed using nested if .

 Replacing the nested if, a more compact statement is


introduced, the switch statement.

© Copyright: Spymek Software Pvt. Ltd.

You might also like