Programming Fundamentals: C++ language Course

Course Content:
  • Introduction
  • Arithmetic operators
  • Logical operators
  • Conditional operators
  • Conditional statements
  • If
  • If else
  • If else if
  • Nested if and nested if else
  • Switch
Paper's Marking:
5 marks = true false
5 marks = MCQs
5 marks = Debugging(Error Finding)
5 marks = code
5 marks = code

Introduction 

Computer

  A computer is a device that accepts information (in the form of digitalized data) and manipulates it for some result based on a programsoftware, or sequence of instructions on how the data is to be processed.

Software/Program


  • A program is a set of instructions in proper sequence, that causes a computer to perform a particular task.
  • The programs that run on a computer are referred to as software.  

Programming Languages

Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps.

These may be divided into three general types:
  1. Machine languages
  2. Assembly languages
  3. High-level languages
  4. Procedural Languages
  5. Object Oriented Languages
Machine Language
Machine code, also known as machine language, is the elemental language of computers. It is read by the computer's central processing unit (CPU), is composed of digital binary numbers, and looks like a very long sequence of zeros and ones.
Assembly Language
An assembly language statement is a line of text that translates into a single-machine instruction. Assembly Language is expressed in a more human-readable form than binary instructions and names are allowed for memory locations, registers, operations, etc. For example, ADD [result],[coursework],[exam].

High-Level Language

A high-level language is any programming language that enables the development of a program in a much more user-friendly programming context and is generally independent of the computer's hardware architecture.
Procedural Language

A procedural language is a type of computer programming language that specifies a series of well-structured steps and procedures within its programming context to compose a program.

Object Oriented Language
Object-oriented programming is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields, and the code is in the form of procedures. A common feature of objects is that procedures are attached to them and can access and modify the object's data fields

Operators

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction.

Operators in C++ can be classified into 6 types:

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators


1. C++ Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data in c+. For example,

x + y;
Here, the + operator is used to add two variables x and y. Similarly, there are various other arithmetic operators in C++.

Operator Operation
  +                   Addition
  -                Subtraction
  *               Multiplication
  /                       Division
 %             Modulo Operation (Remainder after division)


Increment and Decrement Operators

C++ also provides increment and decrement operators: ++ and -- respectively.

++ increases the value of the operand by 1
-- decreases it by 1

Example:
#include<iostream>
using namespace std;
int main()
{
int i=5;
i++;
cout<<"The value of i after increment: "<<i;
return 0;
}
Output:
The value of i after increment: 6

2. Assignment Operators

Assignment operators are used to assign values to variables.
Example:
num = 12;
age = 18;
marks = 89;

Operator Example Equivalent to
    +=          a += b;            a = a + b;
    -=          a -= b;            a = a - b;
    *=          a *= b;            a = a * b;
     =           a = b;            a = b;
    %=          a %= b;            a = a % b;
    /=          a /= b;            a = a / b;

3. Relational/Conditional Operators

In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality and inequalities.
 
Examples:
if(a>b)
if(a <= b)
if (a == 3)

Operator Meaning                                  Example
     ==        Is Equal To                             12 == 15 gives us false
     !=        Not Equal To                           12 != 5 gives us true
     >                Greater Than            12 > 5 gives us false
     <                Less Than                                 12< 5 gives us true
     >=        Greater Than or Equal To          12 >= 5 gives us false
     <=        Less Than or Equal To          12 <= 5 gives us true


4. Logical Operators

A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.


Operator             Example                      Meaning
     &&     expression1 && expression2      Logical AND.

        True only if all the operands are true.

     ||         expression1 || expression2            Logical OR.

True if at least one of the operands is true.

     !                            ! expression                        Logical NOT.

True only if the operand is false.


Conditional Statements

A conditional statement tells a program to execute an action depending on whether a condition is true or false. It is often represented as an if-then or if-then-else statement. The example above is a sequence of code that uses a conditional statement known as an "if/then" block.

if

If statements are logical blocks used within programming. They're conditional statements that tell a computer what to do with certain information. In other words, they let a program make 'decisions' while it's running. They're comprised of a minimum of two parts, 'if' and 'then'.

Syntax:
if(condition)
{
statement(s);
}

Flowchart:


if-else

An if-else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false.

