[go: up one dir, main page]

0% found this document useful (0 votes)
5 views25 pages

Lecture 4

Uploaded by

Rayen Cherbib
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)
5 views25 pages

Lecture 4

Uploaded by

Rayen Cherbib
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/ 25

Department of Electrical Engineering

ELEC366 - Embedded Systems


ELEC
366

Some C Essentials:
Control Flow

Lecture 4 Prof. F Bensaali


Content:
• Statements and Blocks
• If
 Equality and Relational Operators
 Negation Operator
 Logical AND and OR
• if-else
• Conditional Expression
• switch
• while Loop
• do-while
• for loop
• break statement
• continue statement

2
Statements and
Teaching (Cont’d)
Statements and Blocks
Blocks

 An expression becomes a statement when it is followed by a


semicolon
o The semicolon is a statement terminator

x = a + b; i++;

 Braces { and } are used to group declarations and statements


together

3
if Statement
if

 The if construct has the following format:

if (expression)
{ TRUE
statement_1; Condition
statement_2;
. statement_1
. FALSE
.
statement_n;
statement_n
}

4
Expressions
Expressions

Expression

Equality expression Logical expression

 Equality expression: equality and relational operators


are used
 Logical expression: logical operators are used

5
Equality
Teaching Equality and Relational
(Cont’d) and Relational Operators
Operators

a == b // a is equal to b
a != b // a is not equal to b
a < b // a is less than b
a > b // a is greater than b
a <= b // a is less than or equal b
a >= b // a is greater than or equal b

 These operators evaluate to TRUE or FALSE


 TRUE: any non-zero value
 FALSE: zero

6
Logical
Teaching
Logical Operators: Negation
(Cont’d)
Operators: Negation Operator
Operator

 ! is a unary operator used for logical negation


 If applied to non-zero values, the result will be zero
 If applied to a zero value, the result will be 1

int a, b; int a, b;
a = 5; a = 0;
b = !a; // b = 0 b = !a; // b = 1

7
Logical
Teaching
Logical Operators: Logical
(Cont’d)
Operators: Logical AND
AND and
and OR
OR

Logical operators
Operation operator
AND &&  && tests for two conditions being true
OR ||  || tests if either conditions is true

a && b a || b
a FALSE TRUE a FALSE TRUE
b b
FALSE FALSE FALSE FALSE FALSE TRUE

TRUE FALSE TRUE TRUE TRUE TRUE

8
Logical AND
Teaching (Cont’d)
Logical AND and
and OR
OR (Cont’d)
(Cont’d)

Example

int i = 90;
char c= 'A';
if ((i > 50) && (i <= 100))
{
printf("value is between 50 and 100 \n");
}
if ((c = 'a') || (c = 'A'))
{
printf("letter A selected \n");
}

9
if-else
if-else

 The if-else construct has the following format:


if (expression)
{
statement_1;
. FALSE/ELSE TRUE
Condition
.
statement_n; statement_11 statement_1
}
else statement_1n statement_n
{
statement_11;
.
.
statement_1n;
}
10
if-else (Cont’d)
if-else (Cont’d)

 A statement associated with an if can itself be an if


statement:
if (expression_1)
{ TRUE FALSE/ELSE
if (expression_2) e1
{
statement(s); TRUE FALSE/ELSE
e2
}
else
statement(s) statement(s)
{
statement(s);
statement(s)
}
}
else
{
statement(s);
}
11
if-else Chain
if-else Chain
 Frequently occurred construct:
if (expression_1)
{
statement(s);
}
 This is a multiway decision
else if (expression_2)
o The expressions are evaluated in
{
statement(s); order
} o If any expression is TRUE, the
... associated statement(s) is
else if (expression_n) executed and the whole chain is
{ terminated
statement(s);
}
else
{
statement(s);
}
12
If-else Chain
If-else Chain (Cont’d)
(Cont’d)
Example
if ((0 <= S) && (S < 1000))
{
printf("idle \n");
}
else if ((1000 <= S) && (S < 5000))
{
printf("normal running \n");
}
else if ((5000 <= S) && (S < 8000))
{
printf("fast running \n");
}
else
{
printf("overspeed \n");
} 13
Conditional Expression
Conditional Expression

 Conditional expression takes the general form:

