Variable names like function names must be sequences of lower case or upper

Variable names like function names must be sequences

This preview shows page 2 - 4 out of 6 pages.

Variable names, like function names, must be sequences of (lower case or upper case) letters, underscores and digits, and not start with a digit. Assignments allow one to modify the value of a variable. Function test() contains 3 assignment statements. The value of a variable can also be modified with an increment or decrement operator : for instance, the statements ++j in main() increments the value of j (by 1 ). COMP9021 (Principles of Programming) Notes 3.0: A partial overview of C 2014 session 1 8 / 22
Image of page 2

Subscribe to view the full document.

Tests main() can perform a number of tests thanks to a number of if statements and one if-else statement , and test() can also perform tests thanks to one if statement . The result of a test is either true or false. If true, the statement or statements that make up the body of the if statement or if clause (in the case of an if-else statement) are executed, otherwise they are skipped and in the case of an if-else statement, the statement or statements that make up the body of the else clause are executed. A value of 0 is interpreted as false and causes a test to fail; any other value is interpreted as true and causes a test to pass. The unary boolean operator ! changes a value of 0 to 1 and a nonzero value to 0 . For instance, in main() , if test(..., ...) returns true (that is, 1 ) then !test(..., ...) evaluates to 0 and the test if (!test(..., ...)) fails. COMP9021 (Principles of Programming) Notes 3.0: A partial overview of C 2014 session 1 9 / 22 Repetitions (1) main() and test() can execute a sequence of statements repeatedly thanks to some for statements or for loops , and thanks to some while statement or while loop . A for loop has an initialisation , a test , an update , and a body . The first iteration of a for loop starts by executing the initialisation part and then passing the test, while every other iteration starts by executing the update part and then passing the test. Never failing the test of an executing for statement causes the program to loop , in which case it might be necessary to send this running process a signal (often, Control C) to kill it. For instance, for (int i = 1; i <= max; ++i) is the beginning of a for statement whose body is expected to be executed max times, that is, 3,142 times, because it contains no statement that can modify the value of i or cause the program to break out of the loop. COMP9021 (Principles of Programming) Notes 3.0: A partial overview of C 2014 session 1 10 / 22 Repetitions (2) A while loop only has a test and a body. In test() the test of the while loop is passed as long as i evaluates to a nonzero value. For the program not to loop, it is necessary that the value of i be changed in the body of the loop, which is the case thanks to the last statement: the number of times the loop is expected to be executed is at most equal to the number of digits in the decimal representation of i (that is, the integral part of the log of base 10 of i plus 1). Being caught in an infinite while loop is as undesirable as being caught in an infinite for loop and is prone to the same effects, with the same potential remedies.
Image of page 3
Image of page 4
  • Fall '19
  • 11

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes