Ch. 4statementsandcontrolflow

Ch. 4statementsandcontrolflow - Chapter 4 Statements and...

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

View Full Document Right Arrow Icon
Chapter 4 – Statements and Control Flow Outline Simple and Compound Statements Expression and Null Statements Flowcharts Selection Statements Sample Problem Iteration Statements Jump Statements Labeled Statements Sample Problem Extensions in Ch Switch Statement with Case of String Type Foreach Loop for String Type
Background image of page 1

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

View Full DocumentRight Arrow Icon
Simple and Compound Statements •A compound statement is a block of code enclosed with a pair of braces. block allows a set of declarations and statements to be grouped into one syntactic unit. The initializers of objects that have automatic storage duration, and the variable length array declarators of ordinary identifiers with block scope, are evaluated and the values are stored in the objects each time the declaration is reached in the order of execution, as if it were a statement, and within each declaration in the order that declarators appear. Example : int i = 10; // simple statement { // compound statement int i; i = 90; ... } printf(“%d”, i);
Background image of page 2
Expression and NULL Statements An expression statement contains an expression only. For example, i*7+4; •A null statement consisting of just a semicolon performs no operation. /* … */ ; /* … */
Background image of page 3

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

View Full DocumentRight Arrow Icon
Flowcharts Often, programmers use flowcharts to organize the order in which actions are to be performed. Common flowchart symbols are shown on the next slide.
Background image of page 4
Common Flowchart Symbols
Background image of page 5

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

View Full DocumentRight Arrow Icon
Selection Statements If Statements The syntax for an if-statement is as follows: if(expression) statement The controlling expression of an if statement should have a scalar type. The statement is executed if the expression compared is unequal to 0. The macros bool , true , and false are defined in header file stdbool.h for handling Boolean numbers. bool is typedefed as char , true is defined as 1, and false is defined as 0. Example : #include<stdbool.h> bool i = true; /* . .. */ if(i) { i = false; }
Background image of page 6
Flowchart for an if-Statement The syntax for an if-statement is as follows: if(expression) statement
Background image of page 7

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

View Full DocumentRight Arrow Icon
If-else Statements The syntax for an if-else statement is as follows: if(expression) statement1 else statement2 The controlling expression of an if -statement should have a scalar type. The statement1 is executed if the expression compared is unequal to 0, else statement2 is executed.
Background image of page 8
Flowchart of an if-else Statement The syntax for an if-else statement is as follows: if(expression) statement1 else statement2
Background image of page 9

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

View Full DocumentRight Arrow Icon
Else-if Statements - The syntax for the else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4 - Semantically, the syntax of the else-if statement is an extension of the previous if-else statement.
Background image of page 10
Flowchart of an else-if Statement The syntax for an else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4
Background image of page 11

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

View Full DocumentRight Arrow Icon
Example: /* File: ifc.c */ #include <stdio.h> int main () { int i; i = 10; if(i==2 || i == 4) { printf("i = 2 or 4\n"); } else if(i == 10) { printf("i = 10\n"); } else { printf("i = %d\n", i); } return 0; } Output: i = 10
Background image of page 12
Sample Problem
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 65

Ch. 4statementsandcontrolflow - Chapter 4 Statements and...

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

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