Docsity

Understanding Control Statements in Embedded C Programming - Prof. Stephen Kuyath, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Understanding Control Statements in Embedded C Programming - Prof. Stephen Kuyath and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Embedded Systems Programming

ETEE 3285

Topic 5: Control Statements

Agenda

 Evaluate, explain, and utilize the following

control statements

 if, if/else, and if/else/else if

 while

 do/while

 for

 conditional statements

 switch/case

 break, continue, goto

if/else Statements

 The if/else statement is nearly the same as the

if statement

 It has the following format:

if ( expression ) true_statement ; else else_ statement ;

or

if ( expression ) { true_statement 1; true_statement 2; ……} else { else_ statement 1; else_ statement 2; ……}

If the expression evaluates to TRUE, then the true_statement (s ) is(are) executed. If the expression evaluates to FALSE then the else _ statement(s) is(are) executed.

if/else/else if Statements

 You may also need to have an if/else/else if

structure

if ( expression1 ) true_statement 1; else if ( expression2 ) true_statement 2; else if ( expression3 ) true_statement 3; else else_ statement ;

If expression1 evaluates to TRUE, then the true_statement1 (s ) is(are) executed. If expression evaluates to FALSE then the next expression ( expression2 ) is evaluated. If expression evaluates to TRUE, then the true_statement2 (s ) is(are) executed. If none of the expressions evaluate to TRUE then the else_statement (s ) is(are) executed.

if/else/else if Statements

 Example: what is the value of z after this code executes

int x, y, z; x = 10; y = 50; z = 5;

if ( x == y ) z = x/2; else if ( x > y ) z = x * 2 ; else z = x;

if/else/else if Statements

 Example: what is the value of z after this code executes

int x, y, z; x = 10; y = 50; z = 5;

if ( x == y ) z = x/2; else if ( x > y ) z = x * 2 ; else z = x;

x is not equal to y so this statement is skipped

if/else/else if Statements

 Example: what is the value of z after this code executes

int x, y, z; x = 10; y = 50; z = 5;

if ( x == y ) z = x/2; else if ( x > y ) z = x * 2 ; else z = x;

x is not equal to y so this statement is skipped

x is not greater than y so this statement is skipped

so z = 10

if/else/else if Statements

 Example 2: does the printf statement get executed?

int x, y; x = 10; y = 20;

if ( x = y ) printf (“Blah, blah, blah”) ; : :

while Loop

 The while loop has the following format:

while ( expression ) statement ;

or

while ( expression ) { statement 1; statement 2; statement 3; etc. }

When program execution enters the while loop, the expression is evaluated. If the expression evaluates to TRUE the statement (s) is(are) executed. When all statements are executed, the program will be to this process again (evaluate the, expression, …… etc). If the expression evaluates to FALSE the statement (s) is(are) skipped and execution continues at the statement(s) immediately following the while loop structure.

while Loop

 Example: how many times does the printf statement get

executed

while ( 1 ) printf (“Blah, blah, blah”) ; : :

do/while Loop

 The do/while loop has the following format:

do statement ; while ( expression ) ;

or

do { statement 1; statement 2; statement 3; etc. } while ( expression ) ;

The do/while loop is very similar to the while loop except that the expression is evaluated at the end of the loop. When program execution enters the do/while loop, the statement (s) is(are) executed. If the expression evaluates to TRUE the program continues at the beginning of the do/while loop. This process continues as long as the expression evaluate to TRUE. The important difference between while and do/while is that the do/while statement (s) is(are) ALWAYS executed at least once.

do/while Loop

 Example: how many times does the printf statement get

executed

do printf (“Blah, blah, blah”) ; while (0);

Once (0 is considered FALSE)

for Loop

 Example: what is the value of y after this code executes?

int y = 15; for (int x = 1; x = = 10; x++) --y;

for Loop

 Example: what is the value of y after this code executes?

int y = 15; for (int x = 1; x = = 10; x++) --y;

y = 14

x is initialized to 1 and then incremented

once for each time through the loop (y is

decremented in the “loop”). But, the first

test of x is false (x ==10), so the loop

terminates.