expr1 ? expr2 : expr3;

The statement: Can be re-written as:

if (a > b) c = (a > b) ? a : b;
c = a;
else
c = b;

14
switch
switch
 The format of switch statement is:
switch (choice)
{
case choice_1:  The switch and case statements are
statement(s); used to control complex conditional
break; and branching operations
case choice_2:
statement(s);
 Execution control is transferred to the
break; statement whose case choice
... matches the value of switch
case choice_n:  The break statement forces an exit
statement(s); from the statement body after one
break;
statement is executed
default:
statement(s);
break;
} 15
switch (Cont’d)
switch (Cont’d)
Example
int i=2; int i=2;
switch(i+2) switch(i)
{ {
case 1: case 1:
printf("Case1: Value is: %d", i); printf("Case1 "); break;
case 2: case 2:
printf("Case2: Value is: %d", i); printf("Case2 "); break;
case 3: case 3:
printf("Case3: Value is: %d", i); printf("Case3 "); break;
default: default:
printf("Default: Value is: %d", i); printf("Default");
} }

int i=2;
switch(i)
{ case 3:
case 1: printf("Case3 ");
printf("Case1 "); default:
case 2: printf("Default");
printf("Case2 "); }
16
switch –– Multiple
switch Multiple Choices
Choices
 It is possible to have multiple choices per case
Example
char c;
switch (c)
{
case 'T' : case 't':
printf("This is a true case \n");
break;
case 'F' : case 'f':
printf("This is a false case \n");
break;
default :
printf("This is an invalid case \n");
break;
}
17
while Loop
while Loop

 Sequences of statements can be repeated some number of


times under the control of while and for loops.
 The format of a while loop is:
while (expression)
{
statement(s);
}

Example
int i = 0;
while (i < 10)
{
printf("value of i is %d\n", i);
i++;
}
18
do-while
do-while
 To execute a statement at least once in a loop, the do-while can
be used
do
{
statement(s);
} while (expression);

Example
int i = 0;
do
{
printf("value of i is %d\n", i);
i++;
} while (i < 10);

19
for Loop
for Loop

 The format of a for loop is:


for ( init-statement; condition; expression)
{
statement(s);
}

 The flow proceeds as follows:


1. init-statement is executed once before loop begins
2. condition is evaluated on each iteration. If it evaluates to
true, statement(s) is executed
3. expression is executed after the statement(s) each time
around the loop and the condition is then re-evaluated

20
for Loop
for Loop (Cont’d)
(Cont’d)

Example
// normal loop counter
for (i = 0; i < 10; i++)
{
printf("value of i is %d\n", i);
}

// increment steps can be anything


for (i = 0; i < 10; i += 2)
{
printf("value of i is %d\n", i);
}

21
for and
for and while
while Loops
Loops (Cont’d)
(Cont’d)

init-statement;
 Writing a for loop is the same as: while (condition)
{
statement(s);
Example expression;
}
for(i = 0; i < 10; i++)
{
printf("value of i is %d\n", i);
}
int i = 0;
while (i < 10)
{
printf("value of i is %d\n", i);
i++;
}
22
Nested Loops
Nested Loops
 Loops may be nested (embedded) inside of each other
 Any control structure (sequence, selection, or repetition) may be
nested inside of any other control structure
 It is common to see nested for loops
for ( i = 1; i < 5; i++ )
Exercise {
for ( j = 1; j < 3; j++ )
How many times is the if {
statement executed and if ( j % 2 == 0 )
what will be the output? {
printf ("O") ;
}
else
{
printf ("X") ;
}
}
printf ("\n") ;
} 23
break Statement
break Statement

 The break statement provides an early exit from for, while and
do-while as well as from switch

int i, j = 0;
const int threshold = 8;

for(i = 0; i < 10; i++)


{
if (j > threshold)
{
break;
Control moves beyond }
the end of the control j++;
structure }

24
continue Statement
continue Statement
 The continue statement can be used in while, do-while, and for
loops
 It causes the remaining statements in the body of the loop to be
skipped for the current iteration of the loop

Example

Output:
for ( i = 1; i < 10; i = i + 1 )
{ 12346789
if (i == 5) Done.
{
І
continue ;
}
printf ("%d ", i) ;
}
printf ("\nDone.\n") ;

25

You might also like