Summary

This document is a reviewer for the final exams of Computer Programming 1, BSIT 1-3, covering loops, arrays, strings, and functions.

Full Transcript

Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 Finals Coverage: Format: ​​ Lesson V: Loops while(condition){ ​​ Lesson VI: Arrays...

Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 Finals Coverage: Format: ​​ Lesson V: Loops while(condition){ ​​ Lesson VI: Arrays //Code to be executed; ​​ Lesson VII: Strings (di na raw pala ‘to kasama) } ​​ Lesson VIII: Functions ​ Example Code Lesson V: Loops C Looping ○​ Output -​ Data Processing -​ Automation -​ User Input Validation -​ Searching and Sorting The ‘do-while’ Loop Different Kinds of Loops in C -​ The do-while loop is similar to the while The ‘for’ Loop loop but guarantees that the loop body is -​ The for loop is often used when the number executed at least once. of iterations is known before entering the ​ do { loop. ​ //Code to be executed; ​ } while (condition); Format: ​ Example Code for (initialization; condition; increment/decrement){ //Code to be executed; } ​ Example Code ○​ Output ○​ Output Loop Control Statements Break -​ ‘Break’ statement is used to exit a loop The ‘while’ Loop prematurely when a specific condition is -​ The while loop is ideal when the exact met.c number of iterations is not known in advance. 1 kreiiii :D Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 -​ It immediately stops the loop's execution Lesson VII: Strings and transfers control to the first statement after the loop. Strings Continue -​ Sequence of characters, like words or -​ ‘Continue’ statement skips the current sentences, stored in memory. iteration of the loop and moves control to -​ In C, strings are simply arrays of characters. the next iteration. Example: -​ It does not terminate the loop but allows ​ char message = “Hello”; the loop to continue running from the next cycle onward. Lesson VI: Arrays ​ (p.s. \0 is the null terminator) ​ Null Terminator Arrays -​ \0 -​ Collection of elements of the same type -​ used to indicate the end of a string. stored in contiguous memory locations. ​ Scanf for String Input -​ The index is the location of the value, -​ This function can be used to read array indices always start at 0. strings from standard input. -​ One integer takes 4 bytes of memory. -​ It stops reading when it Example: ​ int numbers = {1, 2, 3, 4, encounters a whitespace 5}; character (space, tab, or newline). ​ char* name = {“Jerry”, “C.”, ​ Example Code “Calayan”} ​ Parts of an Array: ○​ Output (notice how it didn’t read Luna, because there is a whitespace between Juan and Luna) Declaration and Initialization ​ int numbers; //declaration ​ Fgets for String Input ​ numbers = 10; //initializing -​ This function is preferred for first element reading strings with spaces or ​ numbers = 20; //initializing entire lines. first element -​ It reads until a newline character is ​ int values[] = {10, 20, 30, 40, 50}; // declare and initialize encountered or the buffer is full. 2 kreiiii :D Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 ​ Example Code ​ strncat(str1, str2) ○​ This simply combines specific number of characters to the ○​ Output destination. ○​ strncat(str1, str2, n); ​ This will concatenate a (unlike scanf, the fgets function does not stop at a whitespace) specific number (n) of characters from str2 String Operations/Functions to str1. -​ Must include ​ ​ String Copy String Length ​ strcpy(str1, str2) ​ strlen(str) ​ This copies/replaces the ​ Returns the length of a string value of one string to (excluding the null terminator). another. Example Code Example Code ○​ Output (%zu is the standard way to print size_t values) ○​ Output ​ strncpy(str1, str2) ○​ This copies/places specific content of one ​ Sizeof ​ sizeof(str) string to another ○​ strncpy(str1, str2, ​ The only difference between strlen and n); sizeof is that sizeof also includes the ​ This will null terminator “\0” when counting. copy/replace ​ String Concatenate (n) characters ​ strcat(str1, str2) of str2 to str1. ​ Combines two strings. ​ ​ String Compare Example Code ​ strcmp(str1, str2) ​ Compare two strings. If the strings are equal, it will print 0; if they are not equal, it will ○​ Output print a negative value. 3 kreiiii :D Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 Two Types of Functions Example Code ​ Standard Library Functions ○​ Predefined functions Functions Header ​ printf() stdio.h ○​ Output ​ scanf() stdio.h ​ ​ ​ ​ (It printed 0 for the first sqrt() math.h strcmp because str1 and str2 are equal. However, pow() math.h since str1 and str3 are not equal, it printed -1.) main() ​ String Lower ​ strlwr (str) ​ User - Defined Functions ​ This converts uppercase ○​ A block of code written by characters into lowercase the user to perform a characters. specific action. Example Code ○​ return_type function_name(parameters) { ○​ Output ​ //code to be ​ ​ ​ executed ​ String Upper } ​ strupr (str) ​ Function declaration, ​ This converts lowercase characters definition, and calling. into uppercase characters. ​ Parameters and Example Code arguments. ​ Return types and void functions. ○​ Output ​ Scope of variables, ​ ​ ​ local and global variables. ​ Passing arrays to Lesson VIII: Functions functions. ○​ Example Function void greet(){ -​ A block of code that performs specific printf(“Happy Birthday!\n”); tasks. } 4 kreiiii :D Computer Programming 1 BSIT 1-3​ 1st Semester | S.Y. 2024 - 2025 Another example: #include void greet(char name[]); int main(){ greet("James"); greet(”David"); greet(”Mary"); return 0; } void greet(char name[]){ printf("Happy Birthday, %s!\n",name); } Return Types and Void Functions ​ Return Types -​ A function may return a value. -​ The return type of the function defines the data type of the value the function will return. -​ Common return types include int, float, char, and double. ​ Void Functions -​ If a function does not return a value, its return type is void. -​ These functions perform an action but do not return a value to the caller. goodluck chat 5 kreiiii :D

Use Quizgecko on...
Browser
Browser