ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
CS111 Lab
Decisions, Decisions
Instructor: Michael Gordon
If statements
? Structure of if statement:
? if (true or false to check) {
instruction or output;
}
OR (if only one instruction:
? if (true or false to check) instruction or
output;
Common Checks
? x>y ¡°x is greater than y?¡±
? x<y ¡°x is less than y?¡±
? x==y ¡°x is equal to y?¡±
? x!=y ¡°x is not equal to y?¡±
? x>=y ¡°x is greater than or equal to y?¡±
? x<=y ¡°x is less than or equal to y?¡±
? x%2==0 ¡°x is divisible by two (even).¡±
Using else
? if (true or false to check) {
instruction or output;
}
else {
instruction or output;
}
else if
? if (true or false to check) {
instruction or output;
}
else if (another check) {
instruction or output;
}
else {
instruction or output;
}
Compound Comparisons
? Use parentheses if clarity is needed.
? Use && for AND and || for OR.
? Examples:
if (x>0 && x<100) ¡°x is greater than 0 and
less than 100.¡±
If (x<0 || x>100) ¡°x is less than 0 or greater
than 100.¡±
Negation
? You can also check the opposite value of
a condition using !
? Example:
? if (!(x%2==0)) ¡°x is odd (not even).¡±

More Related Content

Similar to If statements (8)

If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
?
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
doncreiz1
?
Javascript comparison and logical operators
Javascript comparison and logical operatorsJavascript comparison and logical operators
Javascript comparison and logical operators
Jesus Obenita Jr.
?
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
?
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
ShivamChaturvedi67
?
11ScriptsAndConditionalsinpython1234.ppt
11ScriptsAndConditionalsinpython1234.ppt11ScriptsAndConditionalsinpython1234.ppt
11ScriptsAndConditionalsinpython1234.ppt
sairapathan2
?
11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt
Anjali127411
?
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
Stephen JE Ventura
?
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
?
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
doncreiz1
?
Javascript comparison and logical operators
Javascript comparison and logical operatorsJavascript comparison and logical operators
Javascript comparison and logical operators
Jesus Obenita Jr.
?
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
?
11ScriptsAndConditionalsinpython1234.ppt
11ScriptsAndConditionalsinpython1234.ppt11ScriptsAndConditionalsinpython1234.ppt
11ScriptsAndConditionalsinpython1234.ppt
sairapathan2
?
11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt
Anjali127411
?

More from Michael Gordon (13)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture classRaspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
Michael Gordon
?
Strings2
Strings2Strings2
Strings2
Michael Gordon
?
Strings1
Strings1Strings1
Strings1
Michael Gordon
?
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 LabIntroduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
Michael Gordon
?
Strings
StringsStrings
Strings
Michael Gordon
?
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
Michael Gordon
?
Arrays
ArraysArrays
Arrays
Michael Gordon
?
For loops
For loopsFor loops
For loops
Michael Gordon
?
Arithmetic
ArithmeticArithmetic
Arithmetic
Michael Gordon
?
Variables
VariablesVariables
Variables
Michael Gordon
?
Output
OutputOutput
Output
Michael Gordon
?
Word cloud
Word cloudWord cloud
Word cloud
Michael Gordon
?
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
Michael Gordon
?

Recently uploaded (15)

Carabao Cup Final Newcastle¡¯s Struggles Explained.docx
Carabao Cup Final Newcastle¡¯s Struggles Explained.docxCarabao Cup Final Newcastle¡¯s Struggles Explained.docx
Carabao Cup Final Newcastle¡¯s Struggles Explained.docx
Eticketing General
?
Gage Simmons Personal Brand Exploration!
Gage Simmons Personal Brand Exploration!Gage Simmons Personal Brand Exploration!
Gage Simmons Personal Brand Exploration!
gfsimmons
?
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT
?
Studies Explore Factors Influencing Marathon Run Performance
Studies Explore Factors Influencing Marathon Run PerformanceStudies Explore Factors Influencing Marathon Run Performance
Studies Explore Factors Influencing Marathon Run Performance
Erin Scholtes
?
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup Tickets
?
Jan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Jan Hoff 2008 UKSCA Norwegian 4x4 ProtocolJan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Jan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Iain
?
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYATANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D
?
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
atleuzhan
?
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
TIDA Sports
?
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docxPATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
JoannaMarieSabid2
?
How the NBA is Embracing Artificial intelligence Technology
How the NBA is Embracing Artificial intelligence TechnologyHow the NBA is Embracing Artificial intelligence Technology
How the NBA is Embracing Artificial intelligence Technology
Richard Schwartzenberger
?
Golf, A Timeless Pursuit of Precision.pdf
Golf, A Timeless Pursuit of Precision.pdfGolf, A Timeless Pursuit of Precision.pdf
Golf, A Timeless Pursuit of Precision.pdf
Sebastian Wyczawski
?
cornhole-210717141519-220905211814-8974f330.pptx
cornhole-210717141519-220905211814-8974f330.pptxcornhole-210717141519-220905211814-8974f330.pptx
cornhole-210717141519-220905211814-8974f330.pptx
nucampusrec
?
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca..."Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
chadlomas2011
?
Sander Ganzevles_Making a workout-min.pdf
Sander Ganzevles_Making a workout-min.pdfSander Ganzevles_Making a workout-min.pdf
Sander Ganzevles_Making a workout-min.pdf
Mark Rauterkus
?
Carabao Cup Final Newcastle¡¯s Struggles Explained.docx
Carabao Cup Final Newcastle¡¯s Struggles Explained.docxCarabao Cup Final Newcastle¡¯s Struggles Explained.docx
Carabao Cup Final Newcastle¡¯s Struggles Explained.docx
Eticketing General
?
Gage Simmons Personal Brand Exploration!
Gage Simmons Personal Brand Exploration!Gage Simmons Personal Brand Exploration!
Gage Simmons Personal Brand Exploration!
gfsimmons
?
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT JUGA MENYEDIAKAN INFO PERTANDINGAN SEPAK BOLA MENARIK !!!
SINGASLOT
?
Studies Explore Factors Influencing Marathon Run Performance
Studies Explore Factors Influencing Marathon Run PerformanceStudies Explore Factors Influencing Marathon Run Performance
Studies Explore Factors Influencing Marathon Run Performance
Erin Scholtes
?
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup 2026 Canada Secures Top Talent Jebbison Commits to National Te...
FIFA World Cup Tickets
?
Jan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Jan Hoff 2008 UKSCA Norwegian 4x4 ProtocolJan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Jan Hoff 2008 UKSCA Norwegian 4x4 Protocol
Iain
?
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYATANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D MERUPAKAN SLOT GACOR GAMPANG JACKPOT & PENYEDIA ID PRO 2025 TEPERCAYA
TANGKI4D
?
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
§ã§â§ã §â§å§ã§ã§Ü§Ú§Û §ñ§Ù§í§Ü§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Ó§Þ§Þ§Ó.pptx
atleuzhan
?
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
? Ultimate Sports Quiz: Test Your Sports Knowledge & Challenge Friends!
TIDA Sports
?
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docxPATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
PATHFIT 4- OUTDOOR AND ADVENTURE ACT.docx
JoannaMarieSabid2
?
How the NBA is Embracing Artificial intelligence Technology
How the NBA is Embracing Artificial intelligence TechnologyHow the NBA is Embracing Artificial intelligence Technology
How the NBA is Embracing Artificial intelligence Technology
Richard Schwartzenberger
?
Golf, A Timeless Pursuit of Precision.pdf
Golf, A Timeless Pursuit of Precision.pdfGolf, A Timeless Pursuit of Precision.pdf
Golf, A Timeless Pursuit of Precision.pdf
Sebastian Wyczawski
?
cornhole-210717141519-220905211814-8974f330.pptx
cornhole-210717141519-220905211814-8974f330.pptxcornhole-210717141519-220905211814-8974f330.pptx
cornhole-210717141519-220905211814-8974f330.pptx
nucampusrec
?
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca..."Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
"Stealth Angler Camo Fishing Pack"Here is a title 40 characters long for a ca...
chadlomas2011
?
Sander Ganzevles_Making a workout-min.pdf
Sander Ganzevles_Making a workout-min.pdfSander Ganzevles_Making a workout-min.pdf
Sander Ganzevles_Making a workout-min.pdf
Mark Rauterkus
?

If statements

  • 2. If statements ? Structure of if statement: ? if (true or false to check) { instruction or output; } OR (if only one instruction: ? if (true or false to check) instruction or output;
  • 3. Common Checks ? x>y ¡°x is greater than y?¡± ? x<y ¡°x is less than y?¡± ? x==y ¡°x is equal to y?¡± ? x!=y ¡°x is not equal to y?¡± ? x>=y ¡°x is greater than or equal to y?¡± ? x<=y ¡°x is less than or equal to y?¡± ? x%2==0 ¡°x is divisible by two (even).¡±
  • 4. Using else ? if (true or false to check) { instruction or output; } else { instruction or output; }
  • 5. else if ? if (true or false to check) { instruction or output; } else if (another check) { instruction or output; } else { instruction or output; }
  • 6. Compound Comparisons ? Use parentheses if clarity is needed. ? Use && for AND and || for OR. ? Examples: if (x>0 && x<100) ¡°x is greater than 0 and less than 100.¡± If (x<0 || x>100) ¡°x is less than 0 or greater than 100.¡±
  • 7. Negation ? You can also check the opposite value of a condition using ! ? Example: ? if (!(x%2==0)) ¡°x is odd (not even).¡±