Syntax:
if(condition)
{
statement(s);
else
{
statement(s);
}

Flowchart:



if-else if

IF-ELSE-IF: if the condition is true, then the block of code inside the IF statement is executed. After that, the ELSE-IF block is checked. If the condition associated with this is true, then the block of statements inside this ELSE-IF block is executed. Otherwise, the statement inside the ELSE block is executed.

Syntax:
if(condition)
{
statement(s);
else if (condition)
{
statement(s);
}

Flowchart:


Nested if / if-else


It means an if/ if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if–else statement is present and such type of statement is known as a nested if / if-else statement. We can use one if or else if statement inside another if or else if statements

Syntax:
if(condition)
{
      if(condition)
{
  statement(s);
} else 
    {
       statement(s);
    }
 
}

Flowchart:
Nested if-else:

Nested if:



Switch

A switch statement is an alternative of an if else/ if else if() Statement. It verifies different conditions for a single variable.

Keywords
Switch
Case
Break
default

Syntax

Switch(expression)
{
Case1  ‘expression’:
        statement(s)
             .
             .
              break;
Case n ‘expression’:
     statement(s)
             .
             .
             break;
Default:
    statement(s)

}

Flowchart:

Programs:

General Programs: Click here

Performed in Class: Click Here  (Loop programs are not included)

Error Tracing

Program # 1

Hello World!

#include <iOstream>
using namspace std;
int main();
{
      cout<<Hello World!";
return 0;

}

Program # 2

Sum of Characters

#inlude <iostream.h>
using namespace std;
int main()
{
       char  ch1ch2; sum;
ch1 = '2';
ch2 == '6';
sem=ch1+ch2;
      cout<<"Sum =  "<< sum;
return 0;

}


Program # 3

Use of Comments

#include <iostandard>
using NameSpace std
int main()
{
/*
            Name: Hamza Asif
            Date: 20/06/22
*/
      cout<<"This is My First Program.",
return 0;
}


Program # 4

Use of Increment Operator

#include <iostream>
using namespace std;
int main()


       Int IjkL;
       = 10;
       = ++j;
       = 10;
       k++;     
       cout>>"is  \n"<< i;
       cout>>"is  \n"<<j;
       cout<<"is \n"<<k;
       cout<<"is  "<<L;

return 0
}


Program # 5

Print Area of Square

#include <iostream>
using namespace std
int main();
{
       string  heightwidtharea;
height = 4
width = 4;
areaheight width;
      cout<<"Area of square =  "<< area;
return 0;




Program # 6

Sum of two floating numbers

&include <iostream>
using namespace stdio;
int main()
{
        float num1num2sum;
num1 = 24.27;
num2 = 41.50;
      cout<<"Sum =  " som;

return 0;

}



Program # 7

Use of Escape sequence

#incude <HOD>
using namespace std;
int main()
{
cout<<"\n new line escape sequence tutorial";
cout<<\t 15 \t 400
cout>>"\n Second line \n";
return 0;

}

Program # 8

Convert Kilometers into Meters

#include <iostream>;
using namespace std;
int main()
{
        char meterkilometer,
        cout<<"Please enter the distance in Kilometers>";
       cin>>kilometer;
       meter kilometer * 1000,
       cout<<"Given Kms in meters =  " <<meter;

Return 0;

}


Program # 9

Maximum between 3 numbers

#include <iostream>
notusing namespace std;
int main(0)
{
        int ab, c;
        cout<<"Please enter three numbers\n";
        cin>>a>>b>>c;
       if(ab &| ac)
               {      
                    cout<<a<<" is the maximum number"
                } else  if (c &&  b>a)
                        {
                             cout<<b<<" is the maximum number";
                         }  else if (b < c && ac
                                  {
                                      cout<<c<<" is the maximum number",
                                  else 
                                       {
                                        cout<<" Invalid Input";
                                        }    
return 0;          


Program # 10


The number is negative, positive, or zero.

!include <iostream>
using namespace std;
int main();
{
        int a
        cout<<"Please enter a number\n";
       cin<<a;
       if(a == 0)
               {      
                   cout<<a<< " is Zero";
                } then  if (a > 0)
                        {
                              cout<<a<<" is the positive number";
                         }  else if (a < 0
                                  {
                                      cout<<a<<" is the negative number";
                                  else 
                                       {
                                       cout<<"Please Enter the Number";
                                               
return 0      
}



Program # 11

The number is Even or Odd.

#include <iostream>
using space std;
int main()
{
        int num;
       cout<<"Please enter a numbers\n";
       cin>>num;
       if(num % 2 == 0);
               {      
                  cout<<num<<" is an Even number";
                } else  if (! num  % 2 == 0);
                        {
                           cout<<num<<" is an Od number";
                         }   else 
                                       {
                                        cout<<"Please Enter the valid Number",
                                        }  
return 0;            
}


Program # 12

Year is leap or not

#include <voidmain>
using namespace std;
int main()
{
        int year
        cout<<"Please enter a year\n";
       cin>>year;
       if(year % 4 == 0)
               {      
                   cout<<year<<" is leap year"
                   else 
                           
                                cout<<year<<" is not a leap year";
                           }      
eturn 0;        


Program # 13

Find Factorial of any Number

#include <iostream>
using namespace std;
int main()
{
        int float a=1f=2n;
     cout<<"Enter any Number\n";
       cin>>n;
while(a <= n);
{
ff*a;
a++,
cout<<"Factorial=  "<<f;

return 0
}

Program # 14

Enter the day number to print the day name

$include <iostream>
UsiNg namespace std;
int main();

        int day;
        cout<<"Please Enter day Number\n";
       cin<<day;
       if(day == 1)
               {      
                    cout<<"The day is Monday";
                } else  if (day == 2)
                        {
                             cout<<"The day is Tuesday";
                         }   else  if (day == 3)
                                       
                                             cout<<"The day is Wednesday";
                                       } 
            else  if (day == 4)
               {
                      cout<<"The day is Thursday";
                
 else  if (day == 5)
   {
      cout<<"The day is Friday";
    else  if (day == 6)
              {
                   cout<<"The day is Saturday";
               }         
                    else  if (day == 7)
                        {
                           cout>>"The day is Sunday";
                              
             else 
               {
                 cout<<"Please enter Valid day Number";  
               }
return 0;
}

Correct Programs: Here


Program: To sperate digits from input

#include<iostream>
using namespace std;
int main()
{
int a, b, c,value,new1,new2;
cout<<"Enter number"<<endl;
cin>>value;
c=value%10;
new1=value/10;
b=new1%10;
new2=new1/10;
a=new2%10;
cout<<a<<endl<<b<<endl<<c;
return 0;
}


Post a Comment

4 Comments