Conditional Statements - CS50 Shorts

CS50 · Beginner ·💻 AI-Assisted Coding ·8y ago

Key Takeaways

Teaches conditional statements

Full Transcript

[Music] all right so now let's talk about something really useful in programming conditional statements so conditional expressions allow your programs to make decisions and take different Forks in the road something I alluded to a little earlier depending on the values of variables or based on what the user inputs at the at the program or at the command line or if you have prompt or something like that C has a couple of different ways to express conditional expressions which we also sentence will call a conditional branch in your programs and some of these are going to look pretty familiar to you from scratch so we'll even pull them up side by side just so you can make that analogy in your head so if if it's a pretty simple conditional if if you recall from scratch on the right here you could fill in that that hexagon with a boolean expression if Mouse down or if X is less than 10 or something like that and then if X was less than 10 or if the mouse was in fact down all of the code inside of the puzzle piece would execute all the things that fit inside that C shape similarly do we have if on the left there if boolean expression which I'm just using as a substitute for one of the boolean expressions we've previously discussed open curly brace close curly brace so think of open curly brace and close curly brace as sort of analogous to the sandwich effect of the if block on the right from scratch if the boolean expression in in the if statement is true then all of the lines of code between the curly braces will execute in order from top to bottom if the boolean expression is false we'll skip over everything in between the curly braces because we're only want to go down that fork in the road if the boolean expression is true we can take this one step further with if-else so this scratch block is pretty similar to the one we saw just a second ago except it's if it takes two different paths based on what happens so if the mouse was down or if X was less than 10 we'll do everything that's in between that first for that first C otherwise if the mouse is up or X is not less than 10 we will do everything in the second set and that's analogous to what you see here for see if bully expression do the stuff between the first set of curly braces else do the stuff between the second set of curly braces so if the boolean expression is true we'll do whatever's between the first set if the boolean expression is false that would trigger the else and we would do whatever's in the second set of curly braces again top to bottom off lines in between the braces in C it's possible to create an if-else if-else chain and in fact you can have if else--if else--if else--if and so on and so on and so on in scratch this required nesting the blocks you add an if-else and then you had to put another one inside of the else and so on and so on and I got kind of nested and complicated but seeing we don't have to do that we can actually just have it be a chain like this again as you might expect all of these branches are mutually exclusive you can only ever go down one of the branch if this is true otherwise if this is true otherwise if this is true otherwise do this so all four of the branches in this example are mutually exclusive it's an if-else if-else chain it is possible though and sometimes very useful to create a chain of not mutually exclusive branches in this example only the third and fourth branches are mutually exclusive it could be that you could satisfy the first condition and you could satisfy the second condition and you could satisfy the third condition in which case you would go down the first branch then you'll go down the second branch then you would go down the third branch or perhaps you satisfy the first condition and the second condition but you don't satisfy the third condition in this case you go down the first branch and the second branch and then the fourth branch the reason for this is that the else will only bind to the nearest if so even though there's an else here that doesn't necessarily create a mutually exclusive chain of everything it's only that the expression there with boolean expression three that's the mutually exclusive with the else so it is possible and sometimes quite useful as I said to create a chain of not mutually exclusive branches let's take a look at a different kind of conditional which you have not seen before in scratch there's something called the switch statement the switch statement is kind of neat because it's a conditional statement that allows you to specify distinct cases instead of relying on boolean expressions to make decisions for you so for example let's say that I have this program and I'm asking the user to provide input to me so I hey int x equals get int and if you're not familiar yet get into a function that is also included in the cs50 library so if you pound include cs50.h you'll have access to get int and all of its cousins getfloat getstring and so on basically one get function for every data type that we've already discussed so int x equals get in basically what's happening as I'm at the terminal I'm asking the user to type in a number and here I'm just switching what I'm doing depending on what the user typed at the prompt so if they typed one I print out one and then I break if they print two if AC means they typed two I print out two and then I break it's important to break between each case because otherwise you will fall through so if I didn't have any breaks there and the user typed one what would happen is it would print one two three sorry that's kind of strange behavior right you might think so but there are actually some cases where this could be a pretty useful thing so here's another example of a switch statement where I omit the breaks but I do it on purpose so what happens here think for a second you might even want to pause the video what happens here for the user types for so I've asked the user for input and they provide the value for what gets printed when I do that on the previous slide there were breaks between all of the cases and so it would just print four and then stop but in this case it won't what will happen is you will fall through each case so in this case I'm I've organized my cases in such a way that if the user types four I will print four three two one blast off and if they typed five I would start at five and do the same thing if they typed one I would just do one blast off so in this case I'm using a switch kind of cleverly so that I do intend to fall through all the cases but generally you're probably gonna want to break between all of them unless you have a situation like this one where you're kind of leverage the fact that you'll fall through the cases without a break so that's the second of the major types of conditional statements the last of which is question mark : so I have two snippets of C code here one on the left and one on the right the one on the left should probably be pretty familiar to you I have int X and I probably should have you know ask the user for it just probably be int X equals get int or something like that and then I'm making a decision if some boolean expression is true assign X the value 5 otherwise assign X the value 6 that on the left you're probably pretty familiar for my discussion of if-else just a moment ago would you be surprised to know that the line on the right does the exact same thing so this is called question mark colon or sometimes called the ternary operator and it's pretty cool it's usually used as a cute trick but what it allows you to do is to simulate an if-else with really small really trivially short conditional branches you generally wouldn't use question mark : if you had six lines of code between each set of curly braces but if you're just making a quick decision if you're gonna do one thing or the other and it's very simple this might be an example of how do you do it with a question mark : the ternary operator so int x equals expression question mark the thing after the question mark is what X's value will be if expression is true the thing after the colon is what X's value would be if the expression was false so I'm asking myself is the expression true if it is assigned X the value 5 if it's not assigned X the value 6 again like I said this is usually just a cute trick and sometimes if you become really comfortable with it you'll do this because it looks kind of cool in your programs generally I'm presenting it to you now so that you're familiar with it if you see it but certainly know you don't have to write it and any of your code but it is something to be familiar with because you'll definitely encounter snippets of code here and there where this question mark colon syntax aka the ternary operator is used so a quick summary on what conditionals are and what the options are available to you and see you have if and if-else and if else if etc you can use boolean expressions for those to make decisions with switch statements you use discrete cases to make decisions you explicitly say if it's one or if it's two or if it's three I'll do this thing or this thing or this thing and question mark : can be used to replace very simple if-else branches or if-else chains to make your code look a little fancy I'm Doug Lloyd and this is cs50

