7
Week 3 Lesson 2 Homework: Loop example:

mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Embed Size (px)

Citation preview

Page 1: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Week 3 Lesson 2 Homework:Loop example:

Page 2: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

ID and Password Check

Page 3: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Multiple DecisionsThis program shows how to use if...else if ... else if ... else.. to choose exactly one action from a list of many choices.Each else if... is only tested when the previous if.. is not true.  Whenever one of the checks succeeds, the rest of thechoices are not tested.

if( name.equals("Admin") && pw.equals("master") ) { output("Hello master"); }The if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello master".  Otherwise it goes on and checks the next combination: else if ( name.equals("RONALD") && pw.equals("clown") ) { output("Ha..ha..hallo"); }If these are correct, it prints "Ha..ha..hallo".  Otherwise, it goes on to check the next combination: else if ( name.equals("secret") && pw.equals("magic") ) { output("Okay"); }

If this still doesn't work, then it goes on to the default result: else { output("Rejected! Who are you?"); }So if none of the combinations match the input, it prints "Rejected..." for any other input.That is the DEFAULT result - that is the response for ANY other combination.

We use if .. else if .. else if .. else .. when we want EXACTLY ONE action to be chosen from many possibilities.

Case-Sensitive

Using String.equals to compare strings is case-sensitive.  That means that CAPITAL letters and small letters must allmatch exactly.  This is a normal procedure for inputting user-names and passwords.

If you prefer to do a NON-case-sensitive comparison, use .equalsIgnoreCase( ) instead of just .equals( ).

if( name.equalsIgnoreCase("Admin") && pw.equalsIgnoreCase("master") ) { output("Hello master"); }This would accept "admin" and "master" typed with any combination of capital and small letters:

           Admin , Master    or   ADMIN , MASTER   or  ADMIN , masTER , etc.

Page 4: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Practice

Download the program, run it and try out various combinations of user-names and passwords.Check that the program responds correctly.

Page 5: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Add another User-name & password combination for yourself.

Change the program so that it also asks for a PIN (personal ID number).  For example:

Ronald , clown , 999

Page 6: mitekstuf.files.wordpress.com · Web viewThe if.. command checks whether the user name is correct (Admin) AND the password is correct (master).If these are correct, it outputs "Hello

Then you need 3 input commands, as well as checking THREE things in

each all the if.. commands. 

But if wrong pin is inserted: