0% found this document useful (0 votes)
63 views90 pages

Section1 For cs50

This document provides an overview of topics to be covered in Week 1 of CS50 including variables and types, input and printing, functions, loops, and conditionals. It introduces the concept of variables and data types, demonstrates getting input from the user and printing output, and explains while loops, for loops, and functions through examples. The goal is to introduce foundational programming concepts needed for Problem Set 1.

Uploaded by

wwwron456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views90 pages

Section1 For cs50

This document provides an overview of topics to be covered in Week 1 of CS50 including variables and types, input and printing, functions, loops, and conditionals. It introduces the concept of variables and data types, demonstrates getting input from the user and printing output, and explains while loops, for loops, and functions through examples. The goal is to introduce foundational programming concepts needed for Problem Set 1.

Uploaded by

wwwron456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

This is CS50

Week 1
Carter Zenke
Preceptor
[email protected]
Today
• Variables and Types
• Input and Printing
• Functions, Loops, and Conditionals
• Problem Set 1
Variables and
Types
How would you explain what a
variable is, in a single sentence?
0
calls

0
calls

1
calls

2
calls

3
calls
A variable is a name for some
3 value that can change.
calls
int calls = 3;
3
calls
int calls = 3;

name
3
calls
int calls = 3;

type
3
calls
int calls = 3;

value
3
calls
int calls = 3;

assignment operator
3
calls
int calls = 3;

type name value


3
assignment operator

"Create an integer named calls that gets the value 3."


country_code
int country_code = 65;
65
country_code
int country_code = 65;
65

"Create an integer named country_code that gets the value 65."


country_code
int country_code = 65;
65
Why have data types?
country_code
int country_code = 65;
01000001
country_code
int country_code = 65;
65
01000001
country_code
char country_code = 65;
'A'
01000001
int calls = 4;
calls = calls + 2;
calls = calls - 1;
calls = calls * 2;
calls = calls / 2;
int calls = 4;
calls += 2;
calls -= 1;
calls *= 2;
calls /= 2;

"Syntactic sugar"
int calls = 4;
calls = calls + 2;
calls
calls = calls - 1;
calls = calls *
calls = calls /
2;
2; ?
int calls = 4;
calls = calls + 2;
calls
calls = calls - 1;
calls = calls *
calls = calls /
2;
2; 5
int calls = 4;
calls = calls + 1;
calls
calls = calls - 2;
calls = calls *
calls = calls /
3;
2; ?
int calls = 4;
calls = calls + 1;
calls
calls = calls - 2;
calls = calls *
calls = calls /
3;
2; 4
"Truncation"
Input and
Printing
int calls = get_int("Calls: ");
calls
int calls = get_int("Calls: ");

function call
?
calls
int calls = 4;

value
?
calls
int calls = 4;

value
4
calls
int calls = 4;
printf("calls is %i\n", calls);
4
"calls is 4"
calls
int calls = 4;
printf("calls is %i\n", calls);
4
format code
Types and format codes
• int (%i)
• float (%f)
• char (%c)
• string (%s)
Hello, world!
• Let's write a "Hello, world" program to complete
the rst step of Problem Set 0.
fi
Hello, me!
• Let's write a "Hello, me" program to complete the
second step of Problem Set 0.
Hello, contacts!
• Let's write a program that stores (and prints out!)
a user's contact information
Functions, Loops,
and Conditionals
While Loops
int j = 0;
while (j < 4)
{
printf("#");
j++;
}
printf("\n");
int j = 0;
while (j < 4)
{
printf("#"); ####
j++;
}
printf("\n");
int j = 0;
j
while (j < 4)
{
printf("#"); 0
j++;
}
printf("\n");
int j = 0;
j
while (j < 4)
{
printf("#"); 0
j++;
}
printf("\n");
int j = 0;
j
while (j < 4)
{
printf("#"); 0
j++;
}
printf("\n");
#
int j = 0;
j
while (j < 4)
{
printf("#"); 1
j++;
}
printf("\n");
#
int j = 0;
j
while (j < 4)
{
printf("#"); 1
j++;
}
printf("\n");
#
int j = 0;
j
while (j < 4)
{
printf("#"); 1
j++;
}
printf("\n");
##
int j = 0;
j
while (j < 4)
{
printf("#"); 2
j++;
}
printf("\n");
##
int j = 0;
j
while (j < 4)
{
printf("#"); 2
j++;
}
printf("\n");
##
int j = 0;
j
while (j < 4)
{
printf("#"); 2
j++;
}
printf("\n");
###
int j = 0;
j
while (j < 4)
{
printf("#"); 3
j++;
}
printf("\n");
###
int j = 0;
j
while (j < 4)
{
printf("#"); 3
j++;
}
printf("\n");
###
int j = 0;
j
while (j < 4)
{
printf("#"); 3
j++;
}
printf("\n");
####
int j = 0;
j
while (j < 4)
{
printf("#"); 4
j++;
}
printf("\n");
####
int j = 0;
j
while (j < 4)
{
printf("#"); 4
j++;
}
printf("\n");
####
int j = 0;
j
while (j < 4)
{
printf("#"); 4
j++;
}
printf("\n");
####\n
For Loops
for (int j = 0; j < 4; j++)
{
printf("#");
}
printf("\n");
for (int j = 0; j < 4; j++)
{
printf("#"); ####
}
printf("\n");
j
for (int j = 0; j < 4; j++)
{
printf("#"); 0
}
printf("\n");

#
j
for (int j = 0; j < 4; j++)
{
printf("#"); 1
}
printf("\n");

##
j
for (int j = 0; j < 4; j++)
{
printf("#"); 2
}
printf("\n");

###
j
for (int j = 0; j < 4; j++)
{
printf("#"); 3
}
printf("\n");

####
j
for (int j = 0; j < 4; j++)
{
printf("#"); 4
}
printf("\n");

#### \n
for (int j = 0; j < 4; j++)
{
printf("#");
}
printf("\n");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("#");
}
printf("\n");
}
for (int i = 0; i < 4; i++)
{ ####
for (int j = 0; j < 4; j++)
{ ####
}
printf("#");
####
}
printf("\n"); ####
Mario
• Let's write a program to print a
right-aligned pyramid.
Functions
int get_int(string prompt)
{
// Get int from user
}
get_int

int get_int(string prompt)


{
// Get int from user
}
get_int

int get_int(string prompt)


{
prompt
// Get int from user
}
get_int

int get_int(string prompt)


{
prompt int
// Get int from user
}
get_int

int get_int(string prompt)


{
prompt 2
// Get int from user
}
get_int

int get_int(string prompt)


{
prompt 4
// Get int from user
}
get_int

int get_int(string prompt)


{
prompt int
// Get int from user
}
int height = get_int("Height: ");
void print_row(int bricks)
{
// Print row of bricks
}
print_row

void print_row(int bricks)


{
// Print row of bricks
}
print_row

void print_row(int bricks)


{
bricks
// Print row of bricks
}
print_row

void print_row(int bricks)


{
bricks
// Print row of bricks
}
print_row(4);
void print_row(int spaces, int bricks)
{
// Print row of bricks
}
void print_row(int spaces, int bricks)
{
// Print row of bricks
}

print_row

bricks

spaces
This is CS50
Week 1

You might also like