Top Banner

Click here to load reader

of 53

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.

Dec 31, 2015

Download

Documents

Lawrence Moore
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Introduction to Programming

Dr. Yang, QingXiong(with slides borrowed from Dr. Yuen, Joe)

LT3: Conditional StatementsCS2311 Computer ProgrammingOutlines2if statementSimpleNested

Boolean logic

switch statement

Outcomes3Work out the boolean value of a logic expression

Express decision making logic in boolean logic

Using if / switch statement to express conditional instructions

Syntax Summary4Keywordsif, else, switch, case, default,Punctuators(){}:? :Operators==, !, !=, >, >=, 1&&bless than or equal to =Equality operatorsequal to==not equal to!=Logical operatorslogical not!logical and&&logical or||PS: Expressions with above operators have a true or false value.Precedence & associativity of popular operators11Operator precedence (high to low)Associativity*/%Left to right+-Left to right=Left to right==!=Left to right&&Left to right||Left to right?:Right to leftConditional statements12In decision making process, logical value can be used to determine the actions to take.

E.g. If it is raining, then bring an umbrellaIf AC1 canteen is too crowded, then go to AC2/AC3 for lunch

In programming, certain statements will only be executed when certain condition is fulfilled. We call them conditional statements.Conditional statement: if13One statement will be executed if the condition is trueSyntax:if (logical expression) statement;

Only one statement will be executedC++ example:

cin >>x;if (x=70 and mark