110 likes | 126 Views
Learn about control statements in JavaScript, including the if statement, nested if-else statement, while repetition statement, and assignment operators.
E N D
Internet & WWWHow to program Chap.(7) JavaScript: Control Statements I Dr. Qusai Abuein
(7.1) Introduction • This chapter introduces a general theoretical understanding of the structured programming. • The if statement as a control one in the program in introduced. • Some reserved words (Keywords) used in the JavaScript. Fig. 7.2. Dr. Qusai Abuein
(7.2, 7.3, 7.4) Self Study • Algorithms • Pseudocode • Control Structure Dr. Qusai Abuein
(7.5) The IF statement • Same as in the programming language you know (c++). Dr. Qusai Abuein
(7.6) If … else statement • Same as in the programming language you know (c++). • The Nested if is also the same as you expect. • The conditional operator (?:) is also supported. • document.writeln(grade >= 60 ? ”pass” : “faile”) • Nested if … else statement is also supported. Dr. Qusai Abuein
(7.7, 7.8) While repetition Statement • Example in Fig. 7.7 uses the while repetition statement using a counter (fixed value such as 10, or a number “n” read previously by the user) to control the loop (also syntax and concept as you expect). • Un initialized variables used in calculations result in logic errors and produce the value NaN. See lines 19 and 20 in Fig.7.7 . The calculation is used in line 33. Dr. Qusai Abuein
(7.9) Repetition Example • The example in Fig.7.9 uses a special value called a sentinel value (also called a signal value, a dummy value or a flag value such as “-1”) to stop or control the while loop. Dr. Qusai Abuein
(7.10) Repetition Example • The example in Fig.7.11 introduces counters in the program: • One to count number of students • Other to count passes results • And other to count filed results • Any logical errors in the program ? Dr. Qusai Abuein
(7.11) Assignment Operators • JavaScript provides several compound assignment operators. • C += 3; • C -= 3; • C *= 3; • C /= 3; • C %= 3; • See Fig. 7.11 Dr. Qusai Abuein
(7.12) Increment and decrement • The increment operator ++ is provided by JavaScript. • Also the decrement one --. • Fig.7.14 shows the difference between pre-increment and post-increment. • See Fig. 7.15 for precedence of operators. Dr. Qusai Abuein
End of Chap. (7) Thank you . Dr. Qusai Abuein