Wednesday, August 18, 2010

Programming- Lesson #5 ~ The For...Next Loop

"Good code is short, simple and symmetrical -the challenge is figuring out how to get there" - Sean Parent

Good programmers will choose the best path to complete a program. It's a good practice if you write concise or minimal code to achieve. This can be achieved by mastering the language features and learn by experience what the most efficient approach to achieve results is. But the problem is here; sometimes we can't know how to make it easy and simple to get the results we want, so we take the hard path and get to complicated steps. By experience and skill, it will be easy to figure out how write codes in a simple, short and symmetrical way.

The previous lesson is how to repeat or lets say " loop " the code block for many times. What if we want to repeat this block code only 2 times or for a certain number? Let's modify the Main method and greet 4 people only ( which means , the loop will be repeated 4 times only ). Look at the image below:

Let's link to the previous lessons and see how this example worked.

The counter variable i to the start value which is 1. VB will test it and see that it is less or equal than th end value, which is 4 here. If the start value is less than or equal the end value then the loop will work and the block code will be executed. When you press Enter ( after running the program ); VB will add 1 to the counter variable i and this way the For Loop will continue working and printing the certain number of statements on the screen. And the program will do the same steps again, see the block code if it is equal than or greater than and print the next statement and so for and so on.

The counter variable (i) is the tool that makes the loop repeat for certain number of times. If it is 1 time then it do it's own work, if it was 2 then it will do it's work also. Here is a table of the value variable (i) for the For Statement to check if it will loop again or not.

You can use any numbers you want for the start and end value. That was just an example of how many times you want the statement to stay looped or repeated.

School will start soon and I'm willing to write two more lessons and then I will try to keep giving lessons of the things I'm reading although I know I will be busy in my senior year and won't have that much time like now. The next lesson will be about classes and how to create your own.

Tuesday, August 17, 2010

Photography Update

Here's another photography update. Today I was trying to take pictures for the Float Project for my Digital Art class... I don't think I got good ones but this is the first day I started and I like to share them. Here are two pictures I took for my sister.

By using Fish Eye Lens, I lay down on the ground under the heated sun. I tried to get the sun beams clear but I couldn't do it in this picture.

I wanted to make them look like cartoons, so I just played with the sliders of contrast, brightness, shadow , saturation and the highlights. I did the same things for the first two pictures. Those are the camera information.

F-stop: f/7.1 , Exposure Time: 1/400 sec , ISO speed: ISO 100 , Flash Mode: No Flash, compulsory , Focal Length: 15mm , Mattering Mode: Partial , Attributes: A , Exposure Program: Manual



F-stop: f/22 , Exposure Time: 1/60 sec , ISO speed: ISO 100 , Flash Mode: No Flash, compulsory , Focal Length: 15mm , Mattering Mode: Partial , Attributes: A , Exposure Program: Shutter Priority

If you noticed; the sun beams are clear here and thats because the Exposure time and the F-stop are different in this picture. I liked how the body is curved with the jump.



This is not for my Float Project, it is just a picture I took in my father's birthday, but I guess it is good enough to include it in this post. I edited it of course by giving it black and white effect then kept the colors on the top fire of the candle.

F-stop: f/9, Exposure Time: 1/200 sec , ISO speed: ISO 400 , Flash Mode: Flash, Auto , red-eye , Focal Length: 40mm , Mattering Mode: Pattern , Attributes: A , Exposure Program: Normal


You can see those new pictures and the recent ones on my photoshop site
Comment and tell me what you think! :)

Tuesday, August 10, 2010

Programming- Lesson #4 ~ Looping and Iteration

" If you don't think carefully, you might think that programming is just typing statements in a programming language." - Ward Cunningham

When I first started leaning programming which is few months ago, I didn't know there is so much to learn and understand. We have to pay attention to each inch of the code because any mistake may cause an error. Programming isn't about writing a statement with wired signs only, it is an artificial language that designed for a computer only to understand. Programming language create programs that control the machine itself. They have form, meaning and errors. Don't be fooled by the statements that you only see, there is much more than what it looks!

What is looping? and what does it do with programming? Basically,the programs needs to do and preform tasks repetitively sometimes, the program itself do this. We call this behavior Looping. When there is instructions that are repeating, it will stop repeating till a current condition is met, and thats called a Loop. There is usually Boolean Expression in a loop so we know if it will continue looping or not. Here is an example of the whole thing I have:
Do you see the line where it says " Please enter your name " ? That's what we are talking about today; how to make one code or a task be repeated more than one time. When you run the program, it will tells you " Please enter your name: " you write any name you like and then press Enter. It will greet the name you wrote by Hi or Hello. You can try it as many times you like, and when you get bored just press Enter (without writing any name ) to dismiss the program. Let's take a look at the program.



Now, what is Console.Write and Console.ReadLine? Console.Write is similar to Console.WriteLine but it does not add aline that breaks the text. Console.ReadLine reads the console not write it and we use it when we keep pressing Enter so the same line gets repeated ( or in other words, the program pasues till the user press Enter).

