CSCI6370: Topics in Computer Science Advanced Topics in

Download Report

Transcript CSCI6370: Topics in Computer Science Advanced Topics in

/* #####################################
Name: Justin Garvin
Program: WHAT’S THE OUTPUT #1?
######################################## */
#include <stdio.h>
int main(void)
{
printf("What is the Output?");
return 0;
}
1. What is the Output?
3. What is the
Output?
2. “What is the Output?”
4. Dr. Garvin is Awesome!
/* #####################################
Name: Justin Garvin
Program: WHAT’S THE OUTPUT #2?
######################################## */
#include <stdio.h>
int main(void)
{
printf("What is the\n Output?");
return 0;
}
1. What is the Output?
2. “What is the Output?”
3. What is the
Output?
4. What is the
Output?
/* #####################################
Name: Justin Garvin
Program: WHAT’S THE OUTPUT #3?
######################################## */
#include <stdio.h>
int main(void)
{
printf("What\nis\t the\n Output?");
return 0;
}
1. “What is the Output?”
3. What is
Output?
the
2. What
is
the
Output?
4. No idea
You learned a lot for your first reading assignment:
1. Variables: Location in the computer’s memory that
stores a value to be used by the program:
(i.e. integers, real numbers, characters,etc)
2. An example of declaring an integer called num1:
int num1; /* declares an integer num1 */
3. You can read a value for num1 that the user provides
using scanf:
scanf(“%d”, &num1);
4. You can print out num1 to the screen using printf:
printf(“%d”,num1);
5. Do arithmetic using standard *,+,-,/,etc.
You also learned a little about conditional statements (if):
if (num1 > 10)
{
printf(“num1 is greater than 10”);
}
DON’T PANIC, we are going to go over if statements and all of this
stuff for the next couple of lectures. Being a good programmer is learning
the basics and being able to apply them.
Example problem:
Your boss tells you he needs a program that
calculates force given mass and
acceleration and determines whether the
force is greater than 1500. You know the
equation for force is: force = mass*accel
1. Prompts user for mass and accel. (Use scanf)
2. Reads in the mass and acceleration from
computer screen (Use printf)
3. Calculates the force (Use algebra)
4. Determines whether the force is greater
than 1500 (Use if conditional statements)
ADDITIONAL SUMMARY SLIDES NOT SHOWN IN CLASS
Some Arithmetic Operators
C opetration
Arithmetic Algebraic
C expression
expression
operator
Addition
+
f+7
f + 7
Subtraction
–
p–c
p - c
Multiplication
*
bm
b * m
Division
/
Remainder
%
x y or
x
or x ÷ y
x / y
y
r mod s
r % s
Some Arithmetic Operators
Operator(s) Operation(s) Order of evaluation (precedence)
( )
Parentheses
Evaluated first. If the parentheses are
nested, the expression in the innermost pair is
evaluated first. If there are several pairs of
parentheses “on the same level” (i.e., not nested),
they are evaluated left to right.
*
/
%
Multiplication
Division
Remainder
Evaluated second. If there are several, they are
evaluated left to right.
+
-
Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.
Equality and Relational Operators
Standard algebraic
equality operator or
relational operator
C equality or
relational
operator
Example of
C condition
Meaning of C condition

==
x == y
x is equal to y

!=
x != y
x is not equal to y

>
x > y
x is greater than y

<
x < y
x is less than y
≥
>=
x >= y
x is greater than or equal to y
≤
<=
x <= y
x is less than or equal to y
Equality operators
Relational operators
Keywords
Special words reserved for C
Cannot be used as identifiers or variable names
Keywords
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while