[Android Software Development] Dark Wraith:
Tutorial continued ​

>Looping

One of the most important concepts

What if you had to display your name 50 times????

Wont it be a bit tedious to write it 50 times using printf statement???

That is where the concept of looping comes into picture. What is a loop??

A loop is something that gets executed as many times you want depending on the condition

There are 3 main types of loop methods(Mostly same in all languages, the syntax only differs)

1.For loop

2.While loop

3.Do while loop

For loop

The most widely used probably

Syntax of FOR loop in C is

for(condition)

{

body of the loop

}

that is just the skeleton of the loop. Lets take the problem as our example(you need to print your name 50 times). I will write the program to demonstrate. I'll assume my name : dark wraith to print

#include<stdio.h>

void main()
{

int i;

for (i=1;i<=50;i++) // see the contents of the loop in brackets ( i=0 >> stores the value 0 in i; i<=50 >> this is the actual condition,so as long as i's
{ value is less or equal to 50 the loop's body will run; i++ >> this increments the value by 1 every time the loop is executed
printf("dark wraith"); // the body of the loop is executed as long as the condition is satisfied
} // The curly bracket denotes the end of the loop

}

Ok in the above example i++ is used to increment the value of i. so when the body is executed first time i's value is 1. then i+1 happens. So next time i's value becomes 2 and it goes on till i's value becomes 51. At that point the condition is not satisfied(i<=50) and the compiler exits the loop and the next line after the loop is executed.

Note: You may also write: for(int i=1;i<=50;i+1) // we are initializing i in the loop itself instead of writing it outside

Drawbacks and uses: For loop is generally used when you have to do something for many number of times. Drawback is that if you want to execute something only once if a condition is satisfied then you generally go for if..else loop

While loop

Its the same as for, which one you want to use is upto you( for is my favourite but that's just personal opinion)

int i=0;
while(i<=50)
{
i++;
printf("dark wraith");
}

Do ....while

Same as while but with one difference..this you need to find out urself....;)

Final Note: Looping is a huge concept and you will use it extensively..This is just a beginner's guide so make sure you study more on this

>Conditional Statements

What if you wanted to execute something only if a condition is satisfied..In such cases we use the following

If...else

As the name suggests if something happens execute this else execute something else....:)..Now what if you wanted to print your name only once if user if the user says y and print no name if user says n

#include<stdio.h>

void main()
{
char a;
printf("Do you want to print a name(y/n");

scanf("%c",&a); // stores user input in char variable a

if(a ="y" ) // checks if whatever user entered is y
{
printf("dark wraith");
else if (a = "n") // additional condition to check if user inputs n
{
printf("no name"); // Note that the statement inside a loop body is terminated by semi-colon
}
else // if user enters anything apart from y/n then this is used
{
printf("enter a valid choice");
}
}
} // the three brackets here are one for the last else, one for the if and the last bracket is for closing void main

Note: You need to be careful with the curly brackets, remember if you start a loop, you also need to close it(solves most of the errors most of the times)

>Switch...Case statement and nested if...else conditional statements

Switch...Case...This is a very handy tool in case of a you have multiple conditions and multiple declarations pertaining to those conditions..

for eg: In case you have used dxdia's kitchen or any other tool of such sort, you will see a list of options with a corresponding number. Say if user presses one, then it shows some options, if he presses 2, then you go to some other options and so on and so forth.Switch and case statement allows you to do that with ease

eg:

#include<stdio.h>
#include<stdlib.h> //header file for exit and break functions
void main()
{
int i;
printf("Press 1 to say Hi");
printf("Press 2 to say Hello");
printf("Press 3 to display XDA on screen");
printf("Press 4 to exit");
scanf("%d",&i)
switch( i )
{
case 1: //Executed if input is 1
printf("Hi");
break; // Break statement is used to skip the block and go to the next line after the entire switch case block.It is mandatory except the last default case (Though its better to include in that as well)

case 2:
printf("Hello");
break;
case 3 :
printf("XDA");
break;
case 4 :
exit(0);
default :
printf("make up your mind and try again..:P");
}

}

case statements in bash is my favourite though...Syntax is similar and much more friendlier and you dont need to define a default block or use break to end it....

#!/bin/bash

echo "Press 1 to say Hi"
echo "Press 2 to say Hello"
echo "Press 3 to display XDA on screen"
echo "Press 4 to exit"

read input

case $input in

1)echo "Hi";; # A case ends with ;; not break
2)echo "Hello";;
3)echo "XDA";;
4)exit;;

Nesting

What do you mean by nesting? Nested loops mean a loop iside a loop..It can be nested for loop or while or anything...

Nesting can be done for anything

An if statement can be nested inside a for loop (There is an example in array portion for that)

>Strings and Arrays

Till now we have only taken one character as an input but what if you wanted to get yes as an input from the user??? then char would fail because it can contain only one character

Solution: Array data type

Strings : Arrays are something that can be used to store a string of characters(eg. dark would be a string made of characters d,a,r,k) or a string of numbers(1,2,3,4)

An array is defined like this: char a[size];

Where [] denotes that a is an array data type

Now lets say you define the length: char a[20] : That means a is an array variable which can store upto 20 character data type values

