Sunday, 31 March 2019

C Programming Break and Continue statements

The break and continue statements in C language are used to alter the normal flow of execution of program.
Break statement
  • Used with loops and switch statements
  • When encountered, it breaks the execution of block in which break statement is used and program execution starts from the first statement flowing the block in which break statement is used.
Example:
#include<stdio.h>
int main(){
            int arr[5]={10,8,9,6,5};
            int i,key=9;
           
            //use of break statement
            for(i=0;i<5;i++){
                        if(arr[i]==key){
                                    printf("Element %d found at index %d.\n",key,i);
                                    break;
                        }
            }
            return 0;
}
Output:


Continue statement
  • Used with loops.
  • Usually used with if statement
  • When encountered, it breaks the execution of current iteration and start next iteration.
Example:
#include<stdio.h>
int main(){
            int arr[5]={10,8,9,6,5};
            int i,key=9;
            //use of continue statement
            for(i=1;i<=10;i++){
                        if(i%2==0){
                                    continue;
                        }
                        printf("\n%d",i);
            }
            return 0;
}
Output:


Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Friday, 29 March 2019

C Loops - For loop, while loop and do while loop

You may encounter situations, when a block of code needs to be executed several number of times. A loop is used for executing a block of statements repeatedly until a given condition returns false.
There are three types of loop constructs in C. These are as follows-
(1). for loop
(2). while loop
(3). do while loop

(1).for loop
It is used mainly when number of iterations (number of times loop will execute) are known beforehand.
Syntax of for loop
The for loop can be written in several ways.
Syntax 1:
for(initialization;test_condition;updateStatement){
   //loop body
}

Syntax 2:
Initialization;
for(;test_condition;updateStatement){
   //loop body
}

Syntax 3:
initialization
for(;test_condition;){
   //loop body
updateStatement;
}

Note : updateStatement means increment or decrement statement

How for loop works?
  • The initialization statement is executed only once.
  • Then, the test_condition is evaluated. If the test_condition is false (0), for loop is terminated.
  • But if the test_condition is true (nonzero), codes inside the body of for loop is executed and the updateStatement is updated.

This process repeats until the test_condition is false.
Examples:
//program to print integer number from 1 to 10 in ascending order
#include<stdio.h>
void main(){
  for(int i=1;i<=10;i++){
     printf(“%d\t”,i);
   }
}
Output: 1 2 3 4 5 6 7 8 9 10
//program to print integer number from 1 to 10 in descending order
#include<stdio.h>
void main(){
  for(int i=10;i>=1;i--){
     printf(“%d\t”,i);
   }
}
Output: 10 9 8 7 6 5 4 3 2 1

(2). While loop
The while statement is used when the program needs to perform repetitive tasks.
The general form of a while statement is:

while (condition){
statement;
}
The program will repeatedly execute the statement inside the while until the condition becomes false (0). (If the condition is initially false, the statement will not be executed.)
 How while loop works?

  • The while loop evaluates the condition.
  • If the condition is true (nonzero), codes inside the body of while loop is executed. The condition is evaluated again. The process goes on until the condition is false.
  • When the condition is false, the while loop is terminated.


Example :
//program to print numbers from 1 to 10 in ascending order
#include<stdio.h>
void main(){
int i=1;
while(i<=10){
  printf(“%d\t”,i);
  i++;
 }
}

(3).do while loop
The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.

Syntax:
initialization;
do{
  //loop body
updateExpression;
}while(testExpression);

How do...while loop works?
  • The code block (loop body) inside the braces is executed once.
  • Then, the testExpression is evaluated. If the testExpression is true, the loop body is executed again. This process goes on until the testExpression is evaluated to 0 (false).
  • When the testExpression is false (nonzero), the do...while loop is terminated.

·
Example:
//program to print numbers from 1 to 10.
#include<stdio.h>
void main(){
  int i=1;
do{
  printf(“%d\t”,i);
  i++;
  }while(i<=10);
}

Above discussed loops can be categorized into two groups based on-when test condition is tested.
(1). Entry controlled loop
  • The types of loop where the test condition is stated before the body of the loop, are known as the entry controlled loop.     
  • In this category of loops, test condition is evaluated before entering into the loop body.
  • Example- for loop and while loop.


