SlideShare a Scribd company logo
1 of 24
Download to read offline
1 | P a g e
Workbook #2 {Re-view of principles printf, scanf, designing a
program, definitions, sub-routines (sub functions) with main function,
numeric, string, characters, arrays, for loop, while loop, if ā€“ else
statements}
Theory (Briefing)
2 | P a g e
3 | P a g e
4 | P a g e
5 | P a g e
6 | P a g e
Draw the IPO chart and describe each part
Describe all parts of the problem analysis
7 | P a g e
Algorithm
It is a sequence of instructions to solve a problem, written in human language.
ā€¢ A step-by-step procedure to solve a given problem.
ā€¢ An algorithm should always have a clear stopping point
ā€¢ Programmer writes the problem solving in the form of an algorithm before
coding it into computer language.
ā€¢ Algorithms for making things will divide into sections: (the
parts/components/ ingredients (inputs) required to accomplish the task, the
actions/steps/methods (processing) to produce the required
outcome(output)
ā€¢ Algorithm can be developed using: pseudo code (similar to programming
language)
ā€¢ Flow chart
8 | P a g e
9 | P a g e
Flowchart is a graphical representation of data, information and workflow using
certain symbols that are connected to flow lines to describe the instructions
done in problem solving.
10 | P a g e
11 | P a g e
Operations
Calculations and variables
There are different operators that can be used for calculations which are listed in the following
table:
Operator Operation
+ Addition
ā€“ Subtraction
* Multiplication
/ Division
%
Modulus(Remainder of integer
division)
Now that we know the different operators, letā€™s calculate something:
Example 1:
int main()
{
int a, b;
a = 1;
b = a + 1;
a = b - 1;
return 0;
}
Example 2: So letā€™s make a program that can do all these things:
#include<stdio.h>
int main()
{
int inputvalue;
scanf("%d", &inputvalue);
inputvalue = inputvalue * 10;
printf("Ten times the input equals %dn",inputvalue);
return 0;
}
12 | P a g e
Note: The input must be a whole number (integer).
Boolean Operators
Before we can take a look at test conditions we have to know what Boolean operators are. They
are called Boolean operators because they give you either true or false when you use them to
test a condition. The greater than sign ā€œ>ā€
for instance is a Boolean operator.
In the table below you see all the Boolean operators:
== Equal
! Not
!= Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
&& And
|| Or
13 | P a g e
The if-statement
The if statement can be used to test conditions so that we can alter the flow of a program. In
other words: if a specific statement is true, execute this instruction. If not true, execute this
instruction. So lets take a look at an example:
Example 3 based on if-statement
#include<stdio.h>
int main()
{
int mynumber;
scanf("%d",&mynumber);
if ( mynumber == 10 )
printf("Is equaln");
return 0;
}
Now we like to also print something if the ā€œif statementā€ is not equal. We could do this by adding
another ā€œif statementā€ but there is an easier / better way. Which is using the so called ā€œelse
statementā€ with the ā€œif statementā€.
Example 4:
#include<stdio.h>
int main()
{
int mynumber;
scanf("%d",&mynumber);
if ( mynumber == 10 )
{
printf("Is equaln");
printf("Closing programn");
}
else
{
printf("Not equaln");
printf("Closing programn");
}
return 0;
}
Note: Take a look at the placement of the curly brackets and how the indentations are placed.
This is all done to make reading easier and to make less mistakes in large programs.
14 | P a g e
Nested-if statements
If you use an ā€œif statementā€ in an ā€œif statementā€ it is called nesting. Nesting ā€œif statementsā€ can
make a program very complex, but sometimes there is no other way. So use it wisely. Take a
look at a nested ā€œif statementā€ example below:
Example 5: Nested-if statements
#include<stdio.h>
int main()
{
int grade;
scanf("%d",&grade);
if ( grade <= 10 )
{
printf("YOU DID NOT STUDY.n");
printf("YOU FAILED ! n");
}
else
{
if ( grade < 60 ) {
printf("You failed n");
printf("Study hardern");
}
else
{
if ( grade >= 60 )
printf("YOU PASSED ! n");
}
}
return 0;
}
Multiple condition testing
It is possible to test two or more conditions at once in an ā€œif statementā€ with the use of the AND
(&&) operator. Example:
if (a > 10 && b > 20 && c < 10 )
If a is greater then ten and b is greater then twenty and c is smaller then ten, do something. So
all three conditions must be true, before something happens.
With the OR ( || ) operator you can test if one of two conditions are true. Example:
if ( a = 10 || b < 20 )
15 | P a g e
If a equals ten or b is smaller then twenty then do something. So if a or b is true, something
happens.
The switch statement
The switch statement is almost the same as an ā€œif statementā€. The switch statement can have
many conditions. You start the switch statement with a condition. If one of the variable equals
the condition, the instructions are executed. It is also possible to add a default. If none of the
variable equals the condition the default will be executed. See the
example below:
Example 6:
#include<stdio.h>
int main()
{
char myinput;
printf("Which option will you choose:n");
printf("a) Program 1 n");
printf("b) Program 2 n");
scanf("%c", &myinput);
switch (myinput)
{
case 'a':
printf("Run program 1n");
break;
case 'b':
{
printf("Run program 2n");
printf("Please Waitn");
break;
}
default:
printf("Invalid choicen");
break;
}
return 0;
}
Note: break is used to exit the switch.
16 | P a g e
For-loop, While loop, Do-While loop, Break and Continue
In every programming language, thus also in the C programming language, there are
circumstances where you want to do the same thing many times. For instance you want to print
the same words ten times. You could type ten printf function, but it is easier to use a loop. The
only thing you have to do is to setup a loop that execute the same printf function ten times.
There are three basic types of loops which are:
ā–Ŗ ā€œfor loopā€
ā–Ŗ ā€œwhile loopā€
ā–Ŗ ā€œdo while loopā€
The for loop
The ā€œfor loopā€ loops from one number to another number and increases by a specified value
each time.
The ā€œfor loopā€ uses the following structure:
for (Start value; continue or end condition; increase value)
statement;
Look at the example below:
Example 7:
#include<stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf ("Hellon");
printf ("Worldn");
}
return 0;
}
Note: A single instruction can be placed behind the ā€œfor loopā€ without the curly brackets.
Letā€™s look at the ā€œfor loopā€ from the example: We first start by setting the variable i to 0. This
is where we start to count. Then we say that the for loop must run if the counter i is smaller
then ten. Last we say that every cycle i must be increased by one (i++).
In the example we used i++ which is the same as using i = i + 1. This is called incrementing.
The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i--. It is also
possible to use ++i or --i. The difference is that with ++i (prefix incrementing) the one is added
17 | P a g e
before the ā€œfor loopā€ tests if i < 10. With i++ (postfix incrementing) the one is added after the
test i < 10. In case of a for loop this make no difference, but in while loop test it makes a
difference. But before we look at a postfix and prefix increment while loop example, we first look
at the while loop.
The while loop
The while loop can be used if you donā€™t know how many times a loop must run. Here is an
example 8
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
while ( counter < howmuch)
{
counter++;
printf("%dn", counter);
}
return 0;
}
Letā€™s take a look at the example: First you must always initialize the counter before the while
loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the
variable ā€œhowmuchā€. If the input is ten, then 1 through 10 will be printed on the screen. A last
thing you have to remember is to increment the counter inside the loop (counter++). If you
forget this the loop becomes infinitive.
As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or
postfix incrementing (i++) is used with while loop. Take a look at the following postfix and prefix
increment while loop example:
Example 9
#include<stdio.h>
int main(void) {
int i;
i = 0;
while(i++ < 5) {
printf("%dn", i);
}
18 | P a g e
printf("n");
i = 0;
while(++i < 5) {
printf("%dn", i);
}
return 0;
}
The output of the postfix and prefix increment example will look like this:
1
2
3
4
5
1
2
3
4
i++ will increment the value of i, but is using the pre-incremented value to test against < 5.
Thatā€™s why we get 5 numbers.
++i will increment the value of i, but is using the incremented value to test against < 5. Thatā€™s
why we get 4 numbers.
The do while loop
The ā€œdo while loopā€ is almost the same as the while loop. The ā€œdo while loopā€ has the following
form:
do
{
do something;
}
while (expression);
Do something first and then test if we have to continue. The result is that the loop always runs
once. (Because the expression test comes afterward). Take a look at an example:
Example 10
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
do
19 | P a g e
{
counter++;
printf("%dn", counter);
}
while ( counter < howmuch);
return 0;
}
Note: There is a semi-colon behind the while line.
Break and continue
To exit a loop you can use the break statement at any time. This can be very useful if you want
to stop running a loop because a condition has been met other than the loop end condition. Take
a look at the following example:
Example 11:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
if ( i == 10)
break;
}
return 0;
}
In the example above, the while loop will run, as long i is smaller then twenty. In the while loop
there is an if statement that states that if i equals ten the while loop must stop (break).
With ā€œcontinue;ā€ it is possible to skip the rest of the commands in the current loop and start
from the top again. (the loop variable must still be incremented). Take a look at the example
below:
Example 12:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
20 | P a g e
i++;
continue;
printf("Nothing to seen");
}
return 0;
}
In the example above, the printf function is never called because of the ā€œcontinue;ā€.
Arrays and Multi-Dimensional Arrays
In this C programming language tutorial, we are going to talk about arrays.
An array lets you declare and work with a collection of values of the same type. Letā€™s say you
want to declare four integers. With the knowledge from the last few tutorials you would do
something like this:
int a , b , c , d;
What if you wanted to declare a thousand variables? That will take you a long time to type. This
is where arrays come in handy. An easier way is to declare an array of four integers:
int a[4];
The four separate integers inside this array are accessed by an index. Each element can be
accessed by using square brackets with the element number inside. All arrays start at element
zero and will go to n-1. (In this case from 0 to 3). So if we want to fill each element you get
something like this:
int a[4];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
If you want to use an element, for example for printing, you can do this:
printf("%d", a[1]);
Arrays and loops
One of the nice things about arrays is that you can use a loop to manipulate each element. When
an array is declared, the values of each element are not set to zero automatically. In some cases
you want to ā€œinitializeā€ the array (which means, setting every element to zero). This can be done
like in the example above, but it is easier to use a loop. Here is an example:
21 | P a g e
Example 13:
#include<stdio.h>
int main()
{
int a[4];
int i;
for ( i = 0; i < 4; i++ )
a[i] = 0;
for ( i = 0; i < 4; i++ )
printf("a[%d] = %dn", i , a[i]);
return 0;
}
Multi-dimensional arrays
The arrays we have been using so far are called one-dimensional arrays.
Here is an example of an one-dimensional array:
int a[2];
0 1
1 2
Note: A one-dimensional array has one column of elements.
Two-dimensional arrays have rows and columns. See the example below:
int a[2][2];
0 1
0 1 2
1 4 5
Note: a[0][0] contains the value 1. a[0][1] contains the value 2. a[1][0] contains the value 4.
a[1][1] contains the value 5.
So letā€™s look at an example that initialize a two-dimensional array and prints each element:
Example 14:
22 | P a g e
#include<stdio.h>
int main()
{
int a[4][4], i , j;
for (i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
a[i][j] = 0;
printf("a[%d][%d] = %d n", i, j, a[i][j]);
}
}
return 0;
}
Note: As you can see, we use two ā€œfor loopsā€ in the example above. One to access the rows and
the other to access the columns.
Functions and Global/Local variables
Most languages allow you to create functions of some sort. Functions are used to break up large
programs into named sections. You have already been using a function which is the main
function. Functions are often used when the same piece of code has to run multiple times.
In this case you can put this piece of code in a function and give that function a name. When
the piece of code is required you just have to call the function by its name. (So you only have
to type the piece of code once).
In the example below we declare a function with the name MyPrint. The only thing that this
function does is to print the sentence: Printing from a function. If we want to use the function
we just have to call MyPrint() and the printf statement will be executed. (Donā€™t forget to put the
round brackets behind the function name when you call it or declare it).
Take a look at the example 15:
#include<stdio.h>
void MyPrint()
{
printf("Printing from a function.n");
}
int main()
{
MyPrint();
return 0;
}
23 | P a g e
Parameters and return
Functions can accept parameters and can return a result. (C functions can accept an unlimited
number of parameters).
Where the functions are declared in your program does not matter, as long as a functions name
is known to the compiler before it is called. In other words: when there are two functions, i.e.
functions A and B, and B must call A, than A has to be declared in front of B.
Letā€™s take a look at an example where a result is returned:
Example 16
#include<stdio.h>
int Add(int output1,int output2 )
{
printf("%d", output1);
printf("%d", output2);
return output1 + output2;
}
int main()
{
int answer, input1, input2;
scanf("%d", &input1);
scanf("%d", &input2);
answer = Add(input1,input2);
printf(" answer = %dn", answer);
return 0;
}
The main() function starts with the declaration of three integers. Then the user can input two
whole numbers. These numbers are used as input of function Add(). Input1 is stored in output1
and input2 is stored in output2. The function Add() prints the two numbers onto the screen and
will return the result of output1 + output2. The return value is stored in the integer answer. The
number stored in answer is then printed onto the screen.
24 | P a g e
Void
If you donā€™t want to return a result from a function, you can use void return type instead of the
int.
So letā€™s take a look at an example of a function that will not return an integer:
void our_site()
{
printf("www");
printf(".NextDawn");
printf(".nl");
}
Note: As you can see there is not an int before our_site() and there is not a return 0; in the
function.
The function can be called by the following statement: our_site();
Global and local variables
A local variable is a variable that is declared inside a function. A global variable is a variable that
is declared outside all functions. A local variable can only be used in the function where it is
declared. A global variable can be used in all functions.
See the following example:
#include<stdio.h>
// Global variables
int A;
int B;
int Add()
{
return A + B;
}
int main()
{
int answer; // Local variable
A = 5;
B = 7;
answer = Add();
printf("%dn",answer);
return 0;
}
As you can see two global variables are declared, A and B. These variables can be used in main()
and Add().
The local variable answer can only be used in main().

