Selection - Introduction to Computer Science and Programming - Lecture S, Lecture notes of Computer Security

The key points in computer security which should be know as a beginner are given below:Selection, Making Choices, Execution, Days, Relational Operators, Boolean Values, Expressions, True or False, Must Evaluate, Conditional Expressions

Typology: Lecture notes

2012/2013

Uploaded on 04/22/2013

satheesh
satheesh 🇮🇳

4.5

(11)

85 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Selection
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Selection - Introduction to Computer Science and Programming - Lecture S and more Lecture notes Computer Security in PDF only on Docsity!

Selection

What is Selection?

leap year?

February has 29 days

February has 28 days

yes

no

 Making choices in the flow of execution of a program.

 (^) e.g., if it is a leap year then there are 29 days in February – otherwise there are 28

Conditional Strings

 You cannot compare two strings like other types of

data.

 (^) i.e., “Hello” == “Hello” may not work!

 Instead, use methods in String class.

 (^) “Hello”.compareTo(“Hello”) == 0  (^) “Hello”.equals (“Hello”)  (^) aString.compareTo (“somevalue”) == 0  (^) aString.equals (“somevalue”)

The “if” statement

if ( boolean_expression )

statements …

else

statements …

Another example

if (year < 2000) { fearFactor = 1; } else { fearFactor = 0; } if (fearFactor == 1) { System.out.println (“be afraid – be very afraid”); } else { System.out.println (“it’s OK! no Y2K bug!”); }

Shortcuts I

year-end?

yes Bonus cheque !!

no

 No else part.

if (numberOfStudents > 150)

System.out.println (“Full!”);

Problem

 Write a program to calculate the minimum of 4

integers without using the Math methods. Use a

sequence of if statements.

Nested “if” statement

String password = Keyboard.readString(); if (password.equals (realPassword)) { if (name.equals (“admin”)) { loggedIn = superPrivileges = true; } } else { System.out.println (“Error”); }

Multiway selection

 (^) Multiple conditions, each of which causes a different block of statements to execute.  (^) Can be used where there are more than 2 options.

if ( condition1 ) { statements … } else { if ( condition2 ) { statements … } else … }

“if” ladder

 Just a nicer way to write multiway selection.

if (operation == ‘a’) { answer = first + second; } else if (operation == ‘s’) { answer = first – second; } else if (operation == ‘m’) { answer = first * second; }

Problem

 Write a program to sort 3 integers and output the

sorted order symbolically. For example, if the

numbers are {a=3, b=6, c=5}, then the sorted order is

“a c b”.

 Use nested if statements.

Problem

 Write a program to calculate your final grade and

symbol in CSC1015F based on marks for theory tests,

exam, practicals and practical tests. This must

include the possibility of DPR.

Boolean operators

Boolean

Algebra

Java Meaning

AND && true if both parameters are true

OR || true if at least one parameter is

true

NOT! true if parameter is false;

false if parameter is true;

Operator precedence

 Now that we have seen how operators can be mixed,

we need precedence rules for all operators

 (^) () (highest precedence – performed first)  (^)!  (^) * / %  (^) + -  (^) < <= > >=  (^) == !=  (^) &&  (^) ||  (^) = (lowest precedence – performed last)