Beginners Programming :Conditionals+Loops+Arrays

Geetika Kaushik
4 min readJan 7, 2022

Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.

if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}

Nested If..else statement

When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if else statement:

if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}

Important Points:
1. else and else..if are optional statements, a program having only “if” statement would run fine.
2. else and else..if cannot be used without the “if”.
3. There can be any number of else..if statement in a if else..if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (&&), OR(||) and NOT(!).

For loop

This is one of the most frequently used loop in C programming.
Syntax of for loop:

for (initialization; condition test; increment or decrement)
{
//Statements to be executed repeatedly
}

Step 1: First initialization happens and the counter variable gets initialized.
Step 2: In the second step the condition is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop.
Step 3: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –).

While loop

Syntax of while loop:

while (condition test)
{
//Statements to be executed repeatedly
// Increment (++) or Decrement (--) Operation
}

Example of while loop

#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}

Use of Logical operators in while loop

Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in while loop. The following scenarios are valid :

while(num1<=10 && num2<=10)

-using AND(&&) operator, which means both the conditions should be true.

while(num1<=10||num2<=10)

– OR(||) operator, this loop will run until both conditions return false.

while(num1!=num2 &&num1 <=num2)

– Here we are using two logical operators NOT (!) and AND(&&).

while(num1!=10 ||num2>=num1)

Why we need Array ?

Consider a scenario where you need to find out the average of 100 integer numbers entered by user. In C, you have two ways to do this: 1) Define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them. 2) Have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average.

Which solution is better according to you? Obviously the second solution, it is convenient to store same data types in one single variable and later access them using array index (we will discuss that later in this tutorial).

How to declare Array

int num[35];  /* An integer array of 35 elements */
char ch[10]; /* An array of characters for 10 elements */

Array in C++:

int arr[5];

arr[0] = 5;

arr[2] = -10;

// this is same as arr[1] = 2

arr[3 / 2] = 2;

arr[3] = arr[0];

cout << arr[0] << " " << arr[1] << " " << arr[2] << " "

<< arr[3];

Arrray declaration in Java:

int Array[]; //declaring array
intArray = new int[20]; // allocating memory to array

Array declaration in Python:

cars = [“Ford”, “Volvo”, “BMW”]

Similarly an array can be of any data type such as double, float, short etc.

Various ways to initialize an array

In the above example, we have just declared the array and later we initialized it with the values input by user. However you can also initialize the array during declaration like this:

int arr[5] = {1, 2, 3, 4 ,5};

OR (both are same)

int arr[] = {1, 2, 3, 4, 5};

Un-initialized array always contain garbage values.

--

--