Day 2 - Workshop - C Programming
Day 2 - Workshop - C Programming
Programming :
Day - 02
Day : 02
Index : Duration : 8 Hours
● C If...Else
● C Switch
● C While Loop
● C For Loop
● C Break/Continue
● C Arrays
● C Strings
● C User Input
1. C If...Else :
You can use these conditions to perform different actions for different decisions.
C has the following conditional statements:
● Use if to specify a block of code to be executed, if a specified
condition is true
● Use else to specify a block of code to be executed, if the same
condition is false
● Use else if to specify a new condition to test, if the first condition is
false
● Use switch to specify many alternative blocks of code to be executed
1.1. The if Statement : Use the if statement to specify a block of C code to be executed if a condition is true.
if (condition){
if (condition){
//// block
block of codeof code
to be to if
executed betheexecuted
condition is if
true the condition is true
}
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two variables to find out if x is greater than y. If the condition is true, print some text:
Input : Output :
int x = 20;
int y = 18;
if (x > y) {
printf("x is greater than y");
}
1.1. The if Statement : Use the if statement to specify a block of C code to be executed if a condition is true.
if (condition){
if (condition){
//// block
block of codeof code
to be to if
executed betheexecuted
condition is if
true the condition is true
}
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two variables to find out if x is greater than y. If the condition is true, print some text:
Input : Output :
int y = 18;
if (x > y) {
printf("x is greater than y");
}
1.2. Else Statement : Use the else statement to specify a block of code to be executed if the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Input : Output :
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Input : Output :
Input : Output :
Input : Output :
Input code :
Input code :
● The default keyword specifies some code to run if there is no case match.
● The while loop loops through a block of code as long as a specified condition is true:
while (condition)
{
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
Input Code :
Output :
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Note: Do not forget to increase the variable used in the condition (i++), otherwise the loop will never end!
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
Input Code :
Output :
#include <stdio.h>
0
1
int main() { 2
int i = 0; 3
while (i < 5) { 4
printf("%d\n", i);
i++;
}
return 0;
}
Note: Do not forget to increase the variable used in the condition (i++), otherwise the loop will never end!
3.2. The Do/While Loop :
● The do/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.
do
{
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the
code block is executed before the condition is tested:
Input code :
int main()
{
int i = 0;
do
{
printf("%d\n", i);
Do not forget to increase the variable used in the
i++;
condition, otherwise the loop will never end!
} while (i < 5);
return 0;
}
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the
code block is executed before the condition is tested:
Input code :
int main() 0
1
{
2
int i = 0;
3
do 4
{
printf("%d\n", i);
Do not forget to increase the variable used in the
i++;
condition, otherwise the loop will never end!
} while (i < 5);
return 0;
}
4. C For Loop :
When you know exactly how many times you want to loop through a block of code, use the
for loop instead of a while loop:
for (statement
for (statement1; 1;
statement 2; statement
statement 3) { 3)
2; statement
{ // code block to be executed
// code block to be executed
}
}
● Statement 1 is executed (one time) before the execution of the code block.
● Statement 2 defines the condition for executing the code block.
● Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
Example explained :
● Statement 1 sets a variable before the loop starts (int i = 0).
● Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again,
if it is false, the loop will end.
● Statement 3 increases a value (i++) each time the code block in the loop has been executed.
The example below will print the numbers 0 to 4:
Example explained :
● Statement 1 sets a variable before the loop starts (int i = 0).
● Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again,
if it is false, the loop will end.
● Statement 3 increases a value (i++) each time the code block in the loop has been executed.
5. C Break/Continue :
5.1. Break :
● You have already seen the break statement used earlier, It was used to "jump out" of a switch statement.
● The break statement can also be used to jump out of a loop.
Input code :
Output :
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
5. C Break/Continue :
5.1. Break :
● You have already seen the break statement used earlier, It was used to "jump out" of a switch statement.
● The break statement can also be used to jump out of a loop.
Input code :
Output :
int i; 0
1
for (i = 0; i < 10; i++) {
2
if (i == 4) { 3
break;
}
printf("%d\n", i);
}
5. C Break/Continue :
5.2. Continue :
● The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in
the loop.
Input code :
Output :
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
5. C Break/Continue :
5.2. Continue :
● The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in
the loop.
Input code :
Output :
int i; 0
1
for (i = 0; i < 10; i++) {
2
if (i == 4) { 3
continue; 5
6
}
7
printf("%d\n", i); 8
} 9
6. C Arrays :
Arrays :
● Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
● To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].
int main()
{
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
return 0;
}
● This statement accesses the value of the first element [0] in myNumbers.
6. C Arrays :
6.1. Access the Elements of an Array :
return 0;
}
● This statement accesses the value of the first element [0] in myNumbers.
6. C Arrays :
6.2. Change an Array Element :
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
printf("MyNumber[0] = %d \n", myNumbers[0]);
myNumbers[0] = 33;
printf("MyNumber[0] = %d \n", myNumbers[0]);
return 0;
}
● This statement changes the value of the first element [0] in myNumbers.
6. C Arrays :
6.2. Change an Array Element :
myNumbers[0] = 33;
printf("MyNumber[0] = %d \n", myNumbers[0]);
return 0;
}
● This statement changes the value of the first element [0] in myNumbers.
6. C Arrays :
6.3. Loop Through an Array :
● You can loop through the array elements with the for loop.
● The following example outputs all elements in the myNumbers array:
Example : Output :
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++)
{
printf("Array at location = %d : value = %d\n",
i,myNumbers[i]);
}
return 0;
}
6. C Arrays :
6.3. Loop Through an Array :
● You can loop through the array elements with the for loop.
● The following example outputs all elements in the myNumbers array:
Example : Output :
● Another common way to create arrays, is to specify the size of the array, and add elements later:
Example :
Output :
#include <stdio.h>
int main()
{
// Declare an array of four integers:
int myNumbers[4];
// Add elements to it
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
printf("%d\n", myNumbers[0]);
return 0;
}
6. C Arrays :
6.4. Set Array Size :
● Another common way to create arrays, is to specify the size of the array, and add elements later:
Example :
Output :
#include <stdio.h>
int main() 25
{
// Declare an array of four integers:
int myNumbers[4]; Note :
// Add elements to it ● Using this method, you should know the size of
myNumbers[0] = 25;
the array, in order for the program to store
enough memory.
myNumbers[1] = 50;
● You are not able to change the size of the array
myNumbers[2] = 75;
after creation.
myNumbers[3] = 100;
printf("%d\n", myNumbers[0]);
return 0;
}
7. C Strings :
Strings :
● Unlike many other programming languages, C does not have a String type to easily create string variables. However, you can use
Example : Output :
#include <stdio.h>
int main()
{
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
7. C Strings :
Example : Output :
int main()
{
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
7. C Strings :
7.1. Access Strings :
● Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].
● This example prints the first character (0) in greetings:
Example : Output :
#include <stdio.h>
int main()
{
char greetings[] = "Hello World!";
return 0;
}
7. C Strings :
7.1. Access Strings :
● Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].
● This example prints the first character (0) in greetings:
Example : Output :
return 0;
}
7. C Strings :
7.2. Modify Strings :
● To change the value of a specific character in a string, refer to the index number, and use single quotes:
Example : Output :
#include <stdio.h>
int main()
{
char greetings[] = "Hello World!";
printf("Before : %s \n ", greetings);
greetings[0] = 'J';
printf("After : %s \n", greetings);
return 0;
}
7. C Strings :
7.2. Modify Strings :
● To change the value of a specific character in a string, refer to the index number, and use single quotes:
Example : Output :
int main()
{
char greetings[] = "Hello World!";
printf("Before : %s \n ", greetings);
greetings[0] = 'J';
printf("After : %s \n", greetings);
return 0;
}
7. C Strings :
7.3. Another Way Of Creating Strings :
● In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.
● You should also note that you can to create a string with a set of characters. This example will produce the same result as the one above:
Example : Output :
#include <stdio.h>
int main()
{
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ',
'W', 'o', 'r', 'l', 'd', '!', '\0'};
}
7. C Strings :
7.3. Another Way Of Creating Strings :
● In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.
● You should also note that you can to create a string with a set of characters. This example will produce the same result as the one above:
Example : Output :
}
7. C Strings :
7.3. Another Way Of Creating Strings :
● In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.
● You should also note that you can to create a string with a set of characters. This example will produce the same result as the one above:
Example :
Differences :
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', ● The difference between the two ways of creating strings, is
'W', 'o', 'r', 'l', 'd', '!', '\0'};
that the first method is easier to write, and you do not have
char greetings2[] = "Hello World!";
to include the \0 character, as C will do it for you.
printf("%lu\n", sizeof(greetings)); // Outputs 13
● You should note that the size of both arrays is the same:
printf("%lu\n", sizeof(greetings2)); // Outputs 13
Example : Output :
#include <stdio.h>
int main()
{
// Create an integer variable that will store the number we get from
the user
int myNum;
Example : Output :
int main() {
// Create a string
char firstName[30];
return 0;
}
8. C User Input :
Example :
Output :
#include <stdio.h>
Enter your first name and press enter:
Yash
int main() { Hello Yash.
// Create a string
char firstName[30];
// Output the text it will store enough characters for the first name), and
printf("Hello %s.", firstName);
you don't have to specify the reference operator (&)
return 0;
}
when working with strings in scanf().
Thank You!!