More Related Content

Similar to C Programming Workbook Exercises on Loops, Conditions, and More

Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
Ā 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
Ā 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
Ā 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
Ā 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdfsantosh147365
Ā 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
Ā 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
Ā 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
Ā 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++Muhammad Hammad Waseem
Ā 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
Ā 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
Ā 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2alish sha
Ā 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
Ā 

Similar to C Programming Workbook Exercises on Loops, Conditions, and More (20)

Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
Ā 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Ā 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
Ā 
Loops c++
Loops c++Loops c++
Loops c++
Ā 
Programming basics
Programming basicsProgramming basics
Programming basics
Ā 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
Ā 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Ā 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Ā 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
Ā 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
Ā 
Elements of programming
Elements of programmingElements of programming
Elements of programming
Ā 
while loop in C.pptx
while loop in C.pptxwhile loop in C.pptx
while loop in C.pptx
Ā 
Looping
LoopingLooping
Looping
Ā 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
Ā 
175035-cse LAB-04
175035-cse LAB-04 175035-cse LAB-04
175035-cse LAB-04
Ā 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
Ā 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
Ā 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
Ā 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
Ā 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
Ā 

More from DrDineshenScientist

More from DrDineshenScientist (7)

sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 
sp95-chap3.pdf
sp95-chap3.pdfsp95-chap3.pdf
sp95-chap3.pdf
Ā 

