In this article, you’re going to be introduced to making a practical use of variables that store predefined data as well as data input by the user (to follow in later articles) to make certain decisions. You’ll learn more about conditional logic and loops available to use in WebMatrix, namely – If/Switch statement, and While/Do-While/For/Foreach loop.Download complete CSHTML ProjectStep # 1: First of all, create a new empty site and name it: "BasicsInstructions". Once you’ve done this – you’ll automatically be presented with the contents of the newly created page. To continue, replace the existing markup with the following (we’ll be referring to it as you go):
Code Snippet #1: ASP.NET Razor Syntax
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
Code Snippet #2: HTML/CSS markup
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
Step # 2: When you’re finished, it’s now time to see your web page in action! Click on the “Run” option from the standard toolbar to launch the page in a browser – what you’ll see should correspond to the image shown below.
Step # 3: Let’s now take a closer look on the underlying code (HTML/CSS markup and the ASP.NET Razor Syntax).If StatementStarting from line # 2 you’re presented with 3 examples concerning the “if” statement.
- The first example (lines # 3-22) is a compound “if” statement; this means that you can have many different conditions and instructions to be executed when they’re met – along with alternatives to be executed otherwise. For example: in this case after checking whether the first number is the smallest (the “if(condition)” statement), if this condition isn’t met the program progresses to the next alternative condition (the “else if(condition)” statement), which in turn not being met leads to the final alternative (the “else” statement) instruction terminating the compound “if” statement.
- The second example (lines # 24-30) shows how a simple conditional “if” statement can be used to force execution of a particular code. To do this, simply set the condition to be either “true” or use a Boolean expression that returns the “true” value e.g. “1==1”.
- The third example (lines # 32-38) proves that the “false” value used as the condition (which in this case is regarded as not being met) will never execute a particular code. Switch StatementIn lines # 41-74 you’re presented with an example concerning the “switch” statement. The “switch” statement is just another way (designed for convenience) of representing the “if” statement. In fact, both their structures and functionality are virtually the same – please refer to the comparison shown below.
switch(variable){
The above two samples have exactly the same functionality and could therefore be used interchangeably. Back to our example: we have used the “DateTime” helper to assign the current day of the week to a variable (upon successful conversion to the string data type), which then used by the “switch” statement allows for determining the appropriate greeting to be displayed. Note that there is no “default”/”else” alternative as the value supplied by the server will always remain in the specified range.While LoopIn lines # 92-109 you’re presented with an example concerning the “while” loop. The “while” loop requires the condition specified to be met each time it’s to be run. In this case we’ve used a variable initialised with the value of “1” and the condition for the loop of “<= 10” which ensures that it will execute exactly 10 times.Because this particular example is about counting from 1 to 10, later on we’re using an “if” statement to detect when we’re finished and display the appropriate message.Do-While LoopIn lines # 113-129 you’re presented with an example concerning the “do-while” loop. The “do-while” loop unlike its predecessor checks the condition specified only after it’s been executed (which ensures that it’s executed at least once). You will see some practical uses of this functionality later on when we start dealing with forms and data input by the user. In this case we’ve used a variable initialised with the value of “1” and the condition for the loop of “<= 100” which ensures that it will execute exactly 100 times.Because this particular example is about counting from 1 to 100, later on we’re using an “if” statement to detect when we’re finished and display the appropriate message.
For LoopIn lines # 133-148 you’re presented with an example concerning the “for” loop. The “for” loop is just another way (designed for convenience) of representing the “while” loop. The “for” loop requires three parameters to be specified in order be to run – namely: an initialiser, a loop-test, and a counting expression. In this case, we’ve set them accordingly to: “CountdownTimer = 60”, “CountdownTimer >= 0”, and “CountdownTimer--” which ensures that the loop will execute exactly 60 times.Because this particular example is about counting down from 60, later on we’re using an “if” statement to detect when we’re finished and display the appropriate message.Foreach LoopIn lines # 152-162 you’re presented with an example concerning the “foreach” loop. The “foreach” loop is just another way (designed for convenience in displaying the contents of arrays) of representing the “while” loop. In this case, we’ve used a “helper” variable to read each element of the array in turn and then displayed its contents at each loop run. You will see some practical uses of this functionality later on when we start dealing with databases and displaying the contents of records.
Step # 4: Congratulations! You have now completed all the activities related to this article.
What have we done?In this tutorial, you have been introduced to making a practical use of variables that store predefined data as well as data input by the user to make certain decisions. In the following one – you’ll be introduced to processing of data input by the user by using forms. Then you’ll advance to working with databases and unleash the full power of WebMatrix lying at your fingertips! It’s all yet to come, please stay tuned! :-)