Skip to main content

Posts

Showing posts from September, 2016

Introduction To C++ Programming - Part 1.

Hello World: // File name: hello_world.cpp // A simple C++ program which prints "Hello World!" on the screen. #include <iostream>        // header file to support the C++ I/O system. using namespace std;     // telling the compiler to use namespace "std", // where the entire C++ library is declared. int main() { cout << "Hello World!" <<  endl; /* *  cout is the standard output device on the screen. *  << causes the expression on its right to be directed to the device on its left. *  return 0; indicates the successful termination of the "main" function. */ return 0; } Output: Hello World! Variables And Constants: // File name: variable.cpp // Demonstrate the use of variables and constants. #include <iostream> using namespace std; const int CONST_VAL = 5; // declaring a constant. Its value can't be changed.

The Basics Of C Programming - Part 6.

You may encounter situations, when a block of code needs to be executed multiple times. Loops are used in programming languages to repeat a specific block until some end condition is met. There are three loops in C programming: 1.    for loop 2.    while loop 3.    do...while loop A while loop in C programming repeatedly executes a target statement as long as a given condition is true. The syntax of a while loop is: while(condition)  { statement(s); } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and if the condition is true (nonzero), codes inside the body of while loop is evaluated. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop. The key point to note is that a  while  loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the firs

The Basics Of C Programming - Part 5.

Conditional statements are used to execute a statement or a group of statement based on certain conditions. In the C programming language, you can typically use the following conditional statements: 1.    if statement 2.    if-else statement 3.    ternary statement or ternary operator 4.    nested if…else statement 5.    switch statement The syntax of an if statement is: if(boolean_expression) { /* statement(s) to execute if the boolean expression is true */ } If the Boolean expression is true, code inside the if statement is executed. If the Boolean expression is false, then the first set of code after the end of the if statement (after the closing curly brace) is executed. Example: #include <stdio.h> int main () { int a = 10; if( a < 20 ) { printf("a is less than 20\n"); } printf("value of a is %d\n", a); return 0; } Output: a is less than 20 value of a is 10

The Basics Of C Programming - Part 4.

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators: 1.    Arithmetic Operators 2.    Relational Operators 3.    Logical Operators 4.    Bitwise Operators 5.    Assignment Operators 6.    Misc. Operators We will look into the way each operator works in the below: Arithmetic Operators The following table shows all the arithmetic operators supported by C language. Assume variable a holds 10 and variable b holds 20 then: Operator Description Example + Adds two operands. a + b = 30 − Subtracts second operand from the first. a − b = -10 * Multiplies both operands. a * b = 200 / Divides numerator by de-numerator. b / a = 2 % Modulus Operator and remainder of after an integer division. b % a = 0 ++