4_Control_Structures

4_Control_Structures - C Control Structures CMPE13 Cyrus...

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

View Full Document Right Arrow Icon
CMPE13 Cyrus Bazeghi C Control Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
CMPE13 C ONTROL S TRUCTURES Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iterative executing code multiple times, ending based on evaluated expression for while do-while 2
Background image of page 2
CMPE13 IF if(condition) action; condition action T F Condition is a C expression, which evaluates to TRUE (non- zero) or FALSE (zero). Action is a C statement, which may be simple or compound (a block). 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
CMPE13 E XAMPLES OF IF STATEMENTS if (x <= 10) y = x * x + 5; if (x <= 10) { y = x * x + 5; z = (2 * y) / 3; } if (x <= 10) y = x * x + 5; z = (2 * y) / 3; compound statement; both executed if x <= 10 only first statement is conditional; second statement is always executed (indentation doesn’t help) 4
Background image of page 4
CMPE13 A BOUT BRACES INDENTATION if(x <= 10) { y = x * x + 5; z = (2 * y) / 3; } if(x <= 10) { y = x * x + 5; z = (2 * y) / 3; } Good style: the braces are aligned (*) Different “ style: the braces are not aligned (*) (*) Source: My opinion 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
CMPE13 M ORE IF EXAMPLES kids += 1; if(month == 4 || month == 6 || month == 9 || month == 11) printf(“The month has 30 days. \ n”); if(x = 2) y = 5; This is a common programming error ( = instead of == ), not caught by compiler because it’s syntactically correct. always true, so action is executed! 6
Background image of page 6
CMPE13 IF S CAN BE NESTED if (x == 3) if (y != 6) { z = z + 1; w = w + 2; } if ((x == 3) && (y != 6)) { z = z + 1; w = w + 2; } is the same as. .. 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
CMPE13 IF - ELSE if(condition) action_if; else action_else; condition action_if action_else T F else allows choice between two mutually exclusive actions without re-testing condition. 8
Background image of page 8
CMPE13 M ATCHING ELSE WITH IF Else is always associated with the closest unassociated if. if(x != 10) if(y > 3) z = z/2; else z = z * 2; if(x != 10) { if(y > 3) z = z/2; else z = z*2; } is the same as. .. if (x != 10) { if (y > 3) z = z/2; } else z = z*2; and is NOT the same as. .. This code 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
CMPE13 C HAINING I F S AND E LSE S if (month == 4 || month == 6 || month == 9 || month == 11) printf (“Month has 30 days. \ n”); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf (“Month has 31 days. \ n”); else if (month == 2) printf (“Month has 28 or 29 days. \ n”); else printf (“Don’t know that month. \ n”); 10
Background image of page 10
WHILE while (test) loop_body; test loop_body T F Executes loop body as long as test evaluates to TRUE (non-zero). Note: Test is evaluated
Background image of page 11

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

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

Page1 / 41

4_Control_Structures - C Control Structures CMPE13 Cyrus...

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

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