Original Description

*** This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. *** HOW TO SUBSCRIBE http://www.youtube.com/subscription_center?add_user=cs50tv HOW TO TAKE CS50 edX: https://cs50.edx.org/ Harvard Extension School: https://cs50.harvard.edu/extension Harvard Summer School: https://cs50.harvard.edu/summer OpenCourseWare: https://cs50.harvard.edu/x HOW TO JOIN CS50 COMMUNITIES Discord: https://discord.gg/T8QZqRx Ed: https://cs50.harvard.edu/x/ed Facebook Group: https://www.facebook.com/groups/cs50/ Faceboook Page: https://www.facebook.com/cs50/ GitHub: https://github.com/cs50 Gitter: https://gitter.im/cs50/x Instagram: https://instagram.com/cs50 LinkedIn Group: https://www.linkedin.com/groups/7437240/ LinkedIn Page: https://www.linkedin.com/school/cs50/ Reddit: https://www.reddit.com/r/cs50/ Quora: https://www.quora.com/topic/CS50 Slack: https://cs50.edx.org/slack Snapchat: https://www.snapchat.com/add/cs50 Twitter: https://twitter.com/cs50 YouTube: http://www.youtube.com/cs50 HOW TO FOLLOW DAVID J. MALAN Facebook: https://www.facebook.com/dmalan GitHub: https://github.com/dmalan Instagram: https://www.instagram.com/davidjmalan/ LinkedIn: https://www.linkedin.com/in/malan/ Quora: https://www.quora.com/profile/David-J-Malan Twitter: https://twitter.com/davidjmalan *** CS50 SHOP https://cs50.harvardshop.com/ *** LICENSE CC BY-NC-SA 4.0 Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License https://creativecommons.org/licenses/by-nc-sa/4.0/ David J. Malan https://cs.harvard.edu/malan malan@harvard.edu
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CS50 · CS50 · 0 of 60

