A. Introduction
Sequential execution is the default control structure. But if we have only sequential flow, programs will not be as powerful. The other two flow control structures give a program the capability of making decisions and automation.
B. Watch (slides)
C. Try
Think of an example of each of the three fundamental control structures, and draw a flow chart to illustrate the program flow.
int Years = 0;
double money = 2000;
while (true) {
if (Years < 20)
{money = money + money*0.05;
Years++;
}else{
System.out.println(money);
//i just used something simple with if statements
public class LOL1 {
public static void main(String[] args) {
int Years = 0;
double money = 2000;
while (true) {
if (Years < 20)
{money = money + money*0.05;
Years++;
}else{
System.out.println(money);
if (money > 5000) {
System.out.println("you are rich");
if (money > 1000000) {
System.out.println("you are a millionaire");
}
}else
System.out.println("poor guy, you need to invest more");
}
}}}
// = 5306.59541028884
//I'm using eclipse
import java.util.Scanner;
public class ifelse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("How much money is in the bank account?");
int money = scanner.nextInt();
System.out.println("What is the annual interest rate?");
double interest = scanner.nextDouble();
System.out.println("How many years are you waiting?");
int years = scanner.nextInt();
double bank = money * interest * years + money;
System.out.println("The account balance is " + bank);
if (bank > 5000) {
System.out.println("You're rich!");
} else {
System.out.println("You need to invest more you nublet");
}
if (bank >= 1000000) {
System.out.println("You're a millionare!");
}
}
}
import java.util.*;
import java.io.*;
import java.lang.Math;
class awesome {
public static void main(String[] args) throws IOException {
Scanner inFile = new Scanner(new FileReader("/data/data.txt"));
double money = inFile.nextDouble();;
double years = inFile.nextDouble();;
double interestrate = inFile.nextDouble();;
for (int i = 0; i < years; i++)
money += money * 0.05;
System.out.println(money);
if (money > 1000000)
System.out.println("You are a millionaire!");
if (money > 5000)
System.out.println("You are rich...");
else
System.out.println("Poor guy, you need to invest more!");
}
}
public class LOL1 {
public static void main(String[] args) {
int Years = 0;
double money = 2000;
while (true) {
if (Years < 20)
{money = money + money*0.05;
Years++;
}else{
System.out.println(money);
if (money > 5000) {
System.out.println("you are rich");
}else
System.out.println("poor guy, you need to invest more");
}
}}}
// = 5306.59541028884
class Main { public static void main(String[] args) { int years = 0; double money = 2000; while(true){ if(years < 20){ money = money * 1.05; years = years + 1; }else{ System.out.println ( money ); break; } } if (money > 5000){ System.out.println ("You are rich"); }else if (money > 1000000){ System.out.println ("You are a millionare"); }else{ System.out.println ("You are poor. You need to invest some more"); } } }
it will say that you have $5306.60 and that you are rich
double money = 2000;
int years = 1;
String finalMoney1;
String roundedMoney;
String finalMoney2;
while(years <= 20){
money = money * 1.05;
years ++;
}
finalMoney1 = Double.toString(money);
roundedMoney = finalMoney1.substring(finalMoney1.indexOf('.') + 1, finalMoney1.indexOf('.') + 3) + "." + finalMoney1.substring(finalMoney1.indexOf('.') + 3, finalMoney1.indexOf('.') + 4);
money = (int) Math.round(Double.parseDouble(roundedMoney));
finalMoney2 = finalMoney1.substring(0, finalMoney1.indexOf('.') + 1) + money
System.out.println("$" + finalMoney2.substring(0, finalMoney2.length()-2));
$5306.60
class pokeball {
public static void main(String[] args) {
double money=2000;
double year=20;
double new_money=money+(money*0.05);
double final_balance=money*java.lang.Math.pow(2.718281,year*0.05);
System.out.println(final_balance);
//Compounded continuously
}
}
5436.562
int Years = 0;
double money = 2000;
while (true) {
if (Years < 20)
{money = money + money*0.05;
Years++;
}else{
System.out.println(money);
break;
}}}}
// = 5306.59541028884