Professional Documents
Culture Documents
ch04 Conditions
ch04 Conditions
Control Flow
Chapter Contents
if statement
Conditional operator - “ ? : ”
Nested if
switch statement
break – continue
Summary
Exercise
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.)
if – Flow Diagram
if statement:
F test T
expression
statement(s)
statement(s) statement(s)
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;
}
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
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
4. #include <stdio.h>
Conditional Operator
The operator “ ? : ” is the only operator that takes three
operands, each of which is an expression.
Syntax:
x = abs (y)
y < 0 x = -y
y >= 0 x = y
(y < 0) ? -y : y
x = abs(y)
get 2 numbers
get an oper, + or -
N Y
oper is ‘+’ or ‘-’
print error N Y
message Oper is ‘+’
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
Programming C Style
if (stam != 0) if (stam)
if ((p==0)&&(q ==0))
res = 1;
res = (!p && !q)
else
res = 0;
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
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
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;}
Summary – cont’d
Several courses of action can be expressed using nested if .