Lecture4

Lecture4 - ECE15: Introduction to Computer Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples of Statements in C Conditional Statements The if-else Conditional Statement The switch-case Conditional Statement The (exp)? x:y Conditional Expression Iteration or Loop Statements The while Loop Statement The do-while Loop Statement The for Loop Statement Jump Statements: break , continue , and goto Example: Primality Testing Lecture Unit 4 ECE15: Introduction to Computer Programming Using the C Language 2 Outline of this Lecture
Background image of page 2
Lecture Unit 4 ECE15: Introduction to Computer Programming Using the C Language 3 Examples of Statements Every program in C is a sequence of compiler directives, de- clarations, and statements . There are six kinds of statements in C: expression statement, compound statement, conditional statement, iteration (loop) statement, labeled statement, and jump statement. So far, we have seen just one of these --- the expression statement . An expression statement is just an expression followed by for ex- ample: x = a+b; ; { t = x; x = y; y = t; } a+b; x++; ; Here is one more kind of statements --- the compound statement also called a block . A compound statement is any sequence of statements and declarations enclosed in {...} for example: int main() { bool drunk = True ; if (!drunk) printf( "Can drive!\n" ); } { a = x++; if (a == x) { printf( "Wow?" ); return 1 ; } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples of Statements in C Conditional Statements The if-else Conditional Statement The switch-case Conditional Statement The (exp)? x:y Conditional Expression Iteration or Loop Statements The while Loop Statement The do-while Loop Statement The for Loop Statement Jump Statements: break , continue , and goto Example: Primality Testing Lecture Unit 4 ECE15: Introduction to Computer Programming Using the C Language 4 Outline of this Lecture
Background image of page 4
Lecture Unit 4 ECE15: Introduction to Computer Programming Using the C Language 5 The if Conditional Statement The syntax of the if conditional statement is as follows: If the logical value of the expression is True the statement is executed. If the logical value of the expression is False the statement is skipped, and the program flow continues with the next statement. In most cases, but not always, the statement is a compound statement . if (b*b < 4 *a*c) { printf( "Quadratic equation has no real roots!" ); return 1 ; } if ( expression ) statement 0 expression statement 0 Example :
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Unit 4 ECE15: Introduction to Computer Programming Using the C Language 6 The if-else Conditional Statement if ( expression ) statement1 else statement2 if ( tomorrow_is_rainy == ‘y’ ) printf("Take your umbrella.\n"); else printf("Leave the umbrella at home.\n"); expression st1 0 0 st2 The syntax of the if-else conditional state- ment is as follows: If the logical value of the expression is True statement1 is executed, then statement2 is skipped and the program flow continues.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/15/2010 for the course ECE ECE15 taught by Professor Vardy during the Fall '08 term at UCSD.

Page1 / 31

Lecture4 - ECE15: Introduction to Computer Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online