The difference between the way we get the program to
say “Hello, World!” in Scratch and in C, is as follows:
You may notice, We write ‘main’ in the start of the first function (The statements enclosed in Curly braces at the whole, are termed a function, Don’t confuse between Functions, Conditions and Loops though).
Writing main, here is a way of telling the Computer
that this is the function which the computer needs to run first, it is like the
‘When Flag Clicked’ block in Scratch. It starts the program. There has to be a
main function in any C/C++ Program and all other functions are needed to be
linked to this main function only. (If that doesn’t makes sense to you, don’t
worry for now!). In C/C++, Every Function needs to take some Input and give
some output, whatever type we may want to output is written at the start of the
program(That’s why we write int main(void)), and we take no input in this case
that’s why we write void in those braces.
Similar to the ‘say’ block in Scratch which we used
to have the program say something or print something on the screen, In C, We
have the ‘printf’ function with which we could do the same.
And just like we used to write whatever we want to print(in this case, “Hello, World!”) in the placeholder present in the say block in Scratch, In C we would have two Braces like the ones in the picture and inside those braces we would write whatever we want to write in Double quotes (“ ”). We write “\n” in the end to mark a line break i.e. To move the cursor onto the next line.
At the very beginning of the program, we write
#include, This is a way of telling the Computer that we want to include a certain
library into the Program.
Libraries are lists pre-defined functions which are
already defined for you so that you can use them.
We write the name of the Library in triangular braces
like this < >, In this case, We
have included the stdio.h library which consists of all the functions relating
to Input and Output (for example: Taking Input from the User, Printing
something to the screen etc.)
Furthermore, We may write the same thing in C++ as,
#include
<iostream>
using
namespace std;
int
main(){
cout<<”Hello,
World!”<<endl;
}
Note
that instead of the <stdio.h> library in C, We have the <iosream>
library in C++ which serves as the library of the same type of Input/Output
functions in C++. We have an extra line saying “using namespace std;” in C++, its
just a convention with a deep reason. Don’t worry about it right now!
Instead of writing printf(), In C++, we write
‘cout’ followed by two of ‘<’ this sign i.e. “<<”, again followed by
whatever we want to print. Instead of “\n”, we write ‘endl’ to embark the end
of a line or line break.
Now
Moving on to other Programming Constructs, To do something repeatedly, forever
we had the forever block in Scratch, In C/C++, We may write it as
while(‘condition’){‘statements’}, with all those statements which we want to
repeat inside those curly braces. We could have literally any other Boolean
Expression in the while(‘condition’). We may write while(i>50) or anything
we may wish.
In
this case, We wanted to repeatedly say “Hello World!”, which is why we wrote,
‘printf(“Hello World\n”);’ as a statement inside the curly braces.
If instead, We wanted to repeat something for a fixed number of times in Scratch, We used the repeat block, with the number of times, we wanted to repeat it in one of the placeholders and whatever we want to repeat are to be attached in the space, in C/C++, we do the same using the for loop, whose syntax is: for(‘condtions’){statements}. In the example below, to repeat printing ‘Hello, World!’ many times, We first declare an integer int i = 0;
Then,
Until that integer is less than 50, We execute the commands, given below, in
the curly braces.
And that’s how we execute some given statements, any number of times. In this case, this programming construct shall print “Hello, World!”, 50 times on the screen.
Now,
that we’ve covered Loops and the general Syntax. Lets move on to Conditionals,
That is when we want to do something only when a given condition is fulfilled.
In Scratch, We did so using the If block, Here, we’ll do that using the
following syntax : If(‘Boolean condition’){‘statements’}else if(‘another
Boolean condition’){‘statements’}else{‘statements’}, We may have any number of
‘else if(s)’
Now,
To Summarise, Keep in mind that the general look of any C/C++, program is like:
*Here
its in C++*
#include
<iostream>
using
namespace std;
int
main(){
cout<<”Hello
World!”<<endl;
}
If
we want to add conditions or loops, those need to be added inside this main
function or any other function. As:
#include
<iostream>
using
namespace std;
int
main(){
if(1>2){
cout<<“Hello,
World!”<<Endl;
}
for(int
I = 0; I <50;i++){
cout<<“Hello
World!”<<endl;
}
}
Always
remember that every statement in C/C++ is followed by a semi colon, ‘;’, If you
don’t do so, It will lead to an error.
In
upcoming sections, we shall be driving you through other schematics of C/C++
and have you write code and execute it on your Computers.
Comments
Post a Comment