Difference between revisions of "Control Structures"

From CompSciWiki
Jump to: navigation, search
m (Change italic to bold in intro)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{1010Chapter|Introduction='''Control Structures''' are blocks of code that force the computer to make a decision.  Sometimes you will want your program to perform different tasks depending on certain [[boolean]] conditions.  For example if you were writing a game you would probably find it very useful to be able to know when the user has won the game so that you could show a high scores page.
+
{{1010Chapter
 +
|ChapterNum=4
 +
|Picture=Wiki_sign01.jpg
 +
|Introduction='''Control Structures''' are blocks of code that force the computer to make a decision.  Sometimes you will want your program to perform different tasks depending on certain [[Common Primitive Variables#Primitive Type boolean|boolean]] conditions.  For example if you were writing a game you would probably find it very useful to be able to know when the user has won the game so that you could show a high scores page.
  
If control structures were not used then programs would begin at the first line and excecute every line of code in order, giving the same output every time. Imagine how boring ''Tetris'' would be if there was only one piece!  Control structures allow you to change the order of execution, jumping from one section of code to another as needed. }}
+
If control structures were not used then programs would begin at the first line and excecute every line of code in order, giving the same output every time. Imagine how boring ''Tetris'' would be if there was only one piece!  Control structures allow you to change the order of execution, jumping from one section of code to another as needed.
 
+
__NOTOC__
+
  
 +
|Body=
 
==[[The If-Statement]]==
 
==[[The If-Statement]]==
  
Line 13: Line 15:
 
==[[Nesting]]==
 
==[[Nesting]]==
  
==[[Review Questions and Exercises]]==
+
==[[Control Structures Review Questions and Exercises|Review Questions and Exercises]]==
 
+
==Review Questions and Exercises==
+
The solutions to these exercises can be found [[Control Structures Solutions|here]].
+
 
+
===Review Questions===
+
#Name the 3 different logical operators. What are the benefits of using logical operators?
+
#What is the proper way in Java to compare strings? Why?
+
#What are the results of the following logical operations?
+
## 'a' != 'b'
+
## (127-3*9) >= (97+8/4)
+
## "Hello" == "Hello"
+
#Write the truth table for the expression (X || Y) && Z
+
 
+
 
+
===Exercises===
+
1. Look at the code below. Clean it up so it uses the proper logical operators and proper use of if-else's.
+
 
+
<PRE>
+
if (x == 1)
+
{
+
  if (y == 1)
+
  {
+
    if (!!!!true)
+
    {
+
      System.out.println("X and Y are 1!");
+
    }
+
  }
+
}
+
if (x == 2)
+
{
+
  System.out.println("X is 2!");
+
}
+
if (x < 1)
+
{
+
  System.out.println("X is under 1 or over 2!");
+
}
+
if (x > 2)
+
{
+
  System.out.println("X is under 1 or over 2!");
+
}
+
</PRE>
+
 
+
 
+
 
+
2. Is the result of the following statement true or false?
+
 
+
<PRE>
+
String s1 = "test";
+
String s2 = "TEST";
+
String s3 = "test";
+
 
+
if (!(!(s1.equals(s2) && s1.equals(s3)) || s2.equals(s3)))
+
  System.out.println("The result is true");
+
else
+
  System.out.println("The result is false");
+
</PRE>
+
  
3. Write a program that will prompt a user to enter an integer. The program will determine whether the number is even or odd and print out the result. It will print out a different result if the number is negative. The program will continue until the user enters a 0. Also note that if the user enters a 0, the program will not print out anything other than that the program has finished.
+
|PChapterNum=3
 +
|PChapterLink=[[Calling Methods]]
 +
|NChapterNum=5
 +
|NChapterLink=[[Loops]]
 +
}}
  
4. Write a program that will prompt the user to enter a string.  The program will print out whether or not the first character is a vowel or not.
 
  
 
{{Category:COMP 1010}}
 
{{Category:COMP 1010}}
{{Category:Java}}
 

Latest revision as of 13:57, 7 December 2011


Wiki 1010 Table of Contents

Wiki sign01.jpg

Chapter 4

Control Structures are blocks of code that force the computer to make a decision. Sometimes you will want your program to perform different tasks depending on certain boolean conditions. For example if you were writing a game you would probably find it very useful to be able to know when the user has won the game so that you could show a high scores page.

If control structures were not used then programs would begin at the first line and excecute every line of code in order, giving the same output every time. Imagine how boring Tetris would be if there was only one piece! Control structures allow you to change the order of execution, jumping from one section of code to another as needed.

  Write a Program a Day Case Studies

The If-Statement

The If-Else Statement

Conditions

Nesting

Review Questions and Exercises




Chapter 3: Calling Methods Table of Contents Chapter 5: Loops



Template loop detected: