CS50 — Week One Notes, part 2

CS50 — Week One Notes, part 2

I had to break these up into two different days because it was information overload.

Side note to self: If I’m ever in my workspace and I accidentally made a never-ending program that’s trying to force me into infinite loops of the same banal GetInt() function, use ctrl-z!!!

Conditional Statements

  • conditional branches/expressions/statements allow programs to make decisions depending on variable values or user input

if (boolean expression)

{

}

  • (mouse down) (x < 10) etc.
  • boolean expression evaluates to true? all lines of code between curly braces will execute from top to bottom
  • boolean expression false? those lines of code will not execute

if (boolean expression)

{

}

else

{

}

  • boolean expression evaluates to true? all lines of code between 1st set of curly braces will be executed, top to bottom
  • false? follow instructions in 2nd set of curly braces
  • I can create chains of mutually exclusive branches by using if, else if (* n), else with as many different boolean expressions
  • I can layer on as many if statements as I like (creating a chain of non-mutually exclusive branches), but the else will only bind to the nearest if

switch()

  • allows me to specify distinct cases rather than having to rely on boolean expressions
  • must break between each case, or I’ll “fall thru” each case and execute them all in order (unless that’s what I’m trying to do)

int x = GetInt();
switch(x)
{
case 5:
printf("Five!\n");
case 4:
printf("Four!\n");
case 3:
printf("Three!\n");
case 2:
printf("Two!\n");
case 1:
printf("One!\n");
default:
printf("Blast off!\n");
}

The ternary operator ?:

  • useful when writing very short if-else statements
these 2 code snippets act identically
int x = GetInt();
if (b == 0)
{
x = 5;
}
else
{
x = 6;
}
int x = GetInt();
x = (b == 0) ? 5 : 6;

Loops

  • allow programs to execute lines of code repeatedly (saves me from needing to copy/paste or repeat lines of code)

while (true)

{

}

  • infinite loop: lines of code between curly braces will execute repeatedly top to bottom forever (because true is always true)

while (boolean expression)

{

}

  • if boolean expression evaluates to true, all lines of code within curly braces will execute repeatedly top to bottom, until the boolean expression evaluates false

do

{

}

while (boolean expression);

  • executes all lines of code in curly braces, then checks boolean expression – if true, goes back and repeats code block until boolean exp. evaluates to false

for (int i = 0; i < 10; i++)

{

}

  • repeats the body code a specified number of times (above, for example, 10 times)
  • what the parts?!
    • the counter variable(s) (above, i) is set (above, to 0)
    • the boolean expression is checked (above, i < 10)
      • if true, body code executes
      • if false, body code does not execute
    • after getting to the end of the body code, the counter variable i is incremented (above, by 1, using the shorthand var++)
    • the boolean exp. is checked again and either goes thru the body code again or does not
  • more generally: for (start; expr; increment) where there can be more than one statement in start and increment

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s