# Ch5 - CSEB114: ogr amming Chapt er 5 St r uct ur ed Pr pr...

This preview shows pages 1–10. Sign up to view the full content.

Click to edit Master subtitle style 8/4/2008 prepared by NI, edited by CSEB 114: Principle of Programming Chapter 5: Structured Programming

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

View Full Document
8/4/2008 prepared by NI, edited by Objectives In this chapter you will learn about: Sequential structure Selection structure if if … else switch Repetition Structure while do… while for
8/4/2008 prepared by NI, edited by Sequential Structure Statements are executed one by one until the end of the program is reached. A group of statements that executed sequentially which is usually grouped (bracketed) by { } is known as Compound Statement

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

View Full Document
8/4/2008 prepared by NI, edited by Sequential Structure - example int main(void) { int count = 0; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); count++; printf(“Count = %d\n”, count); return 0; }
8/4/2008 prepared by NI, edited by Selection Structure In selection structure, the program is executed based upon the given condition . Only instructions that satisfy the given condition are executed. There are 3 types of selection structure: if One alternative if…else Two alternatives nested if. .else Multiple alternatives

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

View Full Document
8/4/2008 prepared by NI, edited by Selection structure:  if Syntax : if (condition) Statement; The statement is only executed if the condition is satisfied. Example: if (score >= 60) printf(“Pass!!\n”); § In the example above, the word “Pass!!” will only be printed out if score is larger than or equal to 60. If A condition is an expression that can return true or false (usually involving the use of an operator). Note that there is no semicolon (;) after the if statement. If there is one, that means the if statement and the printf() statement are 2 different statements and they will both get executed sequentially.
8/4/2008 prepared by NI, edited by Selection structure:  if… else Syntax : if (condition) statement1; else statement2; § If the condition is satisfied, statement1 will be executed. Otherwise, statement2 will get executed. Example : if (score >= 60) printf(“Pass!!\n”); else

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

View Full Document
8/4/2008 prepared by NI, edited by Nested  if… else  statements A nested if…else statement is an if…else statement with another if…else statements inside it. Example : if (score >= 90) printf(“A\n”); else if (score >= 80) printf(“B\n”); else if (score >= 70) printf(“C\n”); else if (score >= 60) printf(“D\n”) else
8/4/2008 prepared by NI, edited by Plurality of Statements In the examples that we have seen so far, there is only one statement to be executed after the if statement.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 06/01/2011 for the course ECON 101 taught by Professor Yip during the Spring '11 term at Al-Quds University.

### Page1 / 28

Ch5 - CSEB114: ogr amming Chapt er 5 St r uct ur ed Pr pr...

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

View Full Document
Ask a homework question - tutors are online