WUSTL CSE 131 - sp14_2 (43 pages)

Previewing pages 1, 2, 3, 20, 21, 22, 41, 42, 43 of 43 page document View the full content.
View Full Document

sp14_2



Previewing pages 1, 2, 3, 20, 21, 22, 41, 42, 43 of actual document.

View the full content.
View Full Document
View Full Document

sp14_2

171 views


Pages:
43
School:
Washington University in St. Louis
Course:
Cse 131 - Computer Science I
Computer Science I Documents

Unformatted text preview:

1 3 Conditionals and Loops Introduction to Programming in Java An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 1 14 19 03 29 18 PM A Foundation for Programming any program you might want to write objects functions and modules graphics sound and image I O arrays conditionals and loops Math primitive data types text I O assignment statements last lecture equivalent to a calculator 2 A Foundation for Programming any program you might want to write objects functions and modules graphics sound and image I O arrays conditionals and loops Math primitive data types to infinity and beyond text I O assignment statements 3 Conditionals If Statement The if statement A common branching structure Evaluate a boolean expression If true execute some statements If false execute other statements if boolean expression statement T can be any sequence of statements else statement F boolean expression true statement T false statement F The else part is optional 5 If Statement The if statement A common branching structure Evaluate a boolean expression If true execute some statements If false execute other statements 6 If Statement Ex Take different action depending on value of variable public class Flip public static void main String args if Math random 0 5 System out println Heads else System out println Tails 7 If Statement Examples 8 The While Loop 9 While Loop Powers of Two Ex Print powers of 2 that are 2N N 6 0 1 2 3 4 5 6 1 2 4 8 16 32 64 int i 0 int v 1 while i N System out println i v i i 1 v 2 v 10 While Loop The while loop A common repetition structure Evaluate a boolean expression If true execute some statements Repeat loop continuation condition statement 2 while boolean expression statement 1 loop body statement 2 boolean expression true statement 1 false 11 Powers of Two public class PowersOfTwo public static void main String args last power of two to print int N 6 int i 0 loop control counter int v 1 current power of two while i N System



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view sp14_2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?