(2). Exit controlled loop
  • The types of loop where the test condition is stated at the end of the body of the loop, are known as the exit controlled loops.
  • So, in the case of the exit controlled loops, the body of the loop gets execution without testing the given condition for the first time.
  • Then the condition is tested. If it comes true, then the loop gets another execution and continues till the result of the test condition is not false.
  • Example : do while loop


Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Thursday, 28 March 2019

How to Create a User and Grant Permissions in Oracle


Oracle comes with in-built database user like system and sys. But situation may arise where you want to create a separate user. You may want to create application specific users which may not need all privileges system and sys users have.
Step 1: Connect to oracle using system user with sysdba privilege.
SQLPLUS SYSTEM/password@sid AS SYSDBA;



Step 2 : Once your are connected to oracle, issue following command to create user
CREATE USER username IDENTIFIED BY password;


Replace username and password with your desired username and password.

Step 3: Once you have created user successfully, it may require certain permissions to do its job. You use GRANT statement to assign privileges.
Grant connect to test;
Grant resource to test;
Grant dba to test;
Grant create session to test;
Grant unlimited tablespace to tset;
Grant create view to test;
Grant create table to test;
Grant select on tableName to test;
Grant update on tableName to test;
Grant insert on tableName to test;
Grant delete on tableName to test;

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Wednesday, 27 March 2019

Switch Statement in C

Switch case statements are a substitute for long if statements that compare a variable to several integral values. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax:
switch(expression){
    case  value1 :              // code to be executed if expression = value1;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value2 :     // code to be executed if expression = value2;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value3 :   //// code to be executed if expression = value3;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value4 :      // code to be executed if expression = value4;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value5 :        // code to be executed if expression = value5;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  valueN :  //// code to be executed if expression = valueN;
              statement 1;
               ……………
              ………………
              Break;//optional

  Default :   // code to be executed if value of expression doesn't match any cases
             Statement1;
            ………………………
           
}

Important Points about Switch Case Statements:
  1. The expression provided in the switch should result in a constant value otherwise it would not be valid.
  2. Duplicate case values are not allowed.
  3. The default statement is optional.
  4. The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  5. The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
  6. The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  7. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  8. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Example:
//program to create simple calculator
#include<stdio.h>
int main(){
   int num1,num2,result;
   char operator; 
  
   printf("Enter operator(+,-,*,/,%):\n");
   scanf("%c",&operator);
   printf("Enter two positive integers:\n");
   scanf("%d %d",&num1,&num2);

  switch(operator){
    case '+' :
               result=num1+num2;
               break;
    case '-' :
               result=num1-num2;
               break;

    case '*' :
               result=num1*num2;
               break;

    case '/' :
               result=num1/num2;
              
               break;
    case '%' :
               result=num1%num2;
              
               break;
    default :
               printf("Invalid operator!");
  }
   
  printf("Result of %c on %d and %d is %d.\n",operator,num1,num2,result);
  return 0;
}
Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 26 March 2019

C Program to Check Whether a Character is Vowel or Consonant


This program demonstrates the functioning of if else statements. Program asks user for an alphabet and gives the output whether alphabet is vowel or consonant.
The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
//C Program to Check Whether a alphabet is Vowel or Consonant
#include <stdio.h>
int main()
{
    char ch;
    printf("Enter an alphabet: ");
    scanf("%c",&ch);
     if((ch=='a' || ch=='A') || (ch=='e' || ch=='E') || (ch=='i' || ch=='I') || (ch=='o' || ch=='O') || (ch=='u' || ch=='U'))
        printf("%c is a vowel.\n", ch);
    else
        printf("%c is a consonant.\n", ch);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.

C Program to check whether a number is even or odd

This program demonstrates the functioning of if else statements. Program asks user for an integer number and gives the output whether number is even or odd.
An integer is even if it is divisible by 2 meaning if by dividing number by 2, remainder is zero, then number is even otherwise it is an odd number.
//C Program to demonstrate the functioning of if else statements
//check if number is divisible by 2. If it is, then number is even otherwise it is odd number
#include <stdio.h>
int main()
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    if(num % 2 == 0)
        printf("%d is even number.\n", num);
    else
        printf("%d is odd number.\n", num);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.