The loop works this way...when we run the program, the Boolean expression ( false or true value ) will evaulate the code and tells us wether this value is false or true. When we run the program the expression will tell us the value is wrong and do the follwoing which is " enter your name ", when we enter the name and press Enter, the code will loop again ( go back ) and do the same thing, which is enetring the name and it.


As I noticed while experimenting the program above, I pressed Enter without writing anything...for exmaple, I run the program and it told me " Please enter your name". I didn't write any name and pressed enter.. that way the expression will be false and the return value from Write.ReadLine is empty string.

The next lesson will be also about loops. I hope I am making it as simple as possible.

Thursday, August 5, 2010

Programming- Lesson #3 ~ Functions and Controlling The Program

" Programming is similar to a game of golf. The point is not getting the ball in the hole but how many strokes it takes. " - Harlan Mills

The quote above means that, it is not a bout how many lines to do a program. If you did a program in one code line and someone else did it with 3 codes lines, then your program is better, because you consumed time, and memory from your computer. If you can make the same program with less codes then this is the smart way to do it, even when you fix it, it will take less time. And that's go to our lives too, if we can do things with less time, money and effort then we get the same result, aren't we going to take that path?

Now lets get into the functions.The subroutine were doing half of the work in Visual Basic Program, but you can also write methods and create functions (Imagine Method as an action or many actions grouped together). They are like the Sub except they can return a value. For example, if you want a piece of code to calculate the sum of two numbers and return the result you need to write a function because the sub does not return a value, but if you want to print the value only, then the sub is enough. The Subroutine and Function, they both have Input value and do something with it but the difference is that the function has an Output value. Rather than writing " Sub " we write " Function ". That tells the compiler that we need the value back. See the image bellow:

I tried this code above around 7 times till I got it right and print a result on the screen. It drove me nuts !! so don't be shocked if you faced the same thing.

Let me explain it to have a better picture.
First of all, you need to define the value of the function under the Sub Main, if you didn't then you wont have anything print of the screen. As I explained in previous lessons that Dim means Dimension, As Integer is type of data ( identify numbers ) and " = CalculateSum(3, 4)" are the numbers we want their value to return to us.

As I saw many examples on the internet and tried different codes, I figured that you need to write the function this way:

Function Sum(ByVal A As [ type of data ], ByVal B As [ type of data ] )

Like above example:
Function CalculateSum(ByVal A As Integer, ByVal B As Integer) As Integer

Write the Keyword " Function " then name it anything like " CalculateSum" , ByVal A or Car or Apple, anything you want; it is just a name. Then do the same thing after the comma.
Dim result ( or any message you want ) As [ any data type ] = ( the value you want ). By writing " Return message, or result or any name you put after the Dim word" , then the compiler will know to keep the value and return it to us ( to the Main ). End the function and run the program. The value returned by a function called result or returned value.

Let's jump to a different topic. Remember the first example I gave you about HelloWorld? Eric and Sandra? Well, we are back to it now and we will change the way we greeted Sandra and Eric. Let's re-write the correct function and add " If, Else " statement.
When we used the If statement, we were testing the value of toWhom. If toWhom is equal to Eric then the value returns one greeting and it will be like below picture.


In the If statement, the program compares the value toWhom variable against the string "Eric". So if you want to test two values to see if they are equal, you use equality operator.

There is other comparison operators in VB:

= : Equality. The expression has the value true when the left and right hand values are true.

<> : Inequality. The expression has the value true when the left hand is not equal to the right hand.

< : Less than. The expression has the value true when the left hand value has less value than the right hand value.

> : Greater than. The expression has the value true when the left hand value is greater than the right hand value.

<= : Less than or equal. The expression has the value true when the left hand value is less or equal to the right hand value.

>= : Greater than or equal. The expression has the value true when the left hand value is greater than or equal to the right hand value.

So let's say toWhom= " Eric " , then when the expression will work out, it will evaluated True, because right hand and left hand values are equal. If toWhom has other values then the expression will evaluate False. Boolean Expressions are the expressions that evaluate True or False value.

This what we should know for Functions and controlling your program. The next lesson will be about Looping and Iteration, those two things are also included on how your program should flow.

Wednesday, August 4, 2010

Zoo

"I went to the zoo yesterday and took some pictures. Of course, I barley got 3 or 4 good pictures from all the 400 pictures I took. I took pictures for objects more than the animal." - Mr.Skocko - my Digital Art teacher

Here are the best 3 pictures I could share with you.

Randomly walking and captured this image from the left ( I was standing on the left side ) to make a good competition and show the rest of the way ( also the stairs ).Then I edited it and gave it black and white effect.


Does it looks like a real elephant?


That's my favorite picture. I tried like 30 times till I got the sun beams perfect like this. Especially this object was moving so it is hard to get the sun just in place. Did you notice the clouds and sky? they are kind of circular and that's because I used Fish Eyes lens. Then the rest, I just adjusted it's contrast.