Top Banner
CSE 1310 - Introduction to Computers & Programming Expressions, Statements, and Basic Control Dr. Alex Dillhoff University of Texas at Arlington Fall 2020
35

CSE 1310 - Introduction to Computers & Programming ...

Feb 18, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 1310 - Introduction to Computers & Programming ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

if-else Examples

I Checking scanf

I Even/odd check

I ID check

Page 21: CSE 1310 - Introduction to Computers & Programming ...

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 ...

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 ...

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 ...

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 ...

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 ...

switch Examples

I Pyramid

Page 27: CSE 1310 - Introduction to Computers & Programming ...

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 ...

Relational Operators – Examples

I Simple examples

I Complex assignments

I Using relational operators with controlstatements

Page 29: CSE 1310 - Introduction to Computers & Programming ...

Equality and Assignment

Common mistake when programming:

if (a = 1) {

printf("Is this true?\n");

}

Page 30: CSE 1310 - Introduction to Computers & Programming ...

Operator Precedence

CPPREFERENCE.COM Chart

Page 31: CSE 1310 - Introduction to Computers & Programming ...

Logical Operators

I ! logical NOT

I && logical AND

I || logical OR

Page 32: CSE 1310 - Introduction to Computers & Programming ...

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 ...

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 ...

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 ...

Logical Operators – Examples

I Verifying input

I Checking a successful result