Similarly int a[20] can store 20 numbers

But how do you store the value, you cannot just do scanf a[]. I will show you how...:)

Note: Before i show you how, we need to learn how an array works. think of an array like a train. A train will have compartments. Say 20 bogies. Now you place exactly one passenger in each compartment. Now each compartments has a name. Similarly, say you wanna store a,b,c,d,e,f in an array named abc[7]

abc[7]=

a

b

c

d

e

f

\0

Index= 0 1 2 3 4 5 6

Note that the index of the first letter starts with 0 and the array is take note of the last character "\0". Every array in C/C++ is terminated by "\0"(This will be instrumental in whatever you do with arrays)

#include<stdio.h>

void main()
{
char a[20]; // array definition

printf("Please enter your name");

scanf("%c",&a); // array is stored normally just like char or int using scanf

// Now to print the array

for(int i = 0 ; a[i] != '\0' ; i++)

// i= 0 and != means not equal to so the loop will go on till \0 in array i e the last character is not encountered

{

printf("%c",a[i]); // Prints array a[0] then a[1] and so on till a[] reached \0

}

}

There is a header file called string.h that can be used to calculate a string length, or join two string etc.(Thats your job to find out..:))

Cannot dwelve deeper into arrays but half of your job is done if you understand arrays and loops

Key points for arrays is : You can access individual elements of an array..like the second character in array

looping and array so hand in hand, For loop is extensively used

I will show you one more example.Say you have to find out if the 2nd letter of the array is 'a' (suppose i enter dark, then it should say yes else no)

So lets start

#include<stdio.h>
void main()
{
char name[20];
printf("Enter a name");
scanf("%c",&name);

for( int i=0;a[i] !='\0' ; i++)
{
if(a[1] == 'a') // will check if second character i.e. position 2 has character a
// This is an example of nested if statement in for loop
{
printf("Yes");
}
else printf("no");
}

>Functions

What if there is a process you are calling hundred times in a program. Say you wrote the logic to add two numbers. But you need to add two numbers 100 times in your program. Won't it be tedious to write the same lines of code so many times when you are doing the same thing again and again

That is where functions come into picture..

A function needs to be defined only once and can be called n number of times in your program. How do you define a function

function name ( arguments,arguments....n) (looks scary) (Maybe an example would help)

Say you need to define a function that adds two numbers...so....syntax would be

int add(int a,int b)

{

int c=a+b;

return c;

}

That's the function definition..return statement returns whatever the output of the function is...since it is an addition function it will return an integer value that's why we have defined it as int add....(int a,int b) are the arguments or the input required to make the function work.

How do you call this function...there are many ways....

int d=add(2,3) 1 way, will store whatever is returned by add(2,3) in variable d

or

int p=1;

int q=2;

int r=add(p,q) another way, will take value of p and q as input and store result in r

Hang on!!...Did you guys wonder about the void main we have been using till now????

Yes it is a function as well.....in fact it is the main function of our compiler...The compiler executes the main function first always...Thats why we write our actual program in void main()......

Can it be int main() or char main()????

Well, why not..but remember if you define your main like that then you have to return a value.......so if you write it as int main and dont want any value to return, then you have to add the line....return 0 at the end....or return NULL for char main at the end....Thats why we use void main...void means no return type required...:)

where do you define functions?? At the beginning or end of your main, not inside the body..

Warning: If you want to define the function after your main then a special thing needs to done for your program to work,Its up to you to find out)

Example: Lets say you want to add 3 numbers and print the output

#include<stdio.h>
void add(int a,int b,int c) #We can use void here as we don't need return, we can just print the result from here
{
int c=a+b+c;
printf("%d",c);
}
void main()
{
int a,b,c;
a=2;
b=3;
c=4;
add(a,b,c);

// Now lets try one more thing

a=4;
b=5; // You can change values(Hence the name variable)..:P
c=7;
add(a,b,c);
}

You can do a range of things with functions. They are just like buttons. When you call them, they start their work...

>Last topic:Structures

Pheww....One major problem of C is that it is not object oriented as java or C++ or python. What does object oriented programming mean. Well,

What if you had a class of 50 students and you would like to store their roll numbers,names,DOBs etc....That would be like totally chaotic and the number of variables would be huge...

But what if you just had 50 variables..a,b,c and so on....and just by accessing a you could access all the details of a particular student like name of the student,roll no.,DOB etc.....

that is what object oriented programming is all about. those 50 variables wont be variables but will be known as objects and you would have something called class which will contain the variables name,roll no,DOB etc...

In C, we dont have classes as such.....Thats why C doesnt really come under object oriented programming...a typical example of a class in java would be...

Class Student

{

string name;

int rollnumber;

int DOB;

}

Now in java we can store value using objects

student s= new student("somik",2,26091989);

Now s would be the object..

now say one more object is there

student p=new student("dark",13,19091989);

so now if you print s.name....you will get output as somik

if you print p.name..you will get output as dark...

C doesn't have classes but it has something same called structures..

typedef struct
{
char name[64],
char course[128],
int age,
int year,
}
student;

in main you can access it by

student s;

s.name;

Next post will tell you how to relate all this to android