While and Dowhile expression action do action while

  • Slides: 7
Download presentation
While and Dowhile ( expression ) { action } do { action } while

While and Dowhile ( expression ) { action } do { action } while ( expression );

Expression and Action • Expression can be logical comparison, for example x > 0,

Expression and Action • Expression can be logical comparison, for example x > 0, x==2, or x • Expression can also be x+y, or simply just && y. 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("%dn", x); x = y + 1; }

Compile and Run • The C program pg 64. c is compiled as gcc

Compile and Run • The C program pg 64. c is compiled as gcc pg 64. c -o pg 64 • The executable pg 64 is run pg 64 • Assuming the file ph. in exists with 7. 2 e-5 1. 4 e-6 2. 9 e-11 6. 9 e-8 2. 8 e-10 • The output will be in a file ph. out with Molar concentration = 7. 200000 e-05 p. H = 4. 142667 Acidic Molar concentration = 1. 400000 e-06 p. H = 5. 853872 Acidic Molar concentration = 2. 900000 e-11 p. H = 10. 537602 Nonacidic

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

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

When to Use the Semicolon "; " • Semicolon "; " is used to

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

When to Use the Braces { … } • A brace pair {…} is

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"); } printf("B");

Reading/Home Working • Read Chapter 2, Pages 42 to 65. • Work on Problems

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.