Presentation is loading. Please wait.

Presentation is loading. Please wait.

While and Do-while action action while ( ) { } do { } while ( );

Similar presentations


Presentation on theme: "While and Do-while action action while ( ) { } do { } while ( );"— Presentation transcript:

1 While and Do-while action action while ( ) { } do { } while ( );
expression action action expression

2 Expression and Action x > 0, x==2, or x && y.
Expression can be logical comparison, for example x > 0, x==2, or x && y. Expression can also be x+y, or simply just x. Action can be a single statement like x = 3; Or a sequence of statements, each ending with a semicolon ";". Braces { … } are necessary. For example: { x = x + 3; printf("%d\n", x); x = y + 1; }

3 Compile and Run gcc pg64.c -o pg64  pg64  7.2e-5 1.4e-6
The C program pg64.c is compiled as gcc pg64.c -o pg64  The executable pg64 is run pg64  Assuming the file ph.in exists with 7.2e-5 1.4e-6 2.9e e-8 2.8e-10 The output will be in a file ph.out with Molar concentration = e-05 pH = Acidic Molar concentration = e-06 pH = Molar concentration = e-11 pH = Nonacidic Molar concentration = e-08 pH = Molar concentration = e-10 pH =

4 Using Math Functions #include <math.h> main() { double x, y;
y = sin(x); . . . }

5 When to Use the Semicolon ";"
Semicolon ";" is used to terminate a declaration or statement: int i, j; i = 5; printf("%d\n", i); Omitting ";" in the above courses a syntactical error. There should be no ";" after #include < … > or similar preprocessing directives.

6 When to Use the Braces { … }
A brace pair {…} is needed when an action requires more than one statements. Syntactically, a single statement is equivalent to a group of statements enclosed in a pair of braces. Grammatically, braces are not absolutely required in if, while, etc. But adding braces make the code clearer. if(x>0) printf("A"); printf("B"); is the same as if (x>0) { printf("A"); }

7 Reading/Home Working Read Chapter 2, Pages 42 to 65. Work on Problems
Section 2.4, exercise 3, 5. Section 2.7, exercise 3, 5. Section 2.8, exercise 1. Check your answers in the back of the textbook. Do not hand in.


Download ppt "While and Do-while action action while ( ) { } do { } while ( );"

Similar presentations


Ads by Google