Week5 Combine
Week5 Combine
Expression i j k
i++, j = j + i -
++i, i = j + i -
i = 1, j = 2, k = ++i+j
Comma Operator: Exercise (2/2)
5
Expression i j k
i++, j = j + i 1 6 -
++i, i = j + i 6 5 -
i = 1, j = 2, k = ++i+j 2 2 4
Comma Operator in for
6
Statements
Most often used in for statements
Allows multiple assignment expressions to be
combined into single expression for purpose of
initializing multiple variables when entering for
loop
0 * 0 = 0
2 * 1 = 2
4 * 2 = 8
6 * 3 = 18
8 * 4 = 32
10 * 5 = 50
12 * 6 = 72
14 * 7 = 98
Comma Operator: Exercises (1/2)
8
Expression c i j
c = i++, ++j, j + i
c = (i++, ++j), j + i
c = (++i, ++j, j + i)
Comma Operator: Exercises (2/2)
9
Expression c i j
c = i++, ++j, j + i; 5 6 4
c = (i++, ++j), j + i; 4 6 4
c = (++i, ++j, j + i); 10 6 4
HIGH-LEVEL PROGRAMMING I
do-while loop by Prasanna Ghali
Pretest Loops: while and for
2
Statements
while and for statements called pretest loops
because statement executed only if loop
condition is true
int i = 0;
while (i <= 20) {
printf("%d ", i);
i += 5;
}
do
statement
while(expression);
do...while and while:
Comparison
statement
true true
expression expression statement
false false
do
statement while(expression)
while(expression); statement
do...while Loop Structure:
5
Example 1
int i = 0; int i = 0;
while (i <= 20) { do {
printf("%d ", i); printf("%d ", i);
i += 5; i += 5;
} } while (i <= 20);
? ?
do...while Loop Structure:
7
Example 3
do…while is often used for input validation
Here, user required to enter a number between
30 and 60
int score = 0;
do {
printf("Enter value between 30 and 60: ");
scanf("%d", &score);
} while (score < 30 || score > 60);
// guaranteed that score is between 30 and 60
do...while Loop Structure:
8
Example 4
do…while is often used for input validation
Here, user required to enter lowercase Latin
character
char ch = '\0';
do {
printf("Enter lowercase Latin character: ");
scanf("%c%*c", &ch);
} while (ch < 'a' || ch > 'z');
// guaranteed that ch is between 'a' and 'z'
HIGH-LEVEL PROGRAMMING I
Functions by Prasanna Ghali
References
2
function C
call function A
function B
call function B
call function C
return-type return-type
main (parameter declarations)
⋯ function-name (parameter declarations)
{ {
data declarations (local) data declarations (local)
statements statements
} }
function-return-type function-name(parameter-list);
return-type function-name(parameter-list)
{
statements
}
if (x > y) {
large = x; local variable that is
function body } else { alive only during the
large = y; execution of this
}
function
return large;
} function return value
Function Call Operator
16
int main(void) {
double value = 9.0;
double root = sqrt(value);
printf("Root of %f is %f\n", value, root);
return 0;
}
#include <stdio.h>
This is function declaration!!!
double
average(double x, double y); In both C and C++,
compiler will compile this
int main(void) { source file because it
double x = 10.0, y = 20.0;
double z = average(x, y);
finds a declaration for
this function.
printf("Average is: %d\n", z); However, the linker will
return 0; flag an error because the
} definition of function
average is not present!!!
Will Program Compile? Link? (3/3)
19
// no return, no parameters
// return, no parameters void say_hello(void) {
double pi(void) { printf("Hello!!!\n");
return 3.14159; return; // optional
} }
// no return, 1 parameter
void say_hello_alot(int count) {
for (int i = 0; i < count; ++i) {
printf("Hello!!!\n");
}
// return statement is optional since
// function is returning void
}
Calling Functions
21
int main(void) {
say_hello(); // ok: say_hello has no arguments
// parentheses represent function call operator
say_hello_alot(5); // ok: one argument
double d = pi()+56.0; // ok: pi has no arguments
pi(); // ok: caller can ignore return
// value from function
d = pi; // error: function call operator is required!!!
pi; // ok: pi without function call operator
// means “pointer to function pi”
// more on pointers later ...
d = say_hello(); // error: say_hello has no return value!!!
return 0;
}
Program Memory (1/2)
22
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i);
return 0;
}
Functions: Pass-by-Value Convention
26
(3/20)
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); main
return 0; stack frame
i ?
}
Functions: Pass-by-Value Convention
27
(4/20)
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
28
(5/20)
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); ? ? main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
29
(6/20)
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); str 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
30
(7/20)
Before call: i is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x); local variables
} in function printf
int main(void) {
int i; printf
var1 var2
i = 5; stack frame
printf("Before call: i is %d\n", i); p str q 5
foo(i); // call to function foo
printf("After call: i is %d\n", i); str 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
31
(8/20)
Before call: i is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); ? main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
32
(9/20)
Before call: i is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
33
(10/20)
Before call: i is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i; foo
i = 5; stack frame
printf("Before call: i is %d\n", i); x 5
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
34
(11/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
} var1 var2 printf
q 5 stack frame
p str
int main(void) {
int i; str 5 foo
i = 5; stack frame
printf("Before call: i is %d\n", i); x 5
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
35
(12/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h>
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i; foo
i = 5; stack frame
printf("Before call: i is %d\n", i); x 10
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
36
(13/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
} var1 var2 printf
stack frame
p str q 10
int main(void) {
int i; str 10 foo
i = 5; stack frame
printf("Before call: i is %d\n", i); x 10
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
37
(14/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i; foo
i = 5; stack frame
printf("Before call: i is %d\n", i); x 10
foo(i); // call to function foo
printf("After call: i is %d\n", i); 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
38
(15/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
39
(16/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
After call: i is 5
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i; printf
var1 var2
i = 5; stack frame
printf("Before call: i is %d\n", i); p str q 5
foo(i); // call to function foo
printf("After call: i is %d\n", i); str 5 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
40
(17/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
After call: i is 5
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i); 0 main
return 0; stack frame
i 5
}
Functions: Pass-by-Value Convention
41
(18/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
After call: i is 5
void foo(int x) {
Stack
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i);
return 0;
}
Functions: Pass-by-Value Convention
42
(19/20)
Before call: i is 5
In foo, x is 5
#include <stdio.h> In foo, x is now 10
After call: i is 5
void foo(int x) {
printf("In foo, x is %d\n", x);
x = 10; Main takeaway:
printf("In foo, x is now %d\n", x); Inter-function communication
}
uses pass-by-value semantics.
int main(void) { Using the stack, copy of
int i; argument i is passed to
i = 5;
printf("Before call: i is %d\n", i); function foo to initialize
foo(i); // call to function foo parameter x.
printf("After call: i is %d\n", i); Changes made to parameter
return 0;
} x do not affect argument i!!!
Functions: Pass-by-Value Convention
43
(20/20)
Visualization of program
#include <stdio.h>
void foo(int x) {
printf("In foo, x is %d\n", x);
x = 10;
printf("In foo, x is now %d\n", x);
}
int main(void) {
int i;
i = 5;
printf("Before call: i is %d\n", i);
foo(i); // call to function foo
printf("After call: i is %d\n", i);
return 0;
}
Pass-by-Value Convention: Example
44
Prefix-increment ++variable
Prefix-decrement --variable
lvalue Operands
4
If unambiguous,
Expression Unambiguous?
value of expression
j++*j++
++j*++j
++j*j++
j++*++j
Increment and Decrement
22
Operators: Example 4 (2/2)
Are following expressions evaluated unambiguously?
Assume variable j is of type int and is initialized
with value 1
If unambiguous,
Expression Unambiguous? value of
expression
j++*j++ No Unspecified behavior
i = 7; j = 8; k = 9;
flag = i - 7 && j++ < k;
if (flag == false) {
printf("flag is false\n");
}
printf("%d %d %d\n", i, j, k);
}
HIGH-LEVEL PROGRAMMING I
Jump Statements by Prasanna Ghali
Jump Statements
2
return statement
break statement
continue statement
General format:
return expressionoptional;
return Statement (2/2)
4
switch (year) {
case 1:
int year = 1; printf("Freshman\n");
if (1 == year) { break;
printf("Freshman\n"); case 2:
} else if (2 == year) { printf("Sophomore\n");
printf("Sophomore\n"); break;
} else if (3 == year) { case 3:
printf("Junior\n"); printf("Junior\n");
} else if (4 == year) { break;
printf("Senior\n"); case 4:
} else { printf("Senior\n");
printf("Who are you?\n"); break;
} default:
printf("Who are you?\n");
}
break Statement:
7
Flow of Control in Loop
void foo(int i) {
func(i++, i);
}
void foo(int i) {
func(i, i+1);
i++;
}
Order of Evaluation: Example 2
13
arise?
What is sequence point?
evaluation
How to write compliant code
HIGH-LEVEL PROGRAMMING I
switch Statement by Prasanna Ghali
switch Statement (1/5)
2
switch (integral-expression) {
case integral-value-1: Referred to as label
statements-1
break; Value must be known at compile-
case integral-value-2: time i.e., expression must not
statements-2 contain variables
break;
... Causes program execution to
case integral-value-n: jump to statement after switch
statements-n statement
break;
default label corresponds to a
default:
value that doesn’t match any of
statements
the other labels
}
switch Statement (4/5)
5
int year = 1;
switch (year) {
case 1:
int year = 1; printf("Freshman\n");
if (1 == year) { break;
printf("Freshman\n"); case 2:
} else if (2 == year) { printf("Sophomore\n");
printf("Sophomore\n"); break;
} else if (3 == year) { case 3:
printf("Junior\n"); printf("Junior\n");
} else if (4 == year) { break;
printf("Senior\n"); case 4:
} else { printf("Senior\n");
printf("Who are you?\n"); break;
} default:
printf("Who are you?\n");
}
switch Statement (5/5)
6