cse 1310 - introduction to computers & programming
TRANSCRIPT
![Page 1: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/1.jpg)
CSE 1310 - Introduction toComputers & Programming
Expressions, Statements, and Basic Control
Dr. Alex Dillhoff
University of Texas at Arlington
Fall 2020
![Page 2: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/2.jpg)
Expressions and Statements
An expression is a sequence of tokens thatevaluates to a numerical quantity.
I 1 + 1;
I y = 3;
I 1;
I 46 + 8 / (20 / (1 * 0.5));
I x * 2;
![Page 3: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/3.jpg)
Expressions and Statements
An expression is a sequence of tokens thatevaluates to a numerical quantity.
I 1 + 1;
I y = 3;
I 1;
I 46 + 8 / (20 / (1 * 0.5));
I x * 2;
![Page 4: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/4.jpg)
Expressions and Statements
A statement is anything that can be evaluated bythe compiler.
I int x;
I x;
I y = 3;
I int y = x * 2;
I ;
![Page 5: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/5.jpg)
Expressions and Statements
A statement is anything that can be evaluated bythe compiler.
I int x;
I x;
I y = 3;
I int y = x * 2;
I ;
![Page 6: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/6.jpg)
lvalues and rvalues
An lvalue is an expression with a location inmemory.
I x
I myVar
I v_ptr
I avg_height
![Page 7: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/7.jpg)
lvalues and rvalues
An lvalue is an expression with a location inmemory.
I x
I myVar
I v_ptr
I avg_height
![Page 8: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/8.jpg)
lvalues and rvalues
An rvalue is any expression not representing somelocation in memory.
I x + 3;
I 5;
I -1 * y + (-x);
I 1 / 0.5;
![Page 9: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/9.jpg)
lvalues and rvalues
An rvalue is any expression not representing somelocation in memory.
I x + 3;
I 5;
I -1 * y + (-x);
I 1 / 0.5;
![Page 10: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/10.jpg)
lvalue and rvalue Examples
I lvalues can be used on either side of anassignment.
I rvalues can only be used on the right side of anassignment.
![Page 11: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/11.jpg)
Blocks and Compound Statements
A block is a variable number of statementscontained between braces.
{
x = 3;
int y = x * 0.5;
}
![Page 12: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/12.jpg)
Blocks and Compound Statements
Groups of statements contained within a block arecalled compound statements.
{
float x = 0.1;
{
int y = x * 3;
printf("%d %d\n", x, y);
}
}
![Page 13: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/13.jpg)
Blocks and Scope
Variables defined in a block are called localvariables.
{
int x = 1;
{
float y = 2.9;
printf("x = %d\n", x);
}
printf("%d\n", y);
}
![Page 14: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/14.jpg)
Blocks and Scope
What is the printed value of y in the previousexample?
y does not exist.
Local variables only exist within their scope.
![Page 15: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/15.jpg)
Blocks and Scope
What is the printed value of y in the previousexample?
y does not exist.
Local variables only exist within their scope.
![Page 16: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/16.jpg)
A note of formatting
In the previous examples, it was easy to tell whichstatements belonged to which block. Thestatements of each block were indented.
The number of spaces to use for indentation canchange from group to group, but it is generallyconsidered good formatting to indent statementssuch that they visually represent their scope.
![Page 17: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/17.jpg)
Adding Control – if-else Statements
The syntax of control statements is very similarthroughout many languages. Basic control can beadded to the program through if and else
statements.
![Page 18: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/18.jpg)
Adding Control – if-else Statements
scanf("%d", &a);
if (a == 0) {
printf("The value is 0.\n");
} else {
printf("The value is not 1.\n");
}
![Page 19: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/19.jpg)
Adding Control - if-else Statements
Example: Checking input commands from a menu.
char choice;
scanf("%c", &choice);
if (choice == 'q') {
printf("Exiting the program.\n");
} else if (choice == 'l') {
printf("Load option selected.\n")
} else if (choice == 'p') {
printf("Print option selected.\n");
} else {
printf("Invalid command.\n");
}
![Page 20: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/20.jpg)
if-else Examples
I Checking scanf
I Even/odd check
I ID check
![Page 21: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/21.jpg)
Adding Control - switch Statement
Another way to represent the previous example.
switch (choice) {
case 'q':
printf("Exiting the program.\n");
case 'l':
printf("Load option selected.\n");
case 'p':
printf("Print option selected.\n");
default:
printf("Invalid command.\n");
}
![Page 22: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/22.jpg)
Adding Control - switch Statement
The previous example prints everything else belowthe option that is selected... WHY?
The statements are executed sequentially within theblock.
![Page 23: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/23.jpg)
Adding Control - switch Statement
The previous example prints everything else belowthe option that is selected... WHY?
The statements are executed sequentially within theblock.
![Page 24: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/24.jpg)
Breaking out of switchWe can prevent sequential execution with break.
switch (choice) {
case 'q':
printf("Exiting the program.\n");
break;
case 'l':
printf("Load option selected.\n");
break;
case 'p':
printf("Print option selected.\n");
break;
default:
printf("Invalid command.\n");
break;
}
![Page 25: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/25.jpg)
Breaking out of switch
A break statement allows the control to exit theswitch statement.
It also allows us to break out of loops (more on thatlater).
![Page 26: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/26.jpg)
switch Examples
I Pyramid
![Page 27: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/27.jpg)
Relational Operators
I <= is less than or equal to
I >= is greater than or equal to
I == is equal to
I != is not equal to
I < is less than
I > is greater than
![Page 28: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/28.jpg)
Relational Operators – Examples
I Simple examples
I Complex assignments
I Using relational operators with controlstatements
![Page 29: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/29.jpg)
Equality and Assignment
Common mistake when programming:
if (a = 1) {
printf("Is this true?\n");
}
![Page 30: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/30.jpg)
Operator Precedence
CPPREFERENCE.COM Chart
![Page 31: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/31.jpg)
Logical Operators
I ! logical NOT
I && logical AND
I || logical OR
![Page 32: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/32.jpg)
Logical operators are used to test truth valuesbetween expressions or to negate an expression.
I a * b && c - a
I a || b
I !TRUE
![Page 33: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/33.jpg)
Short-circuit Evaluation
Expressions are evaluated from left to right, if acondition is met then the resulting expressions arenot evaluated.
Example 1
expr1 || expr2
If expr1 is true, then expr2 will not be evaluated.
![Page 34: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/34.jpg)
Short-circuit Evaluation
Expressions are evaluated from left to right, if acondition is met then the resulting expressions arenot evaluated.
Example 2
expr1 && expr2
If expr1 is false, then expr2 will not be evaluated.
![Page 35: CSE 1310 - Introduction to Computers & Programming](https://reader031.vdocuments.us/reader031/viewer/2022021913/620f74125fc4170f6b6807f9/html5/thumbnails/35.jpg)
Logical Operators – Examples
I Verifying input
I Checking a successful result