# Skipping parts of if loop (Coin toss Program)

• October 5th, 2012, 03:12 PM
ChicoTheMan94
Skipping parts of if loop (Coin toss Program)
Hey guys, here's my code:
Code java:

```/* Paulino Rosado 10/3/2012 Computer statistics for eight coin tosses */ import java.util.*;   class Ch4Pa2 { public static void main(String[] args) { int heads=0, tails=0; double percentageHeads, percentageTails; char tossresult; String tempstring;   Scanner keyboard = new Scanner(System.in);   System.out.println("For each coin toss enter either h for heads or t for tails");   for(int i=1; i<=8; i++) { System.out.println("Toss: "+i); tempstring = keyboard.next(); tossresult = tempstring.charAt(0);   if(tossresult == 'h') { i = i+1; heads = heads + 1; } else if(tossresult == 't') { i = i+1; tails = tails + 1; } else { i= i-1; System.out.println("Incorrect toss. You must enter either h or t"); } }   System.out.println("Number of heads: "+heads); System.out.println("Number of tails: "+tails); percentageHeads =(double)heads/8; percentageTails = (double)tails/8; System.out.println("Percent heads: "+percentageHeads); System.out.println("Percent tails: " +percentageTails); } }```
The problem is this is the output:
Code :

```For each coin toss enter either h for heads or t for tails Toss: 1 h Toss: 3 b Incorrect toss. You must enter either h or t Toss: 3 a Incorrect toss. You must enter either h or t Toss: 3 t Toss: 5 t Toss: 7 h Number of heads: 2 Number of tails: 2 Percent heads: 0.25 Percent tails: 0.25 Press any key to continue . . .```
As you can see, it's only going through the loop 4 times instead of 2! It is counting in 2s, and I don't really understand why :/
• October 5th, 2012, 03:38 PM
Zaphod_b
Re: Skipping parts of if loop (Coin toss Program)
Quote:

Originally Posted by ChicoTheMan94
...
As you can see, it's only going through the loop 4 times instead of 2! It is counting in 2s, and I don't really understand why :/

If you have really studied the code and don't understand how the program produces the output that you are seeing, here's a suggestion:

Why don't you "play like" you are the computer?

How's how to play:

Get a pencil and some paper. Really. Pencil. Paper. (You remember those, right?)

Write down initial values of all variables that are defined. Whenever there is an assignment statement that changes the value of a particular variable, cross out the old value and write down the now value.

Now, let's go:
Start at the top of the loop. Write down the value of the variable i

Now, pretend that the user enters an 'h' from the keyboard.

Follow the steps that the program will take. Write down changes that the program will make to any variable that gets a new value.

When you get to the end of the loop (on paper) go back to the top of the loop.

Write down the value of i.

Repeat until you "get it."

If you don't "get it," post again. Present the steps that you followed in your pretend-I'm-the-computer game.

Cheers!

Z
• October 5th, 2012, 03:41 PM
Norm
Re: Skipping parts of if loop (Coin toss Program)
Quote:

it's only going through the loop 4 times instead of 2
The code inside of the loop changes the value of the loop's index variable: i. That will change how many times the loop will be executed. Try debugging the code by adding println statements to show the values of variables used in the loop so you can see what the computer sees.
• October 5th, 2012, 03:52 PM
ChicoTheMan94
Re: Skipping parts of if loop (Coin toss Program)
Omg! I got it, thanks to you both. Why I added i++ to my if statements I'll never know, no duh it was adding an extra i, I did it manually! Face palming incredibly hard, feel like maybe someone drugged me before I coded this. Thanks!

Here is the completed code, works perfect!
Code java:

```/* Paulino Rosado 10/3/2012 Computer statistics for eight coin tosses */ import java.util.*;   class Ch4Pa2 { public static void main(String[] args) { int heads=0, tails=0; double percentageHeads, percentageTails; char tossresult; String tempstring;   Scanner keyboard = new Scanner(System.in);   System.out.println("For each coin toss enter either h for heads or t for tails");   for(int i=1; i<=8; i++) { System.out.println("Toss "+i+": "); tempstring = keyboard.next(); tossresult = tempstring.charAt(0);   if(tossresult == 'h') { heads = heads + 1; } else if(tossresult == 't') { tails = tails + 1; } else { i--; System.out.println("Incorrect toss. You must enter either h or t"); } }   System.out.println("Number of heads: "+heads); System.out.println("Number of tails: "+tails); percentageHeads = ((double)heads/8) * 100; percentageTails = ((double)tails/8) * 100; System.out.println("Percent heads: "+percentageHeads+"%"); System.out.println("Percent tails: " +percentageTails+"%"); } }```