Introduction to C++ Programming Language Basics

csc1201 programming language 2 n.w
1 / 22
Embed
Share

Master the fundamentals of C++ programming with this comprehensive overview covering topics such as Hello World program, data types, reserved words, escape sequences, declaring and initializing variables, and input statements. Learn from the structured content provided by Nouf Aljaffan in the CSC1201 course at KSU.

  • C++
  • Programming Basics
  • Data Types
  • Variables
  • Input Statements

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 1

  2. C++ OVERVIEW Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 2

  3. Hello World program #include <iostream> using namespace std; int main () { cout << Hello World\n ; Return 0; } include information from the standard input/output library, iostream. The instruction is more properly called a preprocessor instruction The # hash character starts the line to denote a preprocessor instruction. library name must be enclosed by < and > angled brackets. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 3

  4. Hello World program using namespace std; The second line of the program makes all the functions within the iostream library available for use by their standard names, which are in the namespace std. One of these is a function named cout that is used to write the output from a program. int main () In the function declaration the data type is specified as int, meaning integer. This means that after executing its statements this function must return an integer value to the operating system. The braces { } contain the statements to be executed by the program. The final statement in the main function Returna value of zero to the operating system to indicate that the program executed correctly. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 4

  5. DATA TYPES Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 5

  6. DATA TYPES Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 6

  7. RESERVED WORDS Key Words Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 7

  8. ESCAPE SEQUENCES Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 8

  9. Declaring and Initializing Variables To declare variables for example int first, second; char ch; double x; bool flage; To declare and initialize variables for example int first = 13, second = 10; char ch = A ; double x = 12.6; bool flage = true; Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 9

  10. Input (Read) Statement The syntax of cin together with >> is: cin >> variable >> variable >> .; For example: cin >> first >> second; Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 10

  11. Output The syntax of cout together with << is: cout << expression or manipulator << expression or manipulator .; The expression is evaluated and its value is printed at the current insertion point on the output device. A manipulator is used to format the output. The simplest manipulator is endl. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 11

  12. Working with String The C++ <string> class provides methods to manipulate strings of text. This is an example of Declaring and initializing a string variable #include <string> #include <iostream> using namespace std; int main() { string str = "C++ is fun"; } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 12

  13. Control Structures IF statement: if (condition) { statement(s); } IF Else statement: if (condition) { statement(s); True Branch } else { statement(s); False Branch } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 13

  14. Example : If statement #include <iostream> using namespace std; int main() { int num=2; bool flag=0; if( (num==2) && (flag) ) { cout << "The first test is true\n"; } else if( (num==2) && (!flag) ) { cout << "The second test is true\n"; } return 0; } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 14

  15. Switch Structures Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 15

  16. Example: Switch Structures #include <iostream> using namespace std; int main() { char letter; cout << "Enter any a-z character: "; cin >> letter; switch(letter) { case 'a' : case 'b' : case 'c' : default : cout << "Letter is not a, b or c\n"; } return 0; cout << "Letter \'a\' found\n"; break; cout << "Letter \'b\' found\n"; break; cout << "Letter \'c\' found\n"; break; } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 16

  17. Relational Operators in C++ Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 17

  18. Logical Operators in C++ Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 18

  19. While looping Structure While (condition) { } statement(s); Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 19

  20. Example: While loop #include <iostream> using namespace std; output: 0 5 10 15 20 int main() { int i=0; while( i<=20 ) { } cout << endl; return 0; } cout << i << " "; i = i + 5 ; Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 20

  21. For looping Structure for (expression1; condition; expression2) { statement(s) } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 21

  22. Example: For Loop Output : enter any number : 1 1*1=1 1*2=2 1*3=3 . . #include <iostream> using namespace std; int main() { int i,num; return 0; } cout<<"enter any number: "; cin>>num; for ( i=1 ; i<=10 ; i++ ) { cout << endl << num << "* << i << "= << num*i << endl; } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 22

Related


More Related Content