In: Computer Science
Q.11
#include <stdio.h>
int main(void) {
// your code goes here
int i = 0;
for(;;)
{
if(i>10)
break;
printf("%d ",i);
i++;
}
printf("\n");
for(int i = 0;i<10;)
{
printf("%d ",i);
i++;
}
printf("\n");
for(int i = 0;;)
{
if(i > 10)
break;
printf("%d ",i);
i++;
}
printf("\n");
return 0;
}
Illustruted above are the example of non traditional for-loops.
The normal flow of for loops is:
1) Initialize
2) Check if condition is true
i) If condition is true then execute the block
ii) If condition is false, then terminate the loop
3) If the condition was true of step 2 then increment the loop
Now lets have a look at the for loops, it is also doing the same thing, but the thing is it is performing the most of the parts inside the body like while loop:
1)
int i = 0
for(;;)
{
if(i>10)
break;
printf("%d ",i);
i++;
}
i intialized outside the loop
entred to for loop
1)Do not found the intialization part, entered the loop body
2) i > 10 is the check inside body, saying if i > 10 then break the loop then we have loop normal body and at last i++ increases the loop by 1,
This does in the similar fashion as the normal for loop but un traditionally.
Similarly, all other cases can be justified
Q.12
Lets see the structure of switch case in Ruby and other programming language.
Ruby
case x
when 1..5
"The number is beteen 1 to 5"
when 6
"The number is 6"
when "cat","dog"
"It is either cat or dog"
when String
"String passed"
else
"Option not specified specifially"
end
Other Programming Language
switch(x){
case 1: printf("The number passed is 1");
break;
case 2: printf("The number passed is 2");
break;
default: print("The default case is passed");
break;
}
Diffferences
1) Ruby switch case compares the object in "when" clause with the object in the case clause using === operator. e.g 1..5 === x and not x === 1..5
The advantage over the normal for loop being Ranges, Classes, and all sorts of things can be easily tested unlike the other Programming lanhuage where the equality need to be provided for all cases.
2) The other major advantage observed is Ruby switch case do not have fall through (fall through means if you don not specify the break statment then all statement below the searched statment will print until it either found break or switch case ends). Thus overhead of writing break carefully in Ruby is savd.
3) If there are more than one discrete statment outputting same result, then we can specify multiple matches in a single case seperated by comma. e.g when cat,dog in the above example.
Q.13
In normal programmming what we do with "break" is what is the work of "last" in the perl i.e When a "last "statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop
e.g
$num = 10; while( $num < 20 ) { if( $num == 15) { # break the loop. $num = $num + 1; last; } print "value of a: $num\n"; $num = $num + 1; }
Q.14
Advatages and disadvatnages of using single line comment and multi line comment under differnet criteria.
1) Single line comment
i) Single line comment is used to make the code readable by specifying the cooment above the line or by side of the line.
ii) Commenting the non usable codes.
Advantages:
1) Plain engish so easier to understand logic going
2) Some times comment more precise then code, in case of debugging.
3) Comments ease comprehension by collecting information into one place.
Disadvantages:
1) Take considerable time to write good comment
2) Less reliable then the code.
3) The encourage bad code, some time developer specifying comment writes bad code specifying comment, thinking that with comment it can later be rectified.
Multi line comment
Purpose of the multi line comment is also the same. But is written where broader expainatio of the code is required or it is required to specifty the TODO list in explained manner unlike the single line comment where comment below the flow statement is written.
Advantage:
1) Easy english easy to understand
2) Comments are persistant, even though the code had been modified for the performance or bug issue, but it's purpose is clearly specified bt the comment.
Disadvantage
1) Less precise then code
2) Commenting the code can sometime be disasetrous for future reference.. Actuallty it is never recommended to comment the running code.
Q.15
Two programiing language not using curly braces are : Python,Haskell
Without Curly Bracket
Advntages of using these languages:
1) These language uses identation to replace the curly brackets. So the code written are very much readbale and understandable.
2) Overhead of looking for curly bracket goes. If your code is properly idented you are ready to go.
Disadvantage
1) Identing the code is not an easy job for an individual form C,C++,Java. So one wrong Identation and the whole code is messed up.
2) Standard tab or spacing need to be followed strictly i.e if one is used for identation then throughout the whole program only one tab should be used.
Lanuages with Brackets
Advantages
1) No need of indenting the code.
2) Curly braces easily help identify each block unless the code is very much messed up.
Disadvantage
1) The code do not strictly require identation, So if the code is not idented then some time becomes hard to look.
2) The single missing bracket missing can some time cost hours of debugging.