You are on page 1of 29

SAP EXPERTS CENTER

C++- Lecture 01 – C# Overview + Basic Syntax


SAP EXPERTS CENTER
TABLE OF CONTENTS

- C# Overview - Math
- Syntax - Booleans
- Output - Conditions
- Comments - Switch
- User Input - While Loop
- Data Types - For Loop
- Strings - Break / Continue
SAP EXPERTS CENTER
TABLE OF CONTENTS
Programming Language
SAP EXPERTS CENTER
TABLE OF CONTENTS

The main difference between machine


code and assembly language is that
the machine code is a language
consisting of binaries that can be
directly executed by a computer while
an assembly language is a low-level
programming language that requires
a software called an assembler to
convert it into machine code.

ASCII code 01000001 represents the letter "A"


SAP EXPERTS CENTER
TABLE OF CONTENTS

The main difference between machine


code and assembly language is that
the machine code is a language
consisting of binaries that can be
directly executed by a computer while
an assembly language is a low-level
programming language that requires
a software called an assembler to
convert it into machine code.
SAP EXPERTS CENTER
TABLE OF CONTENTS
C Overview

What is C ?
C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs.
SAP EXPERTS CENTER
TABLE OF CONTENTS
C++ Overview

What is C++?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup in 1979, as an extension to the C Language .
C++ gives programmers a high level of control over system resources and memory.
SAP EXPERTS CENTER
TABLE OF CONTENTS
C Vs C++

Similarities in C and C++


•Both have a similar syntax.
•Their compilation is similar.
•Both languages follow the same concept of a stack, heap, and static variable.

Features of C Features of C++


•Procedural •Object-Oriented
•Structured or Modular Programming language •General-Purpose Programming Language, supports features such as
(function driven language) Polymorphism, Inheritance, and Encapsulation
•Mid-level programming language •High-level programming language
•scanf , printf [ input / Output syntax ] •cout , cin [ input / Output syntax ]
•C does not support inheritance. •GUI and Interface
•32 Keywords •63 Keywords
•Errors and compile
SAP EXPERTS CENTER
TABLE OF CONTENTS
https://www.tutorialspoint.com/compile_cpp_online.php
C++ Editor
SAP EXPERTS CENTER
TABLE OF CONTENTS
C# Editor https://www.w3schools.com/cs/showjava.asp?filename=demo_strings_inter
SAP EXPERTS CENTER
https://visualstudio.microsoft.com/vs/
TABLE OF CONTENTS
Visual Studio
SAP EXPERTS CENTER
TABLE OF CONTENTS C#

Visual C#

C++ Visual C# . Net

Bjarne Stroustrup
C C# is used for:
•Mobile applications
Dennis Ritchie •Desktop applications
•Web applications
•Web services
•Web sites
•Games
•VR
•Database applications
C#

SAP EXPERTS CENTER Console.WriteLine("Enter username:");

string userName = Console.ReadLine();

Console.WriteLine("Username is: " + userName);


TABLE OF CONTENTS
Syntax
C++
string userName;
cout << "Type a number: ";
cin >> userName;
cout << "Your number is: " << userName;
C
string userName;
printf("Type a number: \n");
scanf("%s", userName);
printf("Your number is: %s", userName);
using System;
namespace HelloWorld C#
{

SAP EXPERTS CENTER class Program


{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
TABLE OF CONTENTS }
Syntax }

C++
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
C
return 0;
#include <stdio.h> }

int main() {
printf("Hello World!");
return 0;
}
SAP EXPERTS CENTER
TABLE OF CONTENTS
Comments
Single Comment
// This is a comment
cout << "Hello World!";
cout << "Hello World!"; // This is a comment

Multiple Comment
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
SAP EXPERTS CENTER
TABLE OF CONTENTS
Variables
•int - stores integers (whole numbers), without decimals, such as 123 or -123
•double - stores floating point numbers, with decimals, such as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
•string - stores text, such as "Hello World". String values are surrounded by double quotes
•bool - stores values with two states: true or false

Examples
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
SAP EXPERTS CENTER
TABLE OF CONTENTS
Constants
Variables const int minutesPerHour = 60;
Declaration const float PI = 3.14;
// Good identifiers

int minutesPerHour = 60;


// OK, but not so easy to understand what m actually is
int m = 60;

The general rules for naming variables are:


•Names can contain letters, digits and underscores
•Names must begin with a letter or an underscore (_)
•Names are case sensitive (myVar and myvar are different variables)
•Names cannot contain whitespaces or special characters like !, #, %, etc.
•Reserved words (like C++ keywords, such as int) cannot be used as names
SAP EXPERTS CENTER
TABLE OF CONTENTS
User Input

int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

cout is pronounced "see-out". Used for output, and uses the insertion operator (<<)
cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)
SAP EXPERTS CENTER
TABLE OF CONTENTS
Data Types
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
SAP EXPERTS CENTER
TABLE OF CONTENTS
Operators

Arithmetic operators Assignment operators


SAP EXPERTS CENTER
TABLE OF CONTENTS
Operators
Comparison operators

Logical operators
SAP EXPERTS CENTER
TABLE OF CONTENTS
Strings

// Include the string library


#include <string>

// Create a string variable


string greeting = "Hello";
SAP EXPERTS CENTER
TABLE OF CONTENTS
Math
// Include the cmath library
#include <cmath>

cout << sqrt(64);


cout << round(2.6);
cout << log(2);
cout << max(5, 10);
cout << min(5, 10);
SAP EXPERTS CENTER
TABLE OF CONTENTS
Booleans

bool isCodingFun = true;


bool isFishTasty = false; •YES / NO
cout << isCodingFun; // Outputs 1 (true) •ON / OFF
cout << isFishTasty; // Outputs 0 (false) •TRUE / FALSE
SAP EXPERTS CENTER
TABLE OF CONTENTS
Conditions
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
int time = 20;
if (time < 18) {
cout << "Good day.";
int time = 22;
} else {
if (time < 10) {
cout << "Good evening.";
cout << "Good morning.";
}
} else if (time < 20) {
// Outputs "Good evening."
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
SAP EXPERTS CENTER
TABLE OF CONTENTS int day = 4;
Switch switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
SAP EXPERTS CENTER
TABLE OF CONTENTS
While Loop

int i = 0; int i = 0;
while (i < 5) { do {
cout << i << "\n"; cout << i << "\n";
i++; i++;
} }
while (i < 5);
SAP EXPERTS CENTER
TABLE OF CONTENTS
For Loop

for (int i = 0; i < 5; i++) {


cout << i << "\n";
}
SAP EXPERTS CENTER
TABLE OF CONTENTS
Break/Continue

for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
if (i == 4) { if (i == 4) {
break; continue;
} }
cout << i << "\n"; cout << i << "\n";
} }

You might also like