Recently uploaded

VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...Suhani Kapoor
Ā 
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...Suhani Kapoor
Ā 
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130 Available With Room
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130  Available With RoomVIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130  Available With Room
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130 Available With Roomdivyansh0kumar0
Ā 
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁobuhobo
Ā 
Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testspriyanshukumar97908
Ā 
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...Suhani Kapoor
Ā 
PM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterPM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterHector Del Castillo, CPM, CPMM
Ā 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...Suhani Kapoor
Ā 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
Ā 
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·åŠžē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·umasea
Ā 
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen Dating
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen DatingDubai Call Girls Starlet O525547819 Call Girls Dubai Showen Dating
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen Datingkojalkojal131
Ā 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackSuhani Kapoor
Ā 
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfNPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfDivyeshPatel234692
Ā 
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...shivangimorya083
Ā 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjLewisJB
Ā 
Ioannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfIoannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfjtzach
Ā 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girlsshivangimorya083
Ā 
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...shivangimorya083
Ā 
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home MadeDubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Madekojalkojal131
Ā 
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service šŸ§³
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service  šŸ§³CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service  šŸ§³
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service šŸ§³anilsa9823
Ā 

Recently uploaded (20)

VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
VIP High Profile Call Girls Jamshedpur Aarushi 8250192130 Independent Escort ...
Ā 
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
VIP Russian Call Girls in Amravati Deepika 8250192130 Independent Escort Serv...
Ā 
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130 Available With Room
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130  Available With RoomVIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130  Available With Room
VIP Kolkata Call Girl Lake Gardens šŸ‘‰ 8250192130 Available With Room
Ā 
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ
儳ēŽ‹å¤§å­¦ē”•å£«ęƕäøščÆęˆē»©å•(åŠ ę€„åŠžē†)认čÆęµ·å¤–ęƕäøščƁ
Ā 
Notes of bca Question paper for exams and tests
Notes of bca Question paper for exams and testsNotes of bca Question paper for exams and tests
Notes of bca Question paper for exams and tests
Ā 
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
VIP Russian Call Girls Amravati Chhaya 8250192130 Independent Escort Service ...
Ā 
PM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring ChapterPM Job Search Council Info Session - PMI Silver Spring Chapter
PM Job Search Council Info Session - PMI Silver Spring Chapter
Ā 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
Ā 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
Ā 
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·åŠžē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·
办ē†å­¦ä½čƁ(ēŗ½ä¼¦å ”å¤§å­¦ę–‡å‡­čƁ书)ēŗ½ä¼¦å ”大学ęƕäøščÆęˆē»©å•åŽŸē‰ˆäø€ęØ”äø€ę ·
Ā 
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen Dating
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen DatingDubai Call Girls Starlet O525547819 Call Girls Dubai Showen Dating
Dubai Call Girls Starlet O525547819 Call Girls Dubai Showen Dating
Ā 
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service CuttackVIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
Ā 
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdfNPPE STUDY GUIDE - NOV2021_study_104040.pdf
NPPE STUDY GUIDE - NOV2021_study_104040.pdf
Ā 
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...
Full Masii Russian Call Girls In Dwarka (Delhi) 9711199012 šŸ’‹āœ”šŸ’•šŸ˜˜We are availab...
Ā 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Ā 
Ioannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdfIoannis Tzachristas Self-Presentation for MBA.pdf
Ioannis Tzachristas Self-Presentation for MBA.pdf
Ā 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Ā 
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...
Delhi Call Girls Preet Vihar 9711199171 ā˜Žāœ”šŸ‘Œāœ” Whatsapp Body to body massage wi...
Ā 
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home MadeDubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Ā 
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service šŸ§³
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service  šŸ§³CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service  šŸ§³
CALL ON āž„8923113531 šŸ”Call Girls Husainganj Lucknow best Female service šŸ§³
Ā 

