Conditions
and
Boolean
Expressions
If
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("Give me an integer: ");
int n = get_int("Give me an integer: ");
if (n > 0)
printf("You picked a positive number!\n"
}
Boolean Expressions
<
<=
> Evaluates to either
>= true or false.
==
!=
!
Combining Boolean
Expressions
Logical OR: ||
if (x < 0 || x > 100)
{
printf("invalid\n");
}
Logical AND: &&
if (x >= 0 && x <= 100)
{
printf("valid\n");
}
If... Else
int main(void)
{
int n = get_int("Give me an integer: “);
if (n > 0)
{
printf("You picked a positive number!\n");
}
else
{
printf("You picked a negative number!\n");
}
}
If... Else if... Else
int main(void)
{
int n = get_int(“Enter a Number: “);
if (n > 0)
{
printf("You picked a positive number!\n");
}
else if (n < 0)
{
printf("You picked a negative number!\n");
}
else
{
printf("You picked 0!\n");
}
}
int main(void)
{
int n = get_int("Enter your grade: ");
if (n > 90)
{
printf("You got an A!\n");
}
if (n > 80)
{
printf("You got a B!\n");
}
if (n > 70)
{
printf("You got a C!\n");
}
}
Switch Statements
int main(void)
{
int n = get_int("Give me an integer between 1 and 3: ");
switch (n)
{
case 1:
printf("You picked a low number.\n");
break;
case 2:
printf("You picked a medium number.\n");
break;
case 3:
printf("You picked a high number.\n");
break;
default:
printf("Invalid.\n");
break;
}
}
Ternary Operator
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Give me an integer: ");
string s = (n > 100) ? "high" : "low";
printf("You picked a %s number!\n", s);
}
Loops
For Loops
for (initialization; condition; update)
{
execute this code
}
Initialize Update
variable(s) variable(s)
Check
condition
Execute code
Exit loop
in body
if false if true
Example #1
Prints “Nice one!” ten times
for (int i = 0; i < 10; i++)
{
printf(“Nice One!\n");
}
Example #2
Converts a lowercase string to
uppercase
char name[] = "milo";
for (int i = 0, j = strlen(name); i < j; i++)
{
name[i] = toupper(name[i]);
}
While Loops
while (condition)
{
execute this code
}
if false Check if true
condition
Execute code
Exit loop
in body
Example #3
Counts down from 10 to 0
int count = 10;
while (count >= 0)
{
printf("%i\n", count)
count--;
}
Example #4
Calculates string length
string s = get_string();
int length = 0;
while (s[length] != '\0')
length++;
Do While Loops
do
{
execute this code
}
while (condition);
if true
if false
Execute code Check Exit loop
in body condition
Example #5
Reprompts until user enters a
positive number
int input;
do
{
input = get_int("Enter a positive number:");
}
while (input < 1);
Functions
Inputs Output
Why Functions?
- Organization
- Simplification
- Reusability
A Function Definition
int cube(int input)
{
int output = input * input *
input;
return output;
}
Header
function name parameter list
return type int cube(int input)
{
int output = input * input * input;
return output;
}
Body
#include <stdio.h>
int cube(int input); Function prototype
int main(void)
{
int x = 2;
printf("x is %i\n", x);
x = cube(x); Function call
printf("x is %i\n", x);
}
int cube(int input)
{
int output = input * input * input;
return output;
}
cube()'s locals
cube()'s parameters
main()'s locals
main()'s parameters
#include <stdio.h>
void swap(int a, int b);
int main(void)
{
int x = 1;
int y = 2;
swap(x, y);
printf("x is %i\n", x);
printf("y is %i\n", y);
}
void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
Arrays
0 1 2 3 4 5
Creating an Array
<data type> name[<size>];
Example:
int temperature[3]; 0 1 2
temperature[0] = 65;
temperature[1] = 87;
temperature[2] = 30; 65 87 30
OR
int temperature[] = { 65, 87,
30 };
Accessing Array Elements
0 1 2
65 87 30
for (int i = 0; i < 3; i++)
{
printf("%d\n",
temperature[i]);
}
#include <stdio.h>
#include <cs50.h>
#define CLASS_SIZE 30
int main(void)
{
// declare array
int scores_array[CLASS_SIZE];
// populate array
for (int i = 0; i < CLASS_SIZE; i++)
{
printf("Enter score for student %d: ", i)
scores_array[i] = GetInt();
}
}
Where's the bug?
string class[3] = { "Sam", "Jess", "Kim" };
for (int i = 0; i <= 3; i++)
{
printf("%s\n", class[i]);
}
Multidimensional Arrays
0,0 0,1 0,2
char board[3][3];
board[1][1] = 'o';
x x
1,0 1,1 1,2
board[0][0] = 'x';
o
board[2][0] = 'o';
board[0][2] = 'x';
2,0 2,1 2,2
o
Accessing Multidimensional
Array Elements
// print out all elements
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
printf("%c", board[i]
[j]);
printf("\n");
}