You are on page 1of 14

Introduc)on

to Programming
Further Selec)on

Control of Flow
SEQUENCE SELECTION REPETITION

SELECTION
Control structures IF IF (condi)on is true ) THEN {do this} IF ELSE IF (condi)on is true) THEN {do this} ELSE {do alterna)ve ac)on}

if in C
Selec)on statements allow the program to choose between alternate ac)ons during execu)on if (<condi)onal expression>) <statement> if (age >= 18) prinO(You can drive);

if else in C
if (condi)onal expression) <statement1> else <statement2> if (age >= 18) prinO(You can drive); else prinO(Sorry, wait un)l you are 18);

Using Blocks { }
To include more than one statement to be executed if a condi)on is true, put them in a block, i.e. { } if(age >= 18) {prinO(You can drive); prinO(You can go to University); }

Rela%onal Operators
Rela%onal Operator Test Condi%on == a == b equal to != a != b not equal to < a<b less than <= a<=b less than or equal to > a>b greater than >= a>=b greater than or equal to

Selec%on Program - ATM


PROMPT Enter your pin number GET input STORE input IF pin = = input OUTPUT You can access your bank account ELSE OUTPUT You CANNOT access your account

Selec%on Program - ATM


#include <stdio.h> main() { int input; Int PIN = 1234; prinO(please enter your PIN number); scanf(%d, &input); if (input == PIN) { prinO( Your PIN number was correct, you can access your bank account); } else { prinO(Your PIN number was INCORRECT, you cannot access your account); } }

Driving Program
PROMPT Enter your age GET age STORE age IF age >= 18 OUTPUT You can drive ELSE OUTPUT Wait un)l you are 18

Driving Program in C
#include <stdio.h> main() { int age; prinO(please enter your age); scanf(%d, &age); if (age >= 18) { prinO( You can drive); } else { prinO(Wait un)l you are 18 to drive); } }

Boolean Logical Operators


&& (AND), || (OR), !( NOT) The operands of boolean operators are boolean expressions. Their evalua)on results in TRUE or FALSE

Truth Table for Boolean Operators


P Q !P P && Q true true false true true false false false false true true false false false true false P||Q true true true false

Boolean operators - examples


if((age >=5) && (age <=16)) prinO(You are of school age); if((age < 5) || (age > 16)) prinO(You are not of school age); If !(age >= 18) prinO(You are NOT old enough to drive);

You might also like