C Programming Workbook Exercises on Loops, Conditions, and More

  • 1. 1 | P a g e Workbook #2 {Re-view of principles printf, scanf, designing a program, definitions, sub-routines (sub functions) with main function, numeric, string, characters, arrays, for loop, while loop, if ā€“ else statements} Theory (Briefing)
  • 2. 2 | P a g e
  • 3. 3 | P a g e
  • 4. 4 | P a g e
  • 5. 5 | P a g e
  • 6. 6 | P a g e Draw the IPO chart and describe each part Describe all parts of the problem analysis
  • 7. 7 | P a g e Algorithm It is a sequence of instructions to solve a problem, written in human language. ā€¢ A step-by-step procedure to solve a given problem. ā€¢ An algorithm should always have a clear stopping point ā€¢ Programmer writes the problem solving in the form of an algorithm before coding it into computer language. ā€¢ Algorithms for making things will divide into sections: (the parts/components/ ingredients (inputs) required to accomplish the task, the actions/steps/methods (processing) to produce the required outcome(output) ā€¢ Algorithm can be developed using: pseudo code (similar to programming language) ā€¢ Flow chart
  • 8. 8 | P a g e
  • 9. 9 | P a g e Flowchart is a graphical representation of data, information and workflow using certain symbols that are connected to flow lines to describe the instructions done in problem solving.
  • 10. 10 | P a g e
  • 11. 11 | P a g e Operations Calculations and variables There are different operators that can be used for calculations which are listed in the following table: Operator Operation + Addition ā€“ Subtraction * Multiplication / Division % Modulus(Remainder of integer division) Now that we know the different operators, letā€™s calculate something: Example 1: int main() { int a, b; a = 1; b = a + 1; a = b - 1; return 0; } Example 2: So letā€™s make a program that can do all these things: #include<stdio.h> int main() { int inputvalue; scanf("%d", &inputvalue); inputvalue = inputvalue * 10; printf("Ten times the input equals %dn",inputvalue); return 0; }
  • 12. 12 | P a g e Note: The input must be a whole number (integer). Boolean Operators Before we can take a look at test conditions we have to know what Boolean operators are. They are called Boolean operators because they give you either true or false when you use them to test a condition. The greater than sign ā€œ>ā€ for instance is a Boolean operator. In the table below you see all the Boolean operators: == Equal ! Not != Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal && And || Or
  • 13. 13 | P a g e The if-statement The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction. So lets take a look at an example: Example 3 based on if-statement #include<stdio.h> int main() { int mynumber; scanf("%d",&mynumber); if ( mynumber == 10 ) printf("Is equaln"); return 0; } Now we like to also print something if the ā€œif statementā€ is not equal. We could do this by adding another ā€œif statementā€ but there is an easier / better way. Which is using the so called ā€œelse statementā€ with the ā€œif statementā€. Example 4: #include<stdio.h> int main() { int mynumber; scanf("%d",&mynumber); if ( mynumber == 10 ) { printf("Is equaln"); printf("Closing programn"); } else { printf("Not equaln"); printf("Closing programn"); } return 0; } Note: Take a look at the placement of the curly brackets and how the indentations are placed. This is all done to make reading easier and to make less mistakes in large programs.
  • 14. 14 | P a g e Nested-if statements If you use an ā€œif statementā€ in an ā€œif statementā€ it is called nesting. Nesting ā€œif statementsā€ can make a program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested ā€œif statementā€ example below: Example 5: Nested-if statements #include<stdio.h> int main() { int grade; scanf("%d",&grade); if ( grade <= 10 ) { printf("YOU DID NOT STUDY.n"); printf("YOU FAILED ! n"); } else { if ( grade < 60 ) { printf("You failed n"); printf("Study hardern"); } else { if ( grade >= 60 ) printf("YOU PASSED ! n"); } } return 0; } Multiple condition testing It is possible to test two or more conditions at once in an ā€œif statementā€ with the use of the AND (&&) operator. Example: if (a > 10 && b > 20 && c < 10 ) If a is greater then ten and b is greater then twenty and c is smaller then ten, do something. So all three conditions must be true, before something happens. With the OR ( || ) operator you can test if one of two conditions are true. Example: if ( a = 10 || b < 20 )
  • 15. 15 | P a g e If a equals ten or b is smaller then twenty then do something. So if a or b is true, something happens. The switch statement The switch statement is almost the same as an ā€œif statementā€. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default. If none of the variable equals the condition the default will be executed. See the example below: Example 6: #include<stdio.h> int main() { char myinput; printf("Which option will you choose:n"); printf("a) Program 1 n"); printf("b) Program 2 n"); scanf("%c", &myinput); switch (myinput) { case 'a': printf("Run program 1n"); break; case 'b': { printf("Run program 2n"); printf("Please Waitn"); break; } default: printf("Invalid choicen"); break; } return 0; } Note: break is used to exit the switch.
  • 16. 16 | P a g e For-loop, While loop, Do-While loop, Break and Continue In every programming language, thus also in the C programming language, there are circumstances where you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf function, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times. There are three basic types of loops which are: ā–Ŗ ā€œfor loopā€ ā–Ŗ ā€œwhile loopā€ ā–Ŗ ā€œdo while loopā€ The for loop The ā€œfor loopā€ loops from one number to another number and increases by a specified value each time. The ā€œfor loopā€ uses the following structure: for (Start value; continue or end condition; increase value) statement; Look at the example below: Example 7: #include<stdio.h> int main() { int i; for (i = 0; i < 10; i++) { printf ("Hellon"); printf ("Worldn"); } return 0; } Note: A single instruction can be placed behind the ā€œfor loopā€ without the curly brackets. Letā€™s look at the ā€œfor loopā€ from the example: We first start by setting the variable i to 0. This is where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++). In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i--. It is also possible to use ++i or --i. The difference is that with ++i (prefix incrementing) the one is added
  • 17. 17 | P a g e before the ā€œfor loopā€ tests if i < 10. With i++ (postfix incrementing) the one is added after the test i < 10. In case of a for loop this make no difference, but in while loop test it makes a difference. But before we look at a postfix and prefix increment while loop example, we first look at the while loop. The while loop The while loop can be used if you donā€™t know how many times a loop must run. Here is an example 8 #include<stdio.h> int main() { int counter, howmuch; scanf("%d", &howmuch); counter = 0; while ( counter < howmuch) { counter++; printf("%dn", counter); } return 0; } Letā€™s take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable ā€œhowmuchā€. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive. As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment while loop example: Example 9 #include<stdio.h> int main(void) { int i; i = 0; while(i++ < 5) { printf("%dn", i); }
  • 18. 18 | P a g e printf("n"); i = 0; while(++i < 5) { printf("%dn", i); } return 0; } The output of the postfix and prefix increment example will look like this: 1 2 3 4 5 1 2 3 4 i++ will increment the value of i, but is using the pre-incremented value to test against < 5. Thatā€™s why we get 5 numbers. ++i will increment the value of i, but is using the incremented value to test against < 5. Thatā€™s why we get 4 numbers. The do while loop The ā€œdo while loopā€ is almost the same as the while loop. The ā€œdo while loopā€ has the following form: do { do something; } while (expression); Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example: Example 10 #include<stdio.h> int main() { int counter, howmuch; scanf("%d", &howmuch); counter = 0; do
  • 19. 19 | P a g e { counter++; printf("%dn", counter); } while ( counter < howmuch); return 0; } Note: There is a semi-colon behind the while line. Break and continue To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following example: Example 11: #include<stdio.h> int main() { int i; i = 0; while ( i < 20 ) { i++; if ( i == 10) break; } return 0; } In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break). With ā€œcontinue;ā€ it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below: Example 12: #include<stdio.h> int main() { int i; i = 0; while ( i < 20 ) {
  • 20. 20 | P a g e i++; continue; printf("Nothing to seen"); } return 0; } In the example above, the printf function is never called because of the ā€œcontinue;ā€. Arrays and Multi-Dimensional Arrays In this C programming language tutorial, we are going to talk about arrays. An array lets you declare and work with a collection of values of the same type. Letā€™s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this: int a , b , c , d; What if you wanted to declare a thousand variables? That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers: int a[4]; The four separate integers inside this array are accessed by an index. Each element can be accessed by using square brackets with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3). So if we want to fill each element you get something like this: int a[4]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; If you want to use an element, for example for printing, you can do this: printf("%d", a[1]); Arrays and loops One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically. In some cases you want to ā€œinitializeā€ the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example:
  • 21. 21 | P a g e Example 13: #include<stdio.h> int main() { int a[4]; int i; for ( i = 0; i < 4; i++ ) a[i] = 0; for ( i = 0; i < 4; i++ ) printf("a[%d] = %dn", i , a[i]); return 0; } Multi-dimensional arrays The arrays we have been using so far are called one-dimensional arrays. Here is an example of an one-dimensional array: int a[2]; 0 1 1 2 Note: A one-dimensional array has one column of elements. Two-dimensional arrays have rows and columns. See the example below: int a[2][2]; 0 1 0 1 2 1 4 5 Note: a[0][0] contains the value 1. a[0][1] contains the value 2. a[1][0] contains the value 4. a[1][1] contains the value 5. So letā€™s look at an example that initialize a two-dimensional array and prints each element: Example 14:
  • 22. 22 | P a g e #include<stdio.h> int main() { int a[4][4], i , j; for (i = 0; i < 4; i++) { for ( j = 0; j < 4; j++) { a[i][j] = 0; printf("a[%d][%d] = %d n", i, j, a[i][j]); } } return 0; } Note: As you can see, we use two ā€œfor loopsā€ in the example above. One to access the rows and the other to access the columns. Functions and Global/Local variables Most languages allow you to create functions of some sort. Functions are used to break up large programs into named sections. You have already been using a function which is the main function. Functions are often used when the same piece of code has to run multiple times. In this case you can put this piece of code in a function and give that function a name. When the piece of code is required you just have to call the function by its name. (So you only have to type the piece of code once). In the example below we declare a function with the name MyPrint. The only thing that this function does is to print the sentence: Printing from a function. If we want to use the function we just have to call MyPrint() and the printf statement will be executed. (Donā€™t forget to put the round brackets behind the function name when you call it or declare it). Take a look at the example 15: #include<stdio.h> void MyPrint() { printf("Printing from a function.n"); } int main() { MyPrint(); return 0; }
  • 23. 23 | P a g e Parameters and return Functions can accept parameters and can return a result. (C functions can accept an unlimited number of parameters). Where the functions are declared in your program does not matter, as long as a functions name is known to the compiler before it is called. In other words: when there are two functions, i.e. functions A and B, and B must call A, than A has to be declared in front of B. Letā€™s take a look at an example where a result is returned: Example 16 #include<stdio.h> int Add(int output1,int output2 ) { printf("%d", output1); printf("%d", output2); return output1 + output2; } int main() { int answer, input1, input2; scanf("%d", &input1); scanf("%d", &input2); answer = Add(input1,input2); printf(" answer = %dn", answer); return 0; } The main() function starts with the declaration of three integers. Then the user can input two whole numbers. These numbers are used as input of function Add(). Input1 is stored in output1 and input2 is stored in output2. The function Add() prints the two numbers onto the screen and will return the result of output1 + output2. The return value is stored in the integer answer. The number stored in answer is then printed onto the screen.
  • 24. 24 | P a g e Void If you donā€™t want to return a result from a function, you can use void return type instead of the int. So letā€™s take a look at an example of a function that will not return an integer: void our_site() { printf("www"); printf(".NextDawn"); printf(".nl"); } Note: As you can see there is not an int before our_site() and there is not a return 0; in the function. The function can be called by the following statement: our_site(); Global and local variables A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions. See the following example: #include<stdio.h> // Global variables int A; int B; int Add() { return A + B; } int main() { int answer; // Local variable A = 5; B = 7; answer = Add(); printf("%dn",answer); return 0; } As you can see two global variables are declared, A and B. These variables can be used in main() and Add(). The local variable answer can only be used in main().