cpsc233 branching - university of...
TRANSCRIPT
cpsc233 Branching
Monir Zaman
Hint: solve the problem for n=10 first, then try with n=50 , n=100
Step 1: Take input from user and assign it to x Also, print the value of x // You need java.util.Scanner Scanner sob=new Scanner(System.in); int x=sob.nextInt();
Lexicographic and Alphabetical Order
• Lexicographic ordering is the same as ASCII ordering
“….ABCDEFGHIJKLMNOPQRSTUVWXYZ…… abcdefghijklmnopqrstuvwxyz…”
int compareTo(String) s1.compareTo(s2)
returns 0 if s1=s2
-ve if s1 appears before than s2
+ve if s1 appears after s2
3-3
String s1=“iPhone5”;
if(s1.compareTo(“iphone5”)>0)
System.out.println( “s1 is behind”);
String s1=“iPhone5”;
if(s1.compareTo(“iphone5”)>0)
System.out.println( “s1 is behind”);
else
System.out.println( “s1 is ahead”);
String s1=“iPhone5”;
if(s1.compareTo(“iphone5”)>0)
System.out.println( “s1 is behind”);
else if(s1.compareTo(“iphone5”)<0)
System.out.println( “s1 is ahead”);
else
System.out.println( “Two strings are equal”);
String s1=“iPhone5”;
if(s1.compareToIgnoreCase(“iphone5”)>0)
System.out.println( “s1 is behind”);
else if(s1.compareToIgnoreCase(“iphone5”)<0)
System.out.println( “s1 is ahead”);
else
System.out.println( “Two strings are equal”);
Logical operators
• &&
• ||
• !
• &
• |
long[] num={3, 6, 8}; for(int j=0;j<num.length;j++){ if (num[j]%2 ==0 && num[j]>5) System.out.println(":)"); else System.out.println(":("); }
Truth Tables
3-10 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
logical operators
c=2; e=55;
if ( c==1 & e++ < 100 )
d=100;
System.out.println(e);
Short circuit logical operators
c=2; e=55;
if ( c==1 && e++ < 100 )
d=100;
System.out.println(e);
Logical operators
c=1; e=55;
if ( c==1 | e++ < 100 )
d=100;
System.out.println(e);
Short circuit logical operators
c=1; e=55;
if ( c==1 || e++ < 100 )
d=100;
System.out.println(e);
3-15 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Precedence and
Associativity Rules
3-16 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
int a=4; double b=8.0; a == (int) b / 2
3-17 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
int a=4; double b=8.0; a == (int) b / 2
(a == ( ((int) b) / 2 ) ) Binding>
The Comma in for Statements
• A for loop can contain multiple initialization actions separated with commas; it is best to declare all variables outside the for statement
• A for loop can contain multiple update actions, separated with commas, It is even possible to eliminate the loop body in this way
• However, a for loop can contain only one Boolean expression to test for ending the loop
• Loops that check for equality or inequality (== or !=) are especially prone to ‘infinite loop’ error and should be avoided if possible
int k,b,sum;
for(sum=0,i=0;i<5;sum+=i++) ;
3-18 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
Debugging Example (1 of 9) • The following code is supposed to present a
menu and get user input until either ‘a’ or ‘b’ is entered.
3-19 Copyright © 2012 Pearson Addison-Wesley. All rights
reserved.
String s = "";
char c = ' ';
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Enter 'A' for option A or 'B' for option B.");
s = keyboard.next();
s.toLowerCase();
c = s.substring(0,1);
}
while ((c != 'a') || (c != 'b'));