← Previous Next →
1 Hello, World: Hadi Partovi
Hello, World: Hadi Partovi
CS50
2 Content Distribution and Archival in a Digital Age
Content Distribution and Archival in a Digital Age
CS50
3 CS50 2014 - Week 1
CS50 2014 - Week 1
CS50
4 CS50 2014 - Week 3
CS50 2014 - Week 3
CS50
5 CS50 2014 - Week 0, continued
CS50 2014 - Week 0, continued
CS50
6 CS50 2014 - Week 4
CS50 2014 - Week 4
CS50
7 Week 3, continued
Week 3, continued
CS50
8 Quiz 0 Review
Quiz 0 Review
CS50
9 CS50 2014 - Week 3, continued
CS50 2014 - Week 3, continued
CS50
10 CS50 2014 - Week 7
CS50 2014 - Week 7
CS50
11 CS50 2014 - Week 7, continued
CS50 2014 - Week 7, continued
CS50
12 Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
13 Introduction to Amazon Web Services by Leo Zhadanovsky
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
14 CS50 2014 - Week 9
CS50 2014 - Week 9
CS50
15 How to Build Innovative Technologies by Abby Fichtner
How to Build Innovative Technologies by Abby Fichtner
CS50
16 Light Your World (with Hue Bulbs) by Dan Bradley
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
17 Building Dynamic Web Apps with Laravel by Eric Ouyang
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
18 CS50 2014 - CS50 Lecture by Steve Ballmer
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
19 CS50 2014 - Week 10
CS50 2014 - Week 10
CS50
20 This is CS50 with Steve Ballmer?
This is CS50 with Steve Ballmer?
CS50
21 Meteor: a better way to build apps by Roger Zurawicki
Meteor: a better way to build apps by Roger Zurawicki
CS50
22 Data Analysis in R by Dustin Tran
Data Analysis in R by Dustin Tran
CS50
23 Data Visualization and D3 by David Chouinard
Data Visualization and D3 by David Chouinard
CS50
24 CS50 2014 - Week 6
CS50 2014 - Week 6
CS50
25 Build Tomorrow's Library by Jeffrey Licht
Build Tomorrow's Library by Jeffrey Licht
CS50
26 CS50 2014 - Week 9, continued
CS50 2014 - Week 9, continued
CS50
27 Essential Scale-Out Computing by James Cuff
Essential Scale-Out Computing by James Cuff
CS50
28 iOS App Development with Swift by Dan Armendariz
iOS App Development with Swift by Dan Armendariz
CS50
29 Sam Clark Leads Yale Students on Tour to CS50 at Harvard
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
30 3D Modeling and Manufacture by Ansel Duff
3D Modeling and Manufacture by Ansel Duff
CS50
31 CS50 2014 - Week 5, continued
CS50 2014 - Week 5, continued
CS50
32 hello, world
hello, world
CS50
33 CS50 2014 - Deep Thoughts - Hash Table
CS50 2014 - Deep Thoughts - Hash Table
CS50
34 CS50 2014 - Deep Thoughts - Binary Tree
CS50 2014 - Deep Thoughts - Binary Tree
CS50
35 CS50 2014 - Deep Thoughts - Scratch
CS50 2014 - Deep Thoughts - Scratch
CS50
36 CS50 2014 - Deep Thoughts - MySQL
CS50 2014 - Deep Thoughts - MySQL
CS50
37 LaunchCode Visits CS50
LaunchCode Visits CS50
CS50
38 CS50 Live, Episode 100
CS50 Live, Episode 100
CS50
39 CS50 Field Trip to Google
CS50 Field Trip to Google
CS50
40 This is CS50 AP
This is CS50 AP
CS50
41 Week 4: Monday - CS50 2011 - Harvard University
Week 4: Monday - CS50 2011 - Harvard University
CS50
42 Week 2: Wednesday - CS50 2011 - Harvard University
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
43 Week 1: Wednesday - CS50 2011 - Harvard University
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
44 Week 11: Monday - CS50 2011 - Harvard University
Week 11: Monday - CS50 2011 - Harvard University
CS50
45 Week 3: Wednesday - CS50 2011 - Harvard University
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
46 Week 12: Monday - CS50 2011 - Harvard University
Week 12: Monday - CS50 2011 - Harvard University
CS50
47 Week 1: Friday - CS50 2011 - Harvard University
Week 1: Friday - CS50 2011 - Harvard University
CS50
48 Week 3: Monday - CS50 2011 - Harvard University
Week 3: Monday - CS50 2011 - Harvard University
CS50
49 Week 10: Wednesday - CS50 2011 - Harvard University
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
50 Week 2: Monday - CS50 2011 - Harvard University
Week 2: Monday - CS50 2011 - Harvard University
CS50
51 Week 9: Monday - CS50 2011 - Harvard University
Week 9: Monday - CS50 2011 - Harvard University
CS50
52 Week 7: Monday - CS50 2011 - Harvard University
Week 7: Monday - CS50 2011 - Harvard University
CS50
53 Week 5: Monday - CS50 2011 - Harvard University
Week 5: Monday - CS50 2011 - Harvard University
CS50
54 Week 5: Wednesday - CS50 2011 - Harvard University
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
55 Week 7: Wednesday - CS50 2011 - Harvard University
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
56 Week 8: Monday - CS50 2011 - Harvard University
Week 8: Monday - CS50 2011 - Harvard University
CS50
57 Week 9: Wednesday - CS50 2011 - Harvard University
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
58 Week 8: Wednesday - CS50 2011 - Harvard University
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
59 Week 10: Monday - CS50 2011 - Harvard University
Week 10: Monday - CS50 2011 - Harvard University
CS50
60 Week 2: Wednesday - CS50 2010 - Harvard University
Week 2: Wednesday - CS50 2010 - Harvard University
CS50

Related Reads

📰
¿Todavía existen programadores que trabajan sin inteligencia artificial o frameworks?
Learn how programmers can work without AI or frameworks and why it still matters in the industry
Medium · AI
📰
¿Todavía existen programadores que trabajan sin inteligencia artificial o frameworks?
Learn how programmers work with and without AI and frameworks in modern software development
Medium · Programming
📰
Canary Agentic Autofix With Failure Classes and Reliability Gates
Learn to evaluate GitHub's agentic code-scanning autofix preview with a focus on reliability and failure classes
Dev.to · Robin
📰
SemaDiff: Identifying Semantic-Changing Commits with Generated Code and Tests
Learn to identify semantic-changing commits using SemaDiff, a method that leverages generated code and tests to improve software repository mining
ArXiv cs.AI
Up next
How to Create ONE PAGE Website using Claude AI (FREE & FAST)
Quick Tips - Web Desiign & Ai Tools
Watch →