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.
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.
No comments:
Post a Comment