FORUMS
Remove All Ads from XDA

[GUIDE]**Beginners guide to programming-C,C++,Bash,Python,java**[GUIDE]

250 posts
Thanks Meter: 1,007
 
By Dark Wraith, Senior Member on 26th December 2013, 11:59 AM
Post Reply Email Thread
12th June 2019, 06:52 PM |#111  
Junior Member
Thanks Meter: 0
 
Donate to Me
More
programming language
Quote:
Originally Posted by Dark Wraith

- An overview over a wide range of topics that are used by every programming language

So lets start ...A program be it in whichever language it always begins with one thing

>Importing necessary libraries

=What does it mean??

Lets say you have to get a door fixed by the carpenter..The carpenter would always need one thing to fix your door i.e. his toolbox. Similarly a Program written in any language would need its toolbox aka libraries to perform whatever task you have for it. Some languages require you to add the libraries required at the beginning of the program while some like python or bash do not need it

Note: In a bash script you need to add the lines #!/bin/bash to tell the interpreter that it is a bash script

In C/C++ this is how you include a library file

Note: A library file in C/C++ is actually called a header file and its extension is .h(You might have seen many .h files in any android source files(ROMs or Kernels)

Code:
For C :

#include<stdio.h>

For C++

#include<iostream.h>
Please note that this is a single example where stdio.h(Full form: Standard input/output) file contains utilities that is used to print something on your monitor or scan whatever you type in your keyboard etc..

For java, you need to get the .jar files. However where do we have to put it is the tricky part. My suggestion as of now would be to use Eclipse and you will be able to see that when you create a java project, Eclipse directly creates the tree structure of your project with a tab called libraries. Just add the jar files there and you are good to go
or
trickier method is to create a lib folder or any folder in your project and add the jar files there and then you just have to set a path variable in your environment settings. Add a path of your jar files in your "PATH" variable(for windows)..in linux, just define and export the PATH in your bashrc file

Note: You will come across import functionality in Java. Import functionality is different and has nothing to with importing libraries. I will explain this at the end of this post

Ok,So lets assume your environment is set, your libraries have been defined...Now its time to start the actual coding

>Initializing variables

Prolly the most important step in this guide. What are variables??

in simple lay man terms, is something that is used in programming to store a value. Now the value can be a lot of things, value can be a number(eg:. 2), value can be a letter of English alphabet(eg: b), It can be a line of letters(eg: Abc), can be anything....

so basically a variable will be something like a=2;
where a is the variable name and 2 is the value...
similarly a = 'a' (where a would be the variable and the a in quotes would be the a character)

But its not that simple, in C,C++ and Java you need to explicitly specify what type of variable it is( The compiler allocates memory according to what you specify)
Note: Python and bash don't need such a declaration( a = 2 is good enough... )
So i C/C++ and Java, variable declaration must be something like
Code:
int a;
char b;
float c;
or 
int a=2;
char b = 's';
float c= 2.00
Where int is integer value,b is character,c is float

Float is nothing but a decimal value is known as float.Please also note the semi colon( after every statement.semicolon in C,C++,java is used to terminate a statement(Except loop)

=Why it needs to be defined in c/c++/java and not in python/pearl/bash??

Every compiler is different. In C/C++/Java, when a variable is declared, memory is assigned to it. For eg. If you say int a then variable a is assigned 2 bytes, now you say char a, a is assigned 1 byte. In python/bash/pearl variable memory is dynamically allocated according to its value and therefore we do not need to specify its type.
So what happens if you assign an char variable(say char a) an integer value
eg:
Code:
 
int a;
a= 'a';

The next section will tell you what happens with the above code....

Type casting (You can skip this section and come back later if you feel its confusing you)...

What if you had to convert a float value to an int value or an int to float..or simple a char value to int(Will give its ASCII value which is your job to find out what that is) or as simple as taking input as char...like '0' and converting it to 0...

That is where type casting comes into picture

There are two types of type casting

Implicit and Explicit

Implicit type casting means that the type is changed automatically by the compiler itself. Explicit is just the opposite..Will show you all scenarios...

Code:
int a=2;
int b=3;

float c;

float c= a+b;  // compiler automatically converts value of a and b to int and calculates(Implicit)

---------------------------

float a = "10.00";   //float values
float b = "20.50";
int c;

c=(int)a+(int)b;       //Explicit type casting

printf("%d",c);

--------------------------

char a= 'a';

int b = a;     //implicit

or

int b = (int)a;  //explicit

Note: Where implicit is required you can use explicit type casting as well to be safe...

-----------------------

now char a = '2';

int b= a - '0'  //used to take number as character input and assign it to integer variable

Again you can write this as

int b = a;

or 

int b = (int)a - '0'
Now how to know which one require explicit or implicit type casting

That is your job to find out....

Hint:

float to int causes truncation, i.e. removal of the fractional part;
double to float causes rounding of digit;
long int to int causes dropping of excess higher order bits

>Printing and scanning

Now for any program on this planet to work, you need one basic thing. The program should be able to display something on your monitor.
Now combine that with taking inputs from the user. You will get an interactive program that takes inputs from a user and prints the result accordingly. the command to print differs from language to language
Code:
For C

printf("Hello");

For C++

cout<< "Hello" ;

For Java

system.out.println("Hello");

For Python

print "Hello"

For bash

echo "Hello"
All of these will give the same output on the screen:

Hello

Note: Please note that C/C++/Java requires brackets while python/bash don't.

Note: Check out the semi-colons again. Python/pearl/Bash don't require a semi-colon to end a statement

Now comes scanning part. Lets say you want to take two integer inputs from the user.Now we'll combine it with variable declaration and print statement so that u can understand better

Note: From here on i will mainly show you examples in C or C++ because it would get too confusing for you to handle all the syntaxes together. Once your C knowledge increases, you can easily learn the syntaxes of other languages and try programming

Code:
C
int a;
int b;
printf("Enter two numbers");
scanf("%d %d",&a,&b)

C++
int a;
int b;
cout << "Enter two numbers";
cin>>a;
cin>>b;

Java (A bit complex and thats why i wont include Java/python or even bash after this)
int a;
int b;
Scanner s=new Scanner(system.in);
System.out.println("Enter two numbers");
a=s.next();
b=s.next();
Now in C you will notice two %d %d...%d tells the compiler that its an integer value..it is repeated two times because you take two input values.&a,&b will tell the compiler that store the first input in a and the second one in b. You could also have written it as

scanf("%d",&a);
scanf("%d",&b);

for int its %d
for char its %c
for float its %f

so in order to take character input the syntax will be

scanf("%c",&a)

>Now comes the operator part

Lets go over some basic operations : there are 4 operators in maths or Programming

sum,minus,division,multiplication

in programming it would be +, -, /, *

so a+b would give you the addition of integer values in a and b

I will write a simple C program with comments after # sign now to take two numbers as input and add them,subtract them, divide them and multiply them

Code:
#include<stdio.h>   // initializes the library for standard input/output
#include<conio.h>  // Another header file containing command to clear the screen

void main()     // for now just learn this,this is how you write the body of a C program,anything that you code inside is executed(no semi-colon here)
{                     // Curly brackets are used to denote the start of your program body

int a;              // variable initializing
int b;              // variable initializing

clrscr();         // module to clear screen,not really required but i used it to show how conio.h is included to make this module work

printf("enter two numbers");
scanf("%d %d",&a,&b);

#now we need to store the value of the result somewhere,so we define new variables

int c,d,e,f;

#to add

c=a+b;

#to subtract

d=a-b;

#to divide

e=a/b;

#to multiply

f=a*b;

#Now to print the result

printf("the result is as follows %d,%d,%d,%d",c,d,e,f);

}   # curly bracket to end the program
Thats it......now variable e can be taken in float..because if you take it as int. and say a is 3 and b is 2 then 3/2 would give you 1.50,a float value

Modulus(%) operator

Modulus operator is used to calculate the remainder of two numbers if divided....

what are its uses?...Well one major use is you can find out if a number is odd or even

Code:
int a=20;
int b=10;
int c=a%b;
printf("%d",c);  // would yield 0 as result

so wen coupled with if

if ( a%2 == 0 )    // Check next post to find out about if...:)

would check if number is even

if a is not even, then it would give a remainder wen divided by 2 and you can easily find out...
There is one slight problem with it though....what if you have a negative number and divide it with a positive number(non divisible numbers)...Then the remainder comes in negative...

say -7%3 should return remainder as 1

but it returns -1..So you have to be careful...Best it to check if remainder is non zero...than exact remainder

Continued in Post 3

hi thank you
good luck
7th September 2019, 07:11 PM |#112  
Junior Member
Thanks Meter: 15
 
More
Thanks for this guide. It's really helpful.
11th October 2019, 02:50 PM |#113  
Junior Member
Thanks Meter: 0
 
More
Guidance
Really Great Guidance. Thanks for sharing it.
16th October 2019, 01:10 PM |#114  
Junior Member
Thanks Meter: 0
 
More
Beginners Guide
Really Great Content. That's very impressive for beginners.
20th October 2019, 07:52 PM |#115  
Junior Member
Thanks Meter: 0
 
More
Thanks for the guide.

Advice for newbies: try to use Xenko / Unity / Godot, it'll be a fun way to learn C# by making Games
27th October 2019, 01:04 PM |#116  
Junior Member
Thanks Meter: 1
 
More
thank you
Quote:
Originally Posted by Dark Wraith

Hi guys!!!Someone posed a nice question on the forum a few days back asking me if there are any guides on programming(he meant C) in the forum and I actually wondered...There are no beginner's guides for programming as such..There are good extensive guides though and you have to ultimately go through those if you wanna dig in deeper..But i thought about creating a beginner's guide..One that teaches you the following thigs...

- Basic introduction to a programming(Be it C/C++/Python/Java)

- An overview over a wide range of topics that are used by every programming language

- And how to relate it to Android and use it to modify kernels/Roms/apks etc

So here it goes....

Basic introduction to programming

We have in our life always wondered, how come the gadgets we use, hardware we use function so well and how do they understand and perform such complicated tasks. The answer lies in one word "Programming"

What is Programming and what are these Programming languages??

-Programming is nothing but a set of commands created by a developer(human being or beings) which is then coded into the machine so that the machine{digital) performs a set of tasks depending on user inputs or independently. In simpler terms, Even a microwave or a washing machine have a set of codes programmed so that they work and do what they are intended to do

-Programming languages are nothing but ways to communicate something to the machines to make them perform the tasks which you want.There are many languages out there..Famous among them being, C,C++,Java,Python and so many more....

So why are these programming machines needed??

-Simple....because the computer understands only one language..the daddy of all languages....Binary language ( 0 & 1)...
Binary language would somewhat be like ( 0 1 01 0101 010101 0101010101....)...To us it would look crazy,To a computer or any digital device..0 means no electric pulse...1 means an electric pulse and hence everything is done in pulses...

-So what are these languages(C and all): Proper term would be an interpreter not a language. All these languages do only one thing in different ways : convert whatever you say aka code into 0s and 1s and tell the computer,mobiles, any other device to do something via binary language(010101000101)

-So why are they needed, well if you can code in 0s and 1s...umm, you'd be a champ..(Dont mean it sarcastically,you'd seriously be a champ)..But normally you need such languages to do the job.

-Which language is the best??Well they all do the same jobs but they are divided into 3 categories
>High level language
>Medium level language
>Low level language

High level languages are nothing but what we are gonna go through in this thread. C,C++,Java,Python,Perl,bash etc are all examples of high level languages.Why are they named as such. Its because they are the closest to human readable language(taking English as the standard)

Medium level language is nothing but an intermediate stage between high level language and low level or machine level language.Medium-level language is mainly an output of the programming source code written in a higher-level language.The GNU Compiler Collection (GCC)(Android tool chains you use to compile kernel) uses several intermediate languages internally to simplify portability and cross-compilation

Low level language is the lowest level of programming. Generally this refers to either machine code or assembly language. As the name suggests it is not close to human readable language. Low-level languages can be converted to machine code without using a compiler or interpreter, and the resulting code runs directly on the processor.

Example: A function in 32-bit x86 machine code to calculate the nth Fibonacci number:

8B542408 83FA0077 06B80000 0000C383 #courtesy : Wikipedia
FA027706 B8010000 00C353BB 01000000
B9010000 008D0419 83FA0376 078BD98B
C84AEBF1 5BC3
(That looks seriously scary to me....)

One advantage of this though is that it runs very fast and consumes less memory as an equivalent program in a high-level language will be more heavyweight
Disadvantage:Low-level languages are considered difficult to use, due to the numerous technical details which must be remembered.

-Why do you need to all this?? Well i for one think that in order to use a language to code something, you should know the flow of your code. how it goes, what it does and how the end result is processed

So say you write a code to add two numbers you should know that

1.You write the code(I'll tell you in later sections how)
2.It goes to your processor which loads the program into your ram
3. With the help of inbuilt functions the the intermediate code is generated
4.The compiler then converts the intermediate code to the machine code
5.The machine code is then executed

-Which language is the best and which one do i need to learn?
Every language has its pros and cons. Which on you learn is up to you and depends on what you want to do. Eg: If you are interested in kernel development, then you need knowledge in linux commands and C programming as the source of a kernel has modules built on C and linux commands are needed for compiling on linux. Similarly, Lets say you wanna create a tool, you can do so using bash(bash is a command line interpreter language: Will tell you in next paragraph),C,Java etc..

BTW, BASH is a command line interpreter for linux, technically its a programming language but it is basically used in command line(eg: command prompt in windows, terminal in linux)...in lay man's term...a line in bash can be executed from the terminal...eg. cd foldername in linux will come under bash..a bash script is executed line by line on the terminal in linux

So enough of basics,Lets head over to the next section(Check the Next post)..

The Following User Says Thank You to joudsy For This Useful Post: [ View ] Gift joudsy Ad-Free
18th November 2019, 08:06 AM |#117  
Junior Member
Thanks Meter: 0
 
More
Quote:
Originally Posted by joudsy

I lenjoyed this thread I have been looking for apps hacks anything . Even a beginners toolbox for getting into this. Stfuff I am a musician and producer of music so I found a way to up my volume on my zte and Galaxy amp 2 phones I have been addicted to learning this. Stuff. Hey thanks for posting that thoughi. It really explained. What I was wanting to know.. any info you may have on a zte z3351s or Galaxy amp2, Ii want to know anything ... About them .. anything .. I can't find the zte anywhere
20th November 2019, 05:59 PM |#118  
Junior Member
Thanks Meter: 0
 
More
How do I update my phone?
Post Reply Subscribe to Thread

Tags
c++, java, programming guide, python, tutorial

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes