flow (2) - Flow of Control True and False in C Conditional...

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

View Full Document Right Arrow Icon
1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 True and False in C False is represented by any zero value . The int expression having the value 0. The floating expression having the value 0.0. The null character ‘\0’. The NULL pointer (for pointer see chap. 8). True is represented by any nonzero value . A logical expression, such as a<b, is either true or false. This expression yields the int value 1 if it is true or the int value 0 if it is false .
Background image of page 2
3 Examples int i=1, j=2, k=3 ; double x=5.5, y=7.7, z=0.0 ; i < j-k i<(j-k) 0 -i+5*j >= k+1 ((-i)+(5*j))>=(k+1) 1 x-y <= j-k-1 (x-y)<=((j-k)-1) 1 x+k+7 < y/k ((x+k)+7)<(y/k) 0 i != j 1 !! 5 !(!5) 1 ! i-j+4 ((!i)-j)+4 2 x || i j-2 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
4 Selection if switch
Background image of page 4
5 if statement Conditional Execution : if (Boolean expression) statement; else statement; Where a statement may consist of a single statement, a code block, or empty statement. The else clause is optional.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 if – logic : if (Boolean Expression) statement_1; If-else-logic : if (Boolean Expression){ compound_1 } else{ compound_2 } Conditional execution allows you write code that reacts to tested conditions. No Semi-colon Yes- For a Semi-colon
Background image of page 6
7 Example #include <stdio.h> WHY? int main ( ) { double salary, payrate; int hours, union; printf (“Please enter hours worked, payrate”, “ and union status”);
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 printf (“Enter 1 if a union member, 0 if not”); scanf (“%d%lf%d”, &hours, payrate, union); if salary = (40 * payrate) + ((1.5 * payrate) * (hours - 40)); else salary = payrate * hours; printf (“ Salary is: $ % 6.2 f , salary); }
Background image of page 8
9 Nested ifs Nested: One statement coded within another statement. Nested ifs: An nested if is an if that is the target of another if or else. Why Nested ifs? Needed to handle tasks where we have 3 or More options that are mutually exclusive. ANSI C specifies that at least 15 levels of nesting must be supported by the compiler.
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 The if-else-if ladder Linear nested If General form: OLD STYLE if ( expression ) statement ; else if ( expression ) statement ; else if ( expression ) statement ; . . . else statement ;
Background image of page 10
11 The conditions are evaluated from the top downward . As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed . That is, if all other conditional tests fail, the last else statement is performed. If the final else is not present, no action takes place if all other conditions are false.
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 E.g. You are a salesperson for the Widget Manufacturing Co. You will earn a salary bonus according to the following rules : Sales > = $50,000 earn $5,000 Sales > = $100,000 earn $15,000 Sales > = $150,000 earn $30,000 Example
Background image of page 12
13 double sales, bonus; printf (“please enter total sales”);
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.

This note was uploaded on 11/07/2011 for the course CSCE 206 taught by Professor Hurley during the Spring '11 term at Texas A&M.

Page1 / 56

flow (2) - Flow of Control True and False in C Conditional...

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