Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.

Similar presentations


Presentation on theme: "C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions."— Presentation transcript:

1 C Language 1 Program Looping

2 C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions Increment / decrement operators Increment / decrement operators break / continue statements break / continue statements scanf( ) formats scanf( ) formats

3 C Language3 Repetition Repetition - multiple execution of the same statement(s) (while, do while, for)

4 C Language4 Repetition // show repetition main() { // variable declarations // code statements while ( this == TRUE ) { statement1; // TRUE, keep doing it !!! } statement2; } FALSE (done)

5 C Language5 Equality Operators equal to == (a == b) not equal to != (a != b) Do not confuse = with = = A terrible language design construct

6 C Language6 Relational Operators less than < (a < b) less than or equal <= (a <= b) greater than > (a > b) greater than or equal >= (a >= b)

7 C Language7 Equality or Relational Expressions for example: a == b result of comparison is always an int result of comparison has value 0 or 1 0 is considered false (FALSE) 1 is considered true (TRUE)

8 C Language8 Increment / Decrement Operators increment ++ (a++ or ++a) decrement -- (a-- or --a) can be prefix or postfix add or subtract 1 from the operand

9 C Language9 Prefix vs. Postfix ++a ++a if a equals 0 if a equals 0 b = ++a b = ++a then a equals 1 and b equals 1 then a equals 1 and b equals 1 use new value use new value a++ a++ if a equals 0 if a equals 0 b = a++ then a equals 1 and b equals 0 use old value Prefix : Postfix :

10 C Language10 Repetition Statements Repeat statement(s) a controlled number of times can be infinite (then out of control!) Controlled by a test expression, typically a relational expression, which must be enclosed in parentheses ( ) Control a single statement or a compound statement (i.e. a block) for while do while

11 C Language11 for Good for counter-controlled or indexed loops Index variable or loop counter (typically an integer variable), controls the loop Condition is tested at the top of the loop (i.e. before executing the loop statements) Has initialization and update expressions for manipulating the loop counter

12 C Language12 for // show for statement main() { // variable declarations int i,// loop index sum = 0; // sum // for statement for ( i = 0 ; i < 10 ; i++ ) { sum = sum + i ; }

13 C Language13 while Good for flag or sentinel value conditions Uses while keyword Condition is tested at the top of the loop (i.e. before executing the loop statements)

14 C Language14 while // show while statement main() { // variable declarations int flag = 1;// flag variable // while statement while ( flag == 1 ) { statements; }

15 C Language15 do while Good for flag or sentinel value conditions, when you need to execute the loop statements at least once Uses do and while keywords (as a pair) Condition is tested at the bottom of the loop (i.e. after executing the loop statements) Requires a semicolon ; at the end

16 C Language16 do while // show do while statement main() { // variable declarations int flag = 0;// flag variable // do while statement do { statements; } while ( flag == 1 ) ; }

17 C Language17 while and for // show for as while statement main() { // variable declarations int I;// loop index int sum = 0; // sum // for loop implemented with while i = 0 ; while ( i < 10 ) { sum = sum + i ; i++ ; }

18 C Language18 break and continue break statement, used for early termination of a loop continue statement, used for early continuation of a loop

19 C Language19 break // show break statement while ( 1 ) { statements1; break ; statements2; } statements3;

20 C Language20 continue // show continue statement while ( 1 ) { statements1; continue ; statements2; } statements3;

21 C Language21 scanf() Formatted input from the console Scanf(control string, variable argument address list) The control string can be text embedded with conversion specifications (these begin with a % and end with a conversion character) The variable argument address list is a comma separated list of addresses of variables, and each argument must correspond to one conversion specification in the control string Uses the address operator (&) for simple variables

22 C Language22 scanf() Examples scanf(“%d”, &average) scanf(“%i %i %i”, &month, &day, &year) scanf(“%i:%i:%i”, &month, &day, &year) scanf(“%f”, &somefloatvalue)

23 C Language23 scanf( ) Conversion Characters d or i – integer in decimal o – integer in octal x – integer in hexadecimal e, f, g – floating point c – single character s – character string consult C Standard Library documentation for details

24 C Language24 WJK Programming Maxims Always indent to show the logic of your program. Always indent to show the logic of your program. Never use assignments in a conditional test (e.g. if ( total = sum ) or while( this = next ) Never use assignments in a conditional test (e.g. if ( total = sum ) or while( this = next )


Download ppt "C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions."

Similar presentations


Ads by Google