AP Computer Science Lesson Introductory Java Language Features (3)

in #tutorials7 years ago (edited)

Hey Steemers!

I've been out for a while, but here's another one. Other new posts will be coming out, but I have a lot of organizing to do, so we shall see. My last lesson I described the basics of loops. We really just ran through some programs with if statements and while and for loops...

APCSLogo.png


A For Loop

  • Usually used for mathematics.

For (int I = 1; I = 10; i++) {
summ = summ + 1
// i from 1, 2, 3,…. 10 i++ means add 1 to i
}
System.out.println("The sum from 1 to 10 is: " + summ)

  • This body will print the sum of the integers from 1 to 10. It loops the brackets until i hits 10. You could make it 1,234,533!

A While Loop

int I = 0;
while (int I < 100) {
i = i + 45
System.out.println(“The value of i is: “ + i)
}

  • This one will print multiples of 45 three times (or until the user desires).

If statements

**Very useful and can be specific.
// Here was our first cull program of the day. Import comes before the actual program if you need any java packages.

Import java.util.scanner;
public Class IfPractice {
public static void main(String[] args) {
double radius = 0
scanner radius = new Scanner(System.in);
if (radius < 0) {
System.out.println(“Radius cannot be less than 0”)
scanner radius = new Scanner(System.in);
}
if (radius > 0) {
System.out.println(“Area is: “ + 3.14 * r*r);
}
if (radius = 0) {
System.out.println(“Area is: 0”);
}
else {
System.out.println(“Sowwy”);
// If user enters a string
}
}
}

  • This program will find the area of a circle based on input from the user.
  • "+" is an concatenation of strings and/or numbers.

We then did some exercises individually and with partners. SBD for solutions programmed in Java... Test your wit!

  1. Print "Welcome to Java!" five times with a for loop.
  2. Print the solution to:
  3. Make a program to find the perimeter and area of a circle.

Thank you for the support! STEEM on!

LOGOAlien.gif

Sort:  

Hi Hansenator

Very informative post .It is resteemed by me as i did for 1 & 2 features

Thanks a lot for the support.

Thanks again kishore!

Welcome back !

Thank you Marcel!

Coin Marketplace

STEEM 0.30
TRX 0.33
JST 0.052
BTC 100146.20
ETH 3986.27
SBD 4.00