GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — learn/doc/cpp_logic_loop at main · gyuho/learn

Expand file tree

/

cpp_logic_loop

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

cpp_logic_loop

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
code
code
 
 
README.md
README.md
 
 

README.md

Outline

back to contents

C++: logic, loop

  • logic
  • if
  • switch
  • for
  • while
  • fizzbuzz

↑ top




logic

#include <iostream>
using namespace std;

int main()
{
	bool b1 = true;
	bool b2 = false;
	cout << b1 && b2; // 1
	cout << endl;
	cout << b1 || b2; // 1
	cout << endl;

	int a = 10, b = 15, c = 20;
	cout << "a < b is "
		<< (a < b)
		<< endl;
	// a < b is 1
	
	cout << "b > c is "
		<< (b > c)
		<< endl;
	// b > c is 0
}

↑ top




if

#include <iostream>
using namespace std;

int main()
{
	int a = 100;

	if ( a == 1 )
	{
		cout << "a is 1" << endl;
	}
	else if ( a == 2 )
	{
		cout << "a is 2" << endl;
	}
	else
	{
		cout << "a is "
			<< a
			<< endl;
	}
	// a is 100
}

↑ top




switch

#include <iostream>
using namespace std;

int main()
{
	while (true)
	{
		cout << "Type an integer: ";
		int selected;
		cin >> selected;

		switch (selected)
		{
			case 0:
				{
					cout  << 0
						<< endl;
					break; // break out of switch
				}
			case 1:
				{
					cout  << 1
						<< endl;
					// continue; 
					// without this, it continues on 2
				}
			case 2:
				{
					cout  << 2
						<< endl;
					continue; // continue on while-loop
				}
			case 3:
				{
					cout  << 3
						<< endl;
					continue;
				}
			default:
				{
					cout  << "selected "
						<< selected
						<< endl;
				}
		}
		cout << "Breaking out of loop!" << endl;
		break;
	}
}

/*
Type an integer: 3
3
Type an integer: 2
2
Type an integer: 1
1
2
Type an integer: 0
0
Breaking out of loop!

Type an integer: 3
3
Type an integer: 2
2
Type an integer: 1
1
2
Type an integer: 5
selected 5
Breaking out of loop!
*/

↑ top




for

#include <iostream>
using namespace std;

int main()
{
	for ( int cnt = 0; cnt < 5; ++cnt )
	{
		cout << cnt
			<< endl;
	}
	// 0
	// 1
	// 2
	// 3
	// 4
}

↑ top




while

#include <iostream>
using namespace std;

int main()
{
	int cnt = 0;
	while ( cnt < 5 )
	{
		cout << cnt << endl;
		cnt++;
	}
	// 0
	// 1
	// 2
	// 3
	// 4
}

↑ top




fizzbuzz

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Fizz Buzz Test

#include <iostream>
using namespace std;

int main()
{
	for ( int i = 1; i < 101; i++ )
	{
		if ( i%15 == 0 )
		{
			cout << "FizzBuzz" << endl;
		}
		else if ( i%3 == 0 )
		{
			cout << "Fizz" << endl;
		}
		else if ( i%5 == 0 )
		{
			cout << "Buzz" << endl;
		}
		else 
		{
			cout << i << endl;
		}
	}
}

// ...
// Buzz
// 41
// Fizz
// 43
// 44
// FizzBuzz
// 46
// ...
